iOS插入排序
来源:互联网 发布:淘宝虚假交易怎么清洗, 编辑:程序博客网 时间:2024/05/18 00:07
#import <Foundation/Foundation.h>@interface InsertSort : NSObject/// 直接插入排序+ (void)insertSort:(NSMutableArray *)list;/// 折半插入排序+ (void)binaryInsertSort:(NSMutableArray *)list;@end
#import "InsertSort.h"@implementation InsertSort+ (void)insertSort:(NSMutableArray *)list{ for (NSInteger i = 1; i< list.count; i++) { NSInteger j = i; NSInteger temp = [[list objectAtIndex:i]integerValue]; while (j>0 && temp < [[list objectAtIndex:j - 1] integerValue]) { [list replaceObjectAtIndex:j withObject:[list objectAtIndex:(j - 1)]]; } [list replaceObjectAtIndex:j withObject:[NSNumber numberWithInteger:temp]]; }}+ (void)binaryInsertSort:(NSMutableArray *)list{ for (NSInteger i =1; i<list.count; i++) { NSInteger temp = [[list objectAtIndex:i]integerValue]; NSInteger left = 0; NSInteger right = i-1; while (left <= right) { NSInteger minddle = (left + right)/2; if(temp < [[list objectAtIndex:minddle]integerValue]){ right = minddle - 1; }else{ left = minddle + 1; } } for (NSInteger j = i; j>left; j--) { [list replaceObjectAtIndex:j withObject:[list objectAtIndex:j-1]]; } [list replaceObjectAtIndex:left withObject:[NSNumber numberWithInteger:temp]]; }}@end
0 0
- iOS 插入排序
- iOS插入排序
- 插入排序算法(iOS)
- iOS开发算法--插入排序
- iOS算法总结-插入排序
- iOS疯狂详解之排序(选择排序/插入排序)
- IOS算法(三)之插入排序
- IOS- 快速排序,冒泡排序,直接插入排序和折半插入排序,希尔排序,堆排序,直接选择排序
- 插入排序-【插入排序】
- 算法 -- iOS开发用Objective_C / Swift3.0实现:直接插入排序 / 二分法插入排序 / 希尔排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- 插入排序
- lcd
- iOS 通过设置颜色去除UISearchBar的灰色背景
- CSV操作(导出和导入)
- sockaddr_in , sockaddr , in_addr区别Socket编程函数集(非常有用)
- Androidstudio加载工程很慢解决方案
- iOS插入排序
- iOS编译时报错The run destination John的 iPhone is not valid for Running the scheme 'Test'.
- c#实现将DataTable中的数据导出到DBF文件
- JAVA 泛型 - Class<T>
- 解决安卓调用ffmpeg函数avformat_open_input进行转码出现打不开文件
- fiddler抓取https请求(android/ios)
- 15自由度机器人课程日志(二)程序下载
- Spring配置文件-applicationContext.xml
- Angular内置服务