[leetcode_88]Merge Sorted Array

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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
class Solution {
public:
    void merge(int A[], int m, int B[], int n) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        for(int i = 0;i < n;i++)
        {
            Insert(A,m+i,B[i]);
        }
    }
    void Insert(int A[],int lengthA,int val)
    {
        if(lengthA == 0)
        {
            A[lengthA] = val;
            return;
        }
        if(val > A[lengthA-1])
        {
            A[lengthA] = val;
            return ;
        }
        int i;
        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;
        }
    }
};