日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

算法-低位优先的字符串排序

發布時間:2023/12/9 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 算法-低位优先的字符串排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

低位優先的字符串排序相當于是對鍵索引計數方法的一個擴展,主要用于處理固定長度字符串,比如說手機號,固定電話,銀行卡卡號,字符串的長度為N,從右向左開始進行每個鍵作為值開始遍歷,實現比較簡單:

-(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];}} }

 代碼測試:

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");

效果如下:

轉載于:https://www.cnblogs.com/xiaofeixiang/p/4855578.html

總結

以上是生活随笔為你收集整理的算法-低位优先的字符串排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。