Merge array B into array A. Both A and B are originally sorted, and the result should remain sorted. Insertion sort. Though I wonder why my insertion sort implementation turned out so verbose. Code below:
classSolution{public:voidmerge(intA[],intm,intB[],intn){// Note: The Solution object is instantiated only once and is reused by each test case.
for(inti=0;i<n;i++){Insert(A,m+i,B[i]);}}voidInsert(intA[],intlengthA,intval){if(lengthA==0){A[lengthA]=val;return;}if(val>A[lengthA-1]){A[lengthA]=val;return;}inti;for(i=lengthA-1;i>=0;i--){if(val<A[i]){A[i+1]=A[i];}else{A[i+1]=val;break;}}if(i==-1){A[0]=val;}}};