常用的算法排序问题-----OC
来源:互联网 发布:ubuntu查看gpu 编辑:程序博客网 时间:2024/06/10 23:28
常用的算法排序问题—–OC
//OC 中常用的一些排序 NSArray *arr = @[@"1",@"2",@"5",@"3",@"6",@"9",@"5",]; NSMutableArray *oldArr = [NSMutableArray arrayWithArray:arr]; NSLog(@"%@",oldArr); [self bubbleSort:oldArr]; [self selectionSort:oldArr]; [self insertSor:oldArr]; [self quickSort:oldArr leftIndex:1 rightIndex:5]; }#pragma mark - bubbleSort/** ①.冒泡排序算法 @param array 参数 */- (void)bubbleSort:(NSMutableArray *)array { for (int i = 0; i < array.count; i++) { for (int j = 0; j < array.count-i-1; j++) { if ([array[j+1]integerValue] < [array[j]integerValue]) { int temp = [array[j] integerValue]; array[j] = array[j+1]; array[j+1] = [NSNumber numberWithInt:temp]; } } } NSLog(@"冒泡排序后: %@",array);}#pragma mark - selectionSort/** ②.选择排序 @param array 参数 */- (void)selectionSort:(NSMutableArray *)array { for (int i = 0; i < array.count; i++) { for (int j = i+1; j <array.count ; j++) { if ([array[i]integerValue] > [array[j]integerValue]) { int temp = [array[i] integerValue]; array[i] = array[j]; array[j] = [NSNumber numberWithInt:temp]; } } } NSLog(@"选择排序后: %@",array);}#pragma mark - insertSort/** ③.插入排序 @param array 参数 */- (void)insertSor:(NSMutableArray *)array { for (int i = 1; i < array.count; i++) { int temp = [array[i]integerValue]; for (int j = i-1; j>=0 && temp <[array[j] integerValue]; j--) { array[j+1] = array[j]; array[j] = [NSNumber numberWithInt:temp]; } } NSLog(@"插入排序后: %@",array);}#pragma mark - quickSort/** ④.快速排序 @param array 参数 */- (void)quickSort:(NSMutableArray *)array leftIndex:(int)left rightIndex:(int)right { if (left < right) { int temp = [self getMiddleIndex:array leftIndex:left rightIndex:right]; [self quickSort:array leftIndex:left rightIndex:temp-1]; [self quickSort:array leftIndex:temp+1 rightIndex:right]; } NSLog(@"快速排序后: %@",array);}- (int)getMiddleIndex:(NSMutableArray *)array leftIndex:(int)left rightIndex:(int)right { int tempValue = [array[left] integerValue]; while (left < right) { while (left < right && tempValue <= [array[right] integerValue]) { right--; } if (left < right) { array[left]=array[right]; } while (left < right && [array[left] integerValue] <= tempValue) { left++; } if (left < right) { array[left] = array[right]; } } array[left] = [NSNumber numberWithInt:tempValue]; return left;}
GitHub 代码地址
0 0
- 常用的算法排序问题-----OC
- OC 的常用排序方式
- OC 的冒泡排序问题
- OC中常用的数组排序
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- 常用的排序算法
- win7 vs2015 编译SiftGPU动态库
- android 7.0读取文件报异常信息
- 光流Optical Flow介绍与OpenCV实现
- 关于OOM的那些事儿
- 5.3号学习日志(1)
- 常用的算法排序问题-----OC
- VB6.0 读写excel表格/dat文件并保存
- 关于架构的一点点思考
- iOS CocoaPods使用详细说明
- 有关Java编写GUI界面的文件读取和保存操作
- Android 多击事件
- hadoop分布式安装hbase
- spark多文件输出
- PrintWriter输出到页面中呈现乱码现象解决方案