Introduction to algorithms eighth __ implementing counting sort

Source: Internet
Author: User
Tags sorts

Count sort: You don't have to compare the order of sorts, for example. Count sort, cardinal sort, bucket sort in this chapter

Sort By comparison: the order in which the sorting is to be performed. For example, heap ordering in this chapter, high-speed sorting (essentially insert sort), insert sort


Code Listing: Count sort __ Perfect deduction of subscript function

public class Count_sort {//Receive ordered array private int[] a;//sorted array private int[] b;//array for count private int[] c;//initialize public count_s ORT (int[] A) {this. A = A; B = new Int[a.length]; C = new Int[innittemp ()];} Initializes the size of the temporary array public int innittemp () {int bigest = a[0];for (int i = 1; i < a.length; i++) {if (A[i] > bigest) {biges t = A[i];}} return bigest+1;} The count sorts public void sort () {//maps the corresponding element in I to C for (int i = 0; i < a.length; i++) {int value = A[i]; C[value] = C[value] + 1;//use C to count the number of}show (c);//To replace the value meaning of C. How many numbers are smaller than the current subscript for (int i = 1; i < c.length; i++) {C[i] + + c[i-1];} Show (C);//finally come to a[i] element the correct insertion position for (int i = a.length-1; I >= 0; i--) {int value = a[i];//Why 1? Think, assuming that the number is 2 than 5, that 5 should be found in the array of 2 positions, then the 2nd position, corresponding to A[c[value]-1]b[c[value] 1] = value;//This is very important! For example, a[5]==a[2] Such a situation, a[5] inserted the correct position, then a[2] Insert the order of how to calculate it? Just let it follow the a[5]!

C[value]-= 1;} Show (B);} public void Show (int[] X) {for (int i = 0; i < x.length; i++) {int j = x[i]; System.out.print (j + "");} System.out.println (); System.out.println ("--------------------------------------------------------------");} public static void Main (string[] args) {int []a={1,2,3,3,2,1,1,5,1,3,4,5}; Count_sort sort=new Count_sort (A); Sort.sort ();}}


Results of the output:

0 4 2 3 1 2
--------------------------------------------------------------
0 4 6 9 10 12
--------------------------------------------------------------
1 1 1 1 2 2 3 3 3 4 5 5
--------------------------------------------------------------

Introduction to algorithms eighth __ implementing counting sort

Related Article

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.