Created
February 15, 2014 16:08
-
-
Save yoman07/9021374 to your computer and use it in GitHub Desktop.
Objective C Quick Sort
This file contains 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*) quickSortArray:(NSArray*) arr { | |
if(arr.count <2) { | |
return arr; | |
} | |
NSMutableArray *mutableArr = [arr mutableCopy]; | |
NSMutableArray *leftArray = [[NSMutableArray alloc] init]; | |
NSMutableArray *rightArray = [[NSMutableArray alloc] init]; | |
NSUInteger pivot = arr.count/2; | |
NSNumber *pivotValue = [mutableArr objectAtIndex:pivot]; | |
[mutableArr removeObjectAtIndex:pivot]; | |
for(int i=0;i<mutableArr.count;i++) { | |
NSNumber *val = [mutableArr objectAtIndex:i]; | |
if([val floatValue] < [pivotValue floatValue] ) { | |
[leftArray addObject:val]; | |
} else { | |
[rightArray addObject:val]; | |
} | |
} | |
leftArray = [[self quickSortArray:leftArray] mutableCopy]; | |
rightArray = [[self quickSortArray:rightArray ] mutableCopy]; | |
[leftArray addObject:pivotValue]; | |
[leftArray addObjectsFromArray:rightArray]; | |
return leftArray; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment