Idea: general idea: first, take an array as the merged array, then traverse each element of the second array, compare one by one, and insert it when you find the right one;

Simple idea: set array C, compare the first element of array A and array B, find the smallest, put it into array C, and go on in turn

The code is as follows:

- (NSArray *)mergeOrderArrayWithFirstArray: (NSMutableArray *)array1 secondArray: (NSMutableArray *)array2 {
    //All empty
    if (!array1.count && !array2.count) {
        return @[];
    }
    //One is empty and the other is returned
    if (!array1.count) {
        return array2;
    }
    if (!array2.count) {
        return array1;
    }
    NSMutableArray *endArray = [NSMutableArray array];
    while (1) {
        if ([array1[0] integerValue] < [array2[0] integerValue]) {
            [endArray addObject:array1[0]];
            [array1 removeObjectAtIndex:0];
        }else  {
            [endArray addObject:array2[0]];
            [array2 removeObjectAtIndex:0];
        }
        if (!array1.count) {
            [endArray addObjectsFromArray:array2];
            break;
        }
        if (!array2.count) {
            [endArray addObjectsFromArray:array1];
            break;
        }
    }
    return endArray;
}

 

Execute the code, input and output the results as follows: