选择排序 冒泡排序 插入排序 快速排序 堆排序
http://www.jianshu.com/p/70619984fbc6
http://www.jianshu.com/p/9a456d1b59b5
Objective-C ^ ^.
NSMutableArray+JXSort.m
- (void)jx_selectionSortUsingComparator:(JXSortComparator)comparator didExchange:(JXSortExchangeCallback)exchangeCallback {
if (self.count == 0) {
return;
}
for (NSInteger i = 0; i < self.count - 1; i ++) {
for (NSInteger j = i + 1; j < self.count; j ++) {
if (comparator(self[i], self[j]) == NSOrderedDescending) {
[self jx_exchangeWithIndexA:i indexB:j didExchange:exchangeCallback];
}
}
}
}
- (void)jx_bubbleSortUsingComparator:(JXSortComparator)comparator didExchange:(JXSortExchangeCallback)exchangeCallback {
if (self.count == 0) {
return;
}
for (NSInteger i = self.count - 1; i > 0; i --) {
for (NSInteger j = 0; j < i; j ++) {
if (comparator(self[j], self[j + 1]) == NSOrderedDescending) {
[self jx_exchangeWithIndexA:j indexB:j + 1 didExchange:exchangeCallback];
}
}
}
}
- (void)jx_insertionSortUsingComparator:(JXSortComparator)comparator didExchange:(JXSortExchangeCallback)exchangeCallback {
if (self.count == 0) {
return;
}
for (NSInteger i = 1; i < self.count; i ++) {
for (NSInteger j = i; j > 0 && comparator(self[j], self[j - 1]) == NSOrderedAscending; j --) {
[self jx_exchangeWithIndexA:j indexB:j - 1 didExchange:exchangeCallback];
}
}
}
1 2 2
pivot
i
j
i
j
j``x
pivot``pivot ... x
x``pivot
6. x ... pivot``j``pivot``i``x
7. i``y``pivot
pivot ... y``i
pivotpivoti
8. i``i``x``j``x``pivot``i``x``x``pivot
j``i``i ++
i``j``j --
9.
i``j``i``j``pivot``i
~
- (void)jx_quickSortUsingComparator:(JXSortComparator)comparator didExchange:(JXSortExchangeCallback)exchangeCallback {
if (self.count == 0) {
return;
}
[self jx_quickSortWithLowIndex:0 highIndex:self.count - 1 usingComparator:comparator didExchange:exchangeCallback];
}
- (void)jx_quickSortWithLowIndex:(NSInteger)low highIndex:(NSInteger)high usingComparator:(JXSortComparator)comparator didExchange:(JXSortExchangeCallback)exchangeCallback {
if (low >= high) {
return;
}
NSInteger pivotIndex = [self jx_quickPartitionWithLowIndex:low highIndex:high usingComparator:comparator didExchange:exchangeCallback];
[self jx_quickSortWithLowIndex:low highIndex:pivotIndex - 1 usingComparator:comparator didExchange:exchangeCallback];
[self jx_quickSortWithLowIndex:pivotIndex + 1 highIndex:high usingComparator:comparator didExchange:exchangeCallback];
}
- (NSInteger)jx_quickPartitionWithLowIndex:(NSInteger)low highIndex:(NSInteger)high usingComparator:(JXSortComparator)comparator didExchange:(JXSortExchangeCallback)exchangeCallback {
id pivot = self[low];
NSInteger i = low;
NSInteger j = high;
while (i < j) {
// pivot
while (i < j && comparator(self[j], pivot) != NSOrderedAscending) {
j --;
}
if (i < j) {
// ijpivot
[self jx_exchangeWithIndexA:i indexB:j didExchange:exchangeCallback];
i ++;
}
/// pivot
while (i < j && comparator(self[i], pivot) != NSOrderedDescending) {
i ++;
}
if (i < j) {
// ijpivot
[self jx_exchangeWithIndexA:i indexB:j didExchange:exchangeCallback];
j --;
}
}
return i;
}