对字符串进行基数排序的JAVA代码
时间: 2024-05-04 08:21:42 浏览: 17
以下是基数排序的JAVA代码,用于对字符串数组进行排序:
```java
public class RadixSort {
public static void radixSort(String[] arr, int stringLen) {
final int BUCKETS = 256;
ArrayList<String>[] buckets = new ArrayList[BUCKETS];
for (int i = 0; i < BUCKETS; i++)
buckets[i] = new ArrayList<>();
for (int pos = stringLen - 1; pos >= 0; pos--) {
for (String s : arr)
buckets[s.charAt(pos)].add(s);
int idx = 0;
for (ArrayList<String> thisBucket : buckets) {
for (String s : thisBucket)
arr[idx++] = s;
thisBucket.clear();
}
}
}
}
```
这个基数排序算法使用了桶排序的思想,将每个字符的 ASCII 码值作为桶的索引,对字符串进行排序。