ios代码示例 实现快排算法
2016-12-14 16:21
363 查看
快排:快速度排序是经典的divide & conquer (分治算法)
思想:
在数组中取得一个值为标兵
对标兵左右区间进行划分,将比标兵大的放到又边,小的放到左边,倒序反之。
重复上过程,到对比结束
代码如下:
思想:
在数组中取得一个值为标兵
对标兵左右区间进行划分,将比标兵大的放到又边,小的放到左边,倒序反之。
重复上过程,到对比结束
代码如下:
#import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSMutableArray *dataSorce = [[NSMutableArray alloc] initWithObjects:@(6), @(1),@(2),@(5),@(9),@(4),@(3),@(7),nil]; [self quickExhaut:dataSorce WhithLeft:0 AndRight:dataSorce.count-1]; NSLog(@"dataSorce value is%@",dataSorce); } -(void)quickExhaut:(NSMutableArray *)dateSource WhithLeft:(NSInteger )left AndRight:(NSInteger)right{ if (left>=right) { return; } NSInteger i = left; NSInteger j = right; //标兵值 NSInteger key = [dateSource [i] integerValue]; //查询 while (i<j) { while (i<j && [dateSource[j] integerValue] >= key) { j--; } //如果小于标兵就放到前面 dateSource[i]= dateSource[j] ; while (i<j && [dateSource[i] integerValue]<=key) { i++; } dateSource[j] = dateSource[i]; } //排序基准数左边的 dateSource[i] = @(key); [self quickExhaut:dateSource WhithLeft:left AndRight:i - 1]; //排序基准数右边的 [self quickExhaut:dateSource WhithLeft:i + 1 AndRight:right]; }
相关文章推荐
- iOS上下文实现评价星星示例代码
- iOS实现“摇一摇”与“扫一扫”功能示例代码
- iOS实现导航栏透明示例代码
- iOS利用AFNetworking实现文件上传的示例代码
- iOS实现爆炸的粒子效果示例代码
- ios实现底部PopupWindow的示例代码(底部弹出菜单)
- iOS 中weak的实现代码示例
- iOS实现只有底部边框线的输入框示例代码
- iOS实现“摇一摇”与“扫一扫”功能示例代码
- iOS实现动态的开屏广告示例代码
- iOS三级联动选择器的实现代码示例
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)
- AjaxPro.NET实现TextBox智能获取服务端数据功能(Asp.net 2.0)(示例代码下载)
- AjaxPro.NET框架实现服务端即时数据验证(Asp.net 2.0)(示例代码下载)
- Asp.net 2.0 GridView的几个事件(如实现: 行的双击/单击/捕捉键盘按键/鼠标悬浮/移出效果)(示例代码下载)
- Singleton模式的C++实现研究(示例代码)
- AjaxPanel自定义控件实现页面无刷新数据交互(做了个示例程序, 效果确实比较Cool, 用法非常简单! )(示例代码下载)