import java.math.* ;
import java.util.* ;
/**
*
*/
public class Test extends Base{
public static void main(String[] args) throws Exception{
int[] arr = getRandomArr() ;
println(arr);
countingSort(arr , arr.length );
println(arr);
println();
}
public static void countingSort(int[] arr, int n){
int maxValue = arr[0] ;
for (int i =0; i < n ; i++){
if (arr[i] > maxValue){
maxValue = arr[i] ;
}
}
int[] count = new int[maxValue+1] ;
int[] temp = new int[n] ;
for (int i = 0 ; i < n ; i++){
count[arr[i]]++ ; ;
}
for (int i = 1; i < count.length ; i++){
count[i] = count[i] + count[i - 1] ;
}
for (int i = n - 1; i >= 0; i-- ){
temp[count[arr[i]] - 1] = arr[i] ;
count[arr[i]]-- ;
}
for (int i = 0 ; i < n ; i++){
arr[i] = temp[i] ;
}
}
}
計數排序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.