将某组数据进行分类排列,排列的顺序依次为:个位数、十位数、百位数。要求:重新排列的数据在同组中不能破坏其原来的相对位置关系 比如原来的数组顺序为:3 221 12 6 10 314 65 29 9 30 81 5 119 20 57 44 99 那么新的排列顺序就应该为:3 6 9 5 12 10 65 29 30 81 20 57 44 99 221 314 119 小弟新学java以及数据结构,如果有好心的大神帮忙最好贴出代码,方便学习,谢过各位
先除以100,结果大于0则是百位数,否则除以10,结果大于0是十位数,否则就是个位数,当然注意数字类型是要int。实现的方法还有很多可以自己去想一个。
实现过程:331 454 230 34 343 45 59 453 345 231 9
第一步:将最后一位按0——9的顺序依次放入bucket[i]中(o<i<9)
bucket[0] bucket[1] bucket[2] bucket[3] bucket[4] ...... bucket[9]
230 331 231 343 453 454 45 345 59 9
bucket[]: 230 331 231 343 453 454 45 345 59 9
第二步:将倒数第二数放入桶中
bucket[0] bucket[1] bucket[2] .... bucket[9]
9 230 331 231 34 343 45 345 453 454 59
将桶删除后: 9 230 331 231 34 343 45 345 453 454 59
第三步: 将倒数第三个数放入桶中:
bucket[0] bucket[1] bucket[2] bucket[3] bucket[4] ...... bucket[9]
9 34 45 59 230 231 331 343 345 453
将桶删除后:9 34 45 59 230 231 331 343 345 453
现在进行比较这些数字就有序了;
代码:public bucketSort(E [] list)
{
E[] buckets =(E[]) new java.util.ArrayList[N];
for(int i = 0;i<list.length;i++)
{
int key = list[i].getkey();
if(buckets[i] != null)
{
for(int j = 0;j<buckets[i].size();j++)
list[k++] = buckets[i].get(j);}
}
}
package com.tur.demo;import java.util.*;public class Hello {
/**
* 计算一个整数的位数,如5的位数是1,231的位数3
* @param n 是一个整数
* @return 返回一个数的位数
*/
public static int bitCountOfNumber(int n) {
int bitCount = 0; do {
n /= 10;
++bitCount;
} while (n != 0); return bitCount;
} /**
* 使用插入排序的思想进行排序
* @param ns
*/
public static void specialSortUsingInsertingIdea(int[] ns) {
System.out.println("Before sort: " + Arrays.toString(ns)); for (int i = 1; i < ns.length; ++i) {
int temp = ns[i]; for (int j = i - 1; j >= 0; --j) {
if (bitCountOfNumber(ns[j]) > bitCountOfNumber(temp)) {
ns[j + 1] = ns[j];
} else {
ns[j + 1] = temp;
break;
}
}
} System.out.println("After sort: " + Arrays.toString(ns));
} /**
* 使用分类的思想进行排序
* @param ns
*/
public static void specialSortUsingClassification(int[] ns) {
System.out.println("Before classification: " + Arrays.toString(ns)); // key是整数的位数,values是对应位数的整数列表
Map<Integer, List<Integer>> kinds = new TreeMap<Integer, List<Integer>>(); for (int n : ns) {
int bits = bitCountOfNumber(n);
List<Integer> list = kinds.get(bits); // 如果对应位数整数的列表不存在,则创建
if (list == null) {
list = new LinkedList<Integer>();
kinds.put(bits, list);
} list.add(n);
} // 把分好类的整数合并在一个数组中
int index = 0;
for (List<Integer> list : kinds.values()) {
for (int n : list) {
ns[index++] = n;
}
} System.out.println("After classification: " + Arrays.toString(ns));
} public static void main(String[] args) {
int[] ns1 = {3, 221, 12, 6, 10, 314, 65, 29, 9, 30, 81, 5, 119, 20, 57, 44, 99};
int[] ns2 = {3, 221, 12, 6, 10, 314, 65, 29, 9, 30, 81, 5, 119, 20, 57, 44, 99}; specialSortUsingInsertingIdea(ns1);
specialSortUsingClassification(ns2);
}
}Before sort: [3, 221, 12, 6, 10, 314, 65, 29, 9, 30, 81, 5, 119, 20, 57, 44, 99]
After sort: [3, 6, 9, 5, 12, 10, 65, 29, 30, 81, 20, 57, 44, 99, 221, 314, 119]
Before classification: [3, 221, 12, 6, 10, 314, 65, 29, 9, 30, 81, 5, 119, 20, 57, 44, 99]
After classification: [3, 6, 9, 5, 12, 10, 65, 29, 30, 81, 20, 57, 44, 99, 221, 314, 119]