Count Sort Java Code implementation

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.