概述
排序有内部排序和外部排序,内部排序是資料記錄在記憶體中進行排序,而外部排序是因排序的資料很大,一次不能容納全部的排序記錄,在排序過程中需要通路外存。
我們這裡說說八大排序就是内部排序。
算法思想:
1.将所有的數的個位數取出,按照個位數進行排序,構成一個序列。
2.将新構成的所有的數的十位數取出,按照十位數進行排序,構成一個序列。
public void sort(int[] array) {
//首先确定排序的趟數;
int max = array[];
for (int i = ; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
int time = ;
//判斷位數;
while (max > ) {
max /= ;
time++;
}
//建立10個隊列;
List<ArrayList> queue = new ArrayList<ArrayList>();
for (int i = ; i < ; i++) {
ArrayList<Integer> queue1 = new ArrayList<Integer>();
queue.add(queue1);
}
//進行time次配置設定和收集;
for (int i = ; i < time; i++) {
//配置設定數組元素;
for (int j = ; j < array.length; j++) {
//得到數字的第time+1位數;
int x = array[j] % (int) Math.pow(, i + ) / (int) Math.pow(, i);
ArrayList<Integer> queue2 = queue.get(x);
queue2.add(array[j]);
queue.set(x, queue2);
}
int count = ;//元素計數器;
//收集隊列元素;
for (int k = ; k < ; k++) {
while (queue.get(k).size() > ) {
ArrayList<Integer> queue3 = queue.get(k);
array[count] = queue3.get();
queue3.remove();
count++;
}
}
}
}
參考部落格:https://blog.csdn.net/wcqwcq123/article/details/53425738