网站 建设需,帷客分享 wordpress,大庆金思维科技网站开发,网站导航app低位优先的字符串排序相当于是对键索引计数方法的一个扩展#xff0c;主要用于处理固定长度字符串#xff0c;比如说手机号#xff0c;固定电话#xff0c;银行卡卡号#xff0c;字符串的长度为N#xff0c;从右向左开始进行每个键作为值开始遍历#xff0c;实现比较简单…低位优先的字符串排序相当于是对键索引计数方法的一个扩展主要用于处理固定长度字符串比如说手机号固定电话银行卡卡号字符串的长度为N从右向左开始进行每个键作为值开始遍历实现比较简单: -(void)lowSort:(NSMutableArray *)dataSource singleLength:(NSInteger)len
{NSInteger sourceCount[dataSource count];NSInteger R256;NSMutableArray *tempArr[[NSMutableArray alloc]initWithCapacity:1];for (NSInteger i0; isourceCount; i) {[tempArr addObject:[NSNull null]];}for (NSInteger dlen-1; d0; d--) {NSMutableArray *count[[NSMutableArray alloc]initWithCapacity:1];for (NSInteger i0; iR1; i) {[count addObject:[NSNumber numberWithInteger:0]];}//统计频率for (NSInteger i0; isourceCount; i) {NSString *str[dataSource objectAtIndex:i];NSInteger charValue[str characterAtIndex:d]-48;count[charValue1][NSNumber numberWithInteger:[count[charValue1] integerValue]1];}for (NSInteger j0; jR; j) {count[j1][NSNumber numberWithInteger:[count[j] integerValue][count[j1] integerValue]];}//将元素从上到下分类for (NSInteger m0; msourceCount; 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 i0; isourceCount; 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