之前一直搞不懂快速排序,今早在地铁上看到一偏文章讲得很好,自己做一个笔记加深印象,用OC实现了一下
快速排序
- (NSArray *)quickSourct:(NSMutableArray<NSNumber * > *)unSourcArray
startIndex:(int)startIndex
endIndex:(int)endIndex {
int middle = (startIndex + endIndex) / 2;
int middleValue = [unSourcArray[middle] intValue];
int i = startIndex;
int j = endIndex;
while (i <= j) {
while ([unSourcArray[i] integerValue] < middleValue) {
i ++;
}
while ([unSourcArray[middle] integerValue] < middleValue) {
j --;
}
NSLog(@"%@", unSourcArray);
i ++;
j --;
}
if (startIndex < j) {
[self quickSourct:unSourcArray startIndex:startIndex endIndex:j];
}
if (i < endIndex) {
[self quickSourct:unSourcArray startIndex:i endIndex:endIndex];
}
return unSourcArray;
}
网友评论