-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmergesort.m
67 lines (49 loc) · 1.99 KB
/
mergesort.m
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#import <Foundation/Foundation.h>
@interface MergeSort: NSObject
- (NSArray *) mergeSortArray:(NSArray *)unsortedArray;
@end
@implementation MergeSort
- (NSArray *) mergeArray:(NSArray *)leftArray rightArray:(NSArray *)rightArray {
NSMutableArray *returnArray = [NSMutableArray array];
int i = 0, e = 0;
while (i < [leftArray count] && e < [rightArray count]) {
int leftValue = [[leftArray objectAtIndex:i] intValue];
int rightValue = [[rightArray objectAtIndex:e] intValue];
if (leftValue < rightValue) {
[returnArray addObject: [leftArray objectAtIndex:i++]];
} else {
[returnArray addObject: [rightArray objectAtIndex:e++]];
}
}
while (i < [leftArray count]) {
[returnArray addObject: [leftArray objectAtIndex:i++]];
}
while (e < [rightArray count]) {
[returnArray addObject: [rightArray objectAtIndex:e++]];
}
return returnArray;
}
- (NSArray *) mergeSortArray:(NSArray *)unsortedArray {
// Time complexity: Worst case is: O(n * log(n)).
// Space complexity: O(3 * n) or O(n) due to the 3 NSArrays that are used.
int count = (int)[unsortedArray count];
if (count < 2) {
return unsortedArray;
}
int middle = count / 2;
NSArray *leftArray = [unsortedArray subarrayWithRange: NSMakeRange(0, middle)];
NSArray *rightArray = [unsortedArray subarrayWithRange: NSMakeRange(middle, (count - middle))];
NSArray *returnArray = [self mergeArray: [self mergeSortArray: leftArray] rightArray: [self mergeSortArray: rightArray]];
return returnArray;
}
@end
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSArray *mergeSorttData = @[@4, @3, @10, @44, @6, @4, @1, @7, @15];
MergeSort *ms = [[MergeSort alloc] init];
// ms.cycles = 0;
NSArray *mergeSortedArray = [ms mergeSortArray: mergeSorttData];
NSLog(@"mergeSortedArray: %@", mergeSortedArray);
}
return 0;
}