Created
August 12, 2014 14:59
-
-
Save minsOne/6e4b1c0656d15337e257 to your computer and use it in GitHub Desktop.
Bubble Sort in Objective-C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| - (NSArray *)bubbleSort:(NSMutableArray *)unsorted comparator:(NSComparator)comparator | |
| { | |
| BOOL done = false; | |
| while (!done) { | |
| done = true; | |
| for (int i = 1; i < unsorted.count; i++) { | |
| if (comparator( [unsorted objectAtIndex:i-1], [unsorted objectAtIndex:i] ) == NSOrderedDescending ) { | |
| [unsorted exchangeObjectAtIndex:i withObjectAtIndex:i-1]; | |
| done = false; | |
| } | |
| } | |
| NSLog(@"%@", unsorted); | |
| } | |
| return unsorted; | |
| } | |
| int main(int argc, const char * argv[]) { | |
| NSLog(@"Sorted : %@", [self bubbleSort:[NSMutableArray arrayWithArray:a] comparator:^NSComparisonResult(id obj1, id obj2) { | |
| return [obj1 compare:obj2]; | |
| }]); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment