算法-键索引计数法
2015-10-05 12:28
375 查看
键索引计数法适合于整数分为较小的简单排序方法,基本的步骤分为四步:
1.统计每个分类出现的次数;
2.将分类的次数转换为对应的索引;
3.通过中间数组按照分类的权重对原始数组排序;
4.将排序之后中间数组赋值给原始数组;
实现具体排序方法:
排序结果:
1.统计每个分类出现的次数;
2.将分类的次数转换为对应的索引;
3.通过中间数组按照分类的权重对原始数组排序;
4.将排序之后中间数组赋值给原始数组;
基础定义
首先定义排序需要的需要类:@interface keyIndexModel:NSObject @property (assign,nonatomic) NSInteger key; @property (strong,nonatomic) NSString *value; -(instancetype)initWithKeyValue:(NSInteger)key value:(NSString *)value; @end @interface KeyIndexSort : NSObject -(void)sort:(NSMutableArray *)dataSource categoryNumber:(NSInteger)number; @end
实现具体排序方法:
-(void)sort:(NSMutableArray *)dataSource categoryNumber:(NSInteger)number{ NSInteger len=[dataSource count]; NSMutableArray *tempArr=[[NSMutableArray alloc]initWithCapacity:1]; //http://www.cnblogs.com/xiaofeixiang/ for (NSInteger i=0; i<len; i++) { [tempArr addObject:[NSNull null]]; } NSMutableArray *count=[[NSMutableArray alloc]initWithCapacity:1]; for (NSInteger i=0; i<number+1; i++) { [count addObject:[NSNumber numberWithInteger:0]]; } //统计每个分类的次数 for (NSInteger i=0;i<len; i++) { keyIndexModel *model=dataSource[i]; count[model.key+1]=[NSNumber numberWithInteger:[count[model.key+1] integerValue]+1]; } //将出现的次数转换为索引,第一组3次,第二组5次,因此对应第三组开始的索引是8 for (NSInteger j=0; j<number; j++) { count[j+1]=[NSNumber numberWithInteger:[count[j] integerValue]+[count[j+1] integerValue]]; } //将元素从上到下分类 for (NSInteger m=0; m<len; m++) { keyIndexModel *model=dataSource[m]; tempArr[[count[model.key] integerValue]]=model; count[model.key]=[NSNumber numberWithInteger:[count[model.key] integerValue]+1]; } //重新排序赋值 for (NSInteger i=0; i<len; i++) { dataSource[i]=tempArr[i]; } }
排序测试
NSMutableArray *dataSource=[[NSMutableArray alloc]initWithCapacity:1]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:0 value:@"FlyElephant"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:1 value:@"keso"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:2 value:@"中山郎"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:3 value:@"http://www.cnblogs.com/xiaofeixiang/"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:3 value:@"iOS技术交流:228407086"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:2 value:@"北京"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:1 value:@"上海"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:3 value:@"深圳"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:2 value:@"广州"]]; [dataSource addObject: [[keyIndexModel alloc]initWithKeyValue:0 value:@"河南"]]; KeyIndexSort *indexSort=[[KeyIndexSort alloc]init]; [indexSort sort:dataSource categoryNumber:5]; [dataSource enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { keyIndexModel *model=obj; NSLog(@"%ld--%@",idx,model.value); }];
排序结果:
相关文章推荐
- 网警1277号正在监控聊天记录中…
- FloatingActionButton,TextInputLayout,Snackbar,TabLayout 四种控件
- 排序一:快速排序
- 大话离散时间信号处理(一)
- C++ 基础知识点 八 第9章 继承性与派生类
- 职业生涯规划的建议
- 条款9:绝不要在构造以及析构函数中调用虚函数
- Codeforces Round #323
- C++刷称号——2707: 素数与要素
- Python爬虫判断url链接的是下载文件还是html文件
- C++ 基础知识点 七 第8章 类和对象的创建
- Agile.Net 组件式开发平台 - 数据访问组件
- C++ 基础知识点 五 第6章 指针、引用和动态空间管理
- Java多线程12:ReentrantLock中的方法
- 【NOIP 2006】Jam的计数法
- CCS绘制成的三角形箭头
- Android listView 替换技术 RecyclerView And CardView
- Android简便通用的SimpleBaseAdapter
- 人工湖公路(树状数组)
- 计算机的存储单位