Skip to content

Instantly share code, notes, and snippets.

@minsOne
Created August 12, 2014 14:59
Show Gist options
  • Select an option

  • Save minsOne/6e4b1c0656d15337e257 to your computer and use it in GitHub Desktop.

Select an option

Save minsOne/6e4b1c0656d15337e257 to your computer and use it in GitHub Desktop.
Bubble Sort in Objective-C
- (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