标签:style [] int util number stat str count port
一、CountSort
package algorithm.sort.notcompare; public class CountSort{ public static void main(String[]args){ int arrA[]={100,93,97}; int arrB[]=countSort(arrA); for(int i:arrB) System.out.print(i+" "); } public static int[] countSort(int[] arr){ int arrb[] = new int[arr.length]; int max = arr[0], min = arr[0]; for(int i:arr){ //Find the max number and min number in arr. if(i>max) max=i; if(i<min) min=i; } int k=max-min+1; int c[]=new int[k]; for(int i=0; i<arr.length; ++i) c[arr[i]-min]+=1; for(int i=1; i<c.length; ++i) c[i]=c[i]+c[i-1]; for(int i=arr.length-1; i>=0; --i) arrb[--c[arr[i]-min]]=arr[i]; return arrb; } }
二、RadixSort
基数排序
时间复杂度:O (nlog(r)m),其中r为所采取的基数,而m为堆数
稳定性:稳定
package algorithm.sort.notcompare; import java.util.Arrays; public class RadixSort { public static void main(String[] args) { int[] arrayA = new int[] { 111, 213, 134, 65, 77, 78, 23, 43, 321 }; radixSort (arrayA, 0, 2, arrayA.length); System.out.println (Arrays.toString (arrayA)); } private static void radixSort ( int[] array, int from, int radix, int d ){ int len = array.length; int[] temporary = new int[len]; int[] count = new int[radix]; int R = 1; for ( int i = 0; i < d; i++ ){ System.arraycopy (array, from, temporary, 0, len); Arrays.fill (count, 0); for ( int k = 0; k < len; k++ ){ int subkey = ( temporary[k] / R ) % radix; count[subkey]++; } // for ( int j = 1; j < radix; j++ ) // 从小到大 // count[j] = count[j] + count[j - 1]; for ( int j = 1; j < radix; j++ ) //从大到小 count[j - 1] = count[j] + count[j - 1]; for ( int m = len - 1; m >= 0; m-- ){ int subkey = ( temporary[m] / R ) % radix; --count[subkey]; array[from + count[subkey]] = temporary[m]; } R *= radix; } } }
标签:style [] int util number stat str count port
原文地址:http://www.cnblogs.com/chen-yonghai/p/6367321.html