算法-低位优先的字符串排序
低位优先的字符串排序相当于是对键索引计数方法的一个扩展,主要用于处理固定长度字符串,比如说手机号,固定电话,银行卡卡号,字符串的长度为N,从右向左开始进行每个键作为值开始遍历,实现比较简单:
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 34 35 | -( void )lowSort:(NSMutableArray *)dataSource singleLength:(NSInteger)len { NSInteger sourceCount=[dataSource count]; NSInteger R=256; NSMutableArray *tempArr=[[NSMutableArray alloc]initWithCapacity:1]; for (NSInteger i=0; i<sourceCount; i++) { [tempArr addObject:[NSNull null ]]; } for (NSInteger d=len-1; d>=0; d--) { NSMutableArray *count=[[NSMutableArray alloc]initWithCapacity:1]; for (NSInteger i=0; i<R+1; i++) { [count addObject:[NSNumber numberWithInteger:0]]; } //统计频率 for (NSInteger i=0; i<sourceCount; i++) { NSString *str=[dataSource objectAtIndex:i]; NSInteger charValue=[str characterAtIndex:d]-48; count[charValue+1]=[NSNumber numberWithInteger:[count[charValue+1] integerValue]+1]; } for (NSInteger j=0; j<R; j++) { count[j+1]=[NSNumber numberWithInteger:[count[j] integerValue]+[count[j+1] integerValue]]; } //将元素从上到下分类 for (NSInteger m=0; m<sourceCount; m++) { NSString *str=[dataSource objectAtIndex:m]; NSInteger charValue=[str characterAtIndex:d]-48; tempArr[[count[charValue] integerValue]]=dataSource[m]; count[charValue]=[NSNumber numberWithInteger:[count[charValue] integerValue]+1]; } //重新排序赋值 for (NSInteger i=0; i<sourceCount; i++) { dataSource[i]=tempArr[i]; } } } |
代码测试:
1 2 3 4 5 6 7 8 | LSD *lsd=[[LSD alloc]init]; NSMutableArray *dataSource=[[NSMutableArray alloc]initWithObjects: @"12345" , @"23456" , @"78901" , @"89764" , @"12345" , @"45678" , @"89794" , @"89754" , @"64532" , @"69784" ,nil]; [lsd lowSort:dataSource singleLength:7]; [dataSource enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { NSLog( @"%@" ,obj); }]; NSLog( @"技术交流群:%@" , @"228407086" ); NSLog( @"博客园-FlyElephant:http://www.cnblogs.com/xiaofeixiang" ); |
效果如下:
作者:FlyElephant
出处:http://www.cnblogs.com/xiaofeixiang
说明:博客经个人辛苦努力所得,如有转载会特别申明,博客不求技惊四座,但求与有缘人分享个人学习知识,生活学习提高之用,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律。
出处:http://www.cnblogs.com/xiaofeixiang
说明:博客经个人辛苦努力所得,如有转载会特别申明,博客不求技惊四座,但求与有缘人分享个人学习知识,生活学习提高之用,博客所有权归本人和博客园所有,如有转载请在显著位置给出博文链接和作者姓名,否则本人将付诸法律。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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的设计模式综述