首页 > 代码库 > 快速排序
快速排序
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSMutableArray *arr = @[@"9", @"8", @"2", @"6", @"1"].mutableCopy; [self QuickSort:arr StartIndex:0 EndIndex:4]; NSLog(@"%@", arr); } -(void)QuickSort:(NSMutableArray *)list StartIndex:(NSInteger)startIndex EndIndex:(NSInteger)endIndex{ if(startIndex >= endIndex)return; NSNumber * temp = [list objectAtIndex:startIndex]; NSInteger tempIndex = startIndex; //临时索引 处理交换位置(即下一个交换的对象的位置) for(int i = (int)startIndex + 1 ; i <= endIndex ; i++){ NSNumber *t = [list objectAtIndex:i]; if([temp intValue] > [t intValue]){ tempIndex = tempIndex + 1; [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:i]; } } [list exchangeObjectAtIndex:tempIndex withObjectAtIndex:startIndex]; [self QuickSort:list StartIndex:startIndex EndIndex:tempIndex-1]; [self QuickSort:list StartIndex:tempIndex+1 EndIndex:endIndex]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
打印结果
2016-12-22 17:56:14.546 text[31326:1735436] (
1,
2,
6,
8,
9
)
快速排序
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。