NSMutaleArray Quick Sort
2017-06-20 15:35
281 查看
#import<Foundation/Foundation.h>
@interface NSMutableArray (QuickSort)
- (void)quickSortUsingComparator:(NSComparator)comparator;
@end
//
// NSArray+Sort.m
// Test
//
// Created by Ansel on 2017/6/20.
// Copyright © 2017年 Ansel. All rights reserved.
//
#import "NSMutableArray+QuickSort.h"
@implementation NSMutableArray (QuickSort)
- (void)quickSortUsingComparator:(NSComparator)comparator
{
[[selfclass]quickSortForArray:selfwithStartIndex:0endIndex:[selfcount]
-1comparator:comparator];
}
#pragma mark - PrivateMethod
+ (void)quickSortForArray:(NSMutableArray *)array withStartIndex:(NSInteger)startIndex
endIndex:(NSInteger)endIndex comparator:(NSComparator)comparator
{
NSInteger count = [arraycount];
//异常处理
if (!array || count <=0 || startIndex <0 || endIndex
>= count || !comparator) {
return;
}
//递归结束
if (startIndex == endIndex) {
return;
}
NSInteger index = [self partitionForArray:arraywithStartIndex:startIndexendIndex:endIndexcomparator:comparator];
if (index > startIndex) {
[self quickSortForArray:arraywithStartIndex:startIndexendIndex:index
-1comparator:comparator];
}
if (index < endIndex) {
[self quickSortForArray:arraywithStartIndex:index
+1endIndex:endIndexcomparator:comparator];
}
}
+ (NSInteger)partitionForArray:(NSMutableArray *)array withStartIndex:(NSInteger)startIndex
endIndex:(NSInteger)endIndex comparator:(NSComparator)comparator
{
//把数组最后的元素作为比较,findIndex就是元素最后位置
NSInteger findIndex = startIndex -1;
//把小的数据放到findIndex之前,大的放到之后
for (NSInteger index = startIndex; index < endIndex; index++) {
if (comparator(array[index], array[endIndex]) !=NSOrderedDescending) {
findIndex++;
if (findIndex != index) {
[array exchangeObjectAtIndex:findIndexwithObjectAtIndex:index];
}
}
}
//把最后一个元素放到争取的位置
++findIndex;
[array exchangeObjectAtIndex:findIndexwithObjectAtIndex:endIndex];
return findIndex;
}
@end
@interface NSMutableArray (QuickSort)
- (void)quickSortUsingComparator:(NSComparator)comparator;
@end
//
// NSArray+Sort.m
// Test
//
// Created by Ansel on 2017/6/20.
// Copyright © 2017年 Ansel. All rights reserved.
//
#import "NSMutableArray+QuickSort.h"
@implementation NSMutableArray (QuickSort)
- (void)quickSortUsingComparator:(NSComparator)comparator
{
[[selfclass]quickSortForArray:selfwithStartIndex:0endIndex:[selfcount]
-1comparator:comparator];
}
#pragma mark - PrivateMethod
+ (void)quickSortForArray:(NSMutableArray *)array withStartIndex:(NSInteger)startIndex
endIndex:(NSInteger)endIndex comparator:(NSComparator)comparator
{
NSInteger count = [arraycount];
//异常处理
if (!array || count <=0 || startIndex <0 || endIndex
>= count || !comparator) {
return;
}
//递归结束
if (startIndex == endIndex) {
return;
}
NSInteger index = [self partitionForArray:arraywithStartIndex:startIndexendIndex:endIndexcomparator:comparator];
if (index > startIndex) {
[self quickSortForArray:arraywithStartIndex:startIndexendIndex:index
-1comparator:comparator];
}
if (index < endIndex) {
[self quickSortForArray:arraywithStartIndex:index
+1endIndex:endIndexcomparator:comparator];
}
}
+ (NSInteger)partitionForArray:(NSMutableArray *)array withStartIndex:(NSInteger)startIndex
endIndex:(NSInteger)endIndex comparator:(NSComparator)comparator
{
//把数组最后的元素作为比较,findIndex就是元素最后位置
NSInteger findIndex = startIndex -1;
//把小的数据放到findIndex之前,大的放到之后
for (NSInteger index = startIndex; index < endIndex; index++) {
if (comparator(array[index], array[endIndex]) !=NSOrderedDescending) {
findIndex++;
if (findIndex != index) {
[array exchangeObjectAtIndex:findIndexwithObjectAtIndex:index];
}
}
}
//把最后一个元素放到争取的位置
++findIndex;
[array exchangeObjectAtIndex:findIndexwithObjectAtIndex:endIndex];
return findIndex;
}
@end
相关文章推荐
- NSMutableArray Merge Sort
- How to sort an NSMutableArray with custom objects in it?
- 0003-¥¥¥¥可变数组按条件排序sort NSMutableArray
- NSMutableArray Heap Sort
- Array.sort()学习到的
- NSArray,NSMutableArray,NSXXXXX,NSMutableXXXXX等之间的本质区别(堆,栈)
- [leetcode] 360. Sort Transformed Array 解题报告
- array=nil 和 Array=[[NSMutableArray alloc]init]; 的区别
- NSMutableArray的一些用法
- 黑马程序员——Foundation框架——NSArray和NSMutableArray
- NSMutableArray中存放struct以及NSValue详解
- Objective-C学习之NSMutableArray中arraywithcapacity 和 initwithcapacity的区别?
- NSMutableArray and NSDictionary
- iPhone/iOS中保存自定义对象(Custom Object/Custom Class)的数组(NSMutableArray/NSArray)到NSUserDefaults
- geeksforgeeks@ Sorting Elements of an Array by Frequency (Sort)
- iOS: NSMutableArray的方法removeObject:
- NSArray,NSMutableArray的使用与内存问题
- Objective-C语法之NSArray和NSMutableArray
- objc写一个NSMutableArray不连续索引替换对象的方法
- javascript学习笔记之array.sort