Conclusion: Because the counting sort is not based on the comparison sort, the time complexity can break through O (NLGN), the count sort time complexity is O (n), the extra Space complexity is O (n);
The Java implementation code is as follows:
1 PackageCom.cmbc.test1;2 3 Public classcountsorting {4 5 Public Static voidCountsort (int[] arr) {6 if(arr==NULL|| Arr.length<2){7 return;8 }9 intMax =Integer.min_value;Ten for(inti = 0; i<arr.length;i++){ OneMax =Math.max (Max, arr[i]); A } - int[] help =New int[Max+1]; - the for(inti = 0;i<arr.length;i++){ -help[arr[i]]++; - } - + inti = 0; - for(intj = 0; J < Help.length; J + +) { + while(help[j]--> 0) { Aarr[i++] =J; at } - } - - } - - Public Static voidPrintArray (int[] arr) { in if(arr = =NULL) { - return; to } + for(inti = 0; i < arr.length; i++) { -System.out.print (Arr[i] + ""); the } * System.out.println (); $ }Panax Notoginseng - Public Static voidMain (string[] args) { the int[] arr = {1,7,3,9,2,0,3,6,9}; + PrintArray (arr); A Countsort (arr); the PrintArray (arr); + } - $ $}
Count Sort Java Code implementation