咕泡Java架构师 p7技术专家-西西软件站漫画
Objective-C语言实现:
- (NSArray *)mergeWithArray:(NSArray *)sourceArray startIndex:(NSInteger)startIndex midIndex:(NSInteger)midIndex endIndex:(NSInteger)endIndex{ NSMutableArray *sourceMutableArray = [NSMutableArray arrayWithArray:sourceArray]; NSMutableArray *tempMutableArray = [[NSMutableArray alloc] init]; NSInteger i = startIndex; NSInteger j = midIndex + 1; NSInteger k = startIndex; while (i != midIndex && j != endIndex){ if (sourceMutableArray[i] > sourceMutableArray[j]) { //tempMutableArray[k] = sourceMutableArray[j]; [tempMutableArray replaceObjectAtIndex:k withObject:sourceMutableArray[j]]; k ++; j ++; }else{ //tempMutableArray[k] = sourceMutableArray[i]; [tempMutableArray replaceObjectAtIndex:k withObject:sourceMutableArray[i]]; k ++; i ++; } }