算法-键索引计数法

键索引计数法适合于整数分为较小的简单排序方法,基本的步骤分为四步:

1.统计每个分类出现的次数;

2.将分类的次数转换为对应的索引;

3.通过中间数组按照分类的权重对原始数组排序;

4.将排序之后中间数组赋值给原始数组;

基础定义

首先定义排序需要的需要类:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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

  实现具体排序方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-(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];
    }
     
}

排序测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
 }];

 排序结果:

posted @   Fly_Elephant  阅读(1682)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示