一.算法簡介
通過統計元素出現的次數進而排序,需要一個輔助數組,大小是最大元素值(想想計數的過程),為了更好的理解計數排序,我們先來想象一下如果一個數組里所有元素都是非負整數(數組下標是整數),而且都在0-max(由於內存的原因,這個值要小一些)以內,那對於數組里每個元素來說,如果我能知道數組里有多少項小於或等於該元素,就能准確地給出該元素在排序后的數組的位置。
局限性:通過上面的描述可以看出需要整數(若有負數,則正負數分別排序),而且最大值要在能開數組范圍內。
算法是穩定的,算法第五步從后往前保證了穩定性,希望讀者細細體會……
二.算法描述
- 求得元素最大值max(看算法實現過程,體會這個方法需要獨立,就是說max需要事先確定)
- 開輔助數組c[max]和res[原數組大小]並全部初始化為0
- 計數:統計元素出現次數並把次數放在c數組的以該元素為下標的位置
- 針對c數組從下標1開始,前一項加上后一項並存入后一項獲得數組里有多少項小於或等於該元素
- 對原數組中的每一個元素temp存入res[c[temp]-1](減一是因為下標從0開始)並更新c[temp]--(不必擔心小於0,因為一旦等於0就不會再減小了,因為原數組沒該元素了)
三.算法的Java實現
public class CountSort { public static void main(String[] args) { int[] a = { 3, 1, 6, 0, 3, 0, 1, 5, 3, 6 }; int max = getMax(a); arrDisplay(a, "Before mySort:"); a = mySort(a, max); arrDisplay(a, "After mySort:"); } public static int[] mySort(int[] a, int max) { int[] res = new int[a.length]; int[] c = new int[max + 1]; for (int i = 0; i < res.length; i++) { res[i] = 0; } for (int i = 0; i < c.length; i++) { c[i] = 0; } int temp = 0; /*統計元素出現次數並把次數放在c數組的以該元素為下標的位置 不可以在這個遍歷過程中取最大值,就是說getMax不可合並,因為需要事先c的大小; 能否直接開為整形最大值,這樣不可能,超內存*/ for (int i = 0; i < a.length; i++) { temp = a[i]; c[temp] = c[temp] + 1; } for (int i = 1; i < c.length; i++) { c[i] = c[i] + c[i - 1]; } //必須從后往前,這樣保證了穩定性 for (int i = a.length - 1; i >= 0; i--) { temp = a[i]; res[c[temp] - 1] = temp; //不必擔心小於0,因為一旦等於0就不會再減小了,因為原數組沒該元素了 c[temp] = c[temp] - 1; } return res; } public static int getMax(int[] a) { int max = a[0]; for (int i = 1; i < a.length; i++) { if (a[i] > max) max = a[i]; } return max; } public static void arrDisplay(int[] a, String str) { System.out.println(str); for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } }