Sorting algorithm (9)--distribution sorting--distribution Sort [1]--counting sort--counter Sort

Source: Internet
Author: User

1. Basic Ideas

Assuming that the number of elements in the sequence is less than the number of element A is n, the A is placed directly on the first n+1 position. Make the appropriate adjustments when there are several identical elements, because you cannot put all the elements in the same position. The count sort assumes that the input elements are integers between 0 and K.

2. Principle of Implementation

The position of this number can be determined by counting the number of other numbers in the group before sorting for a group of numbers. For example, the number to sort is 7 4 2 1 5 3 1 5; Then there are 7 numbers, all 7 should be in the eighth bit of the sorted sequence, the same as 3 in the fourth place, for the repeating number, 1 in 1 and 2 bits (for the moment the first 1 is smaller than the second 1), 5 and 1 are in 6 and 7 bits.

3. Code Examples

(1) Code:

Private Static int[] Countsort (int[] Array,intk) {//build two arrays: B arrays and C arrays    int[] C =New int[k + 1];//Constructing C Arrays    intlength = array.length, sum = 0;//gets the A array size used to construct the B array    int[] B =New int[Length];//construct B Array//count the number of elements in a, and deposit in the C array     for(inti = 0; i < length; i++) {C[array[i]]+ = 1; }    //accumulate the number of each element: Set the value of the C array     for(inti = 0; I < K + 1; i++) {sum+=C[i]; C[i]=sum; }    //iterate a array, construct B array     for(inti = length-1; I >= 0; i--) {B[c[array[i]]-1] = array[i];//Place the element in a in the position specified in array b after sortingC[array[i]]--;//this element in C-1 is convenient for storing the next element of the same size: the same elements are sequentially in the back row    }    returnB//returns the sorted array and completes the sort} Public Static voidMain (string[] args) {int[] A =New int[]{2, 5, 3, 0, 2, 3, 0, 3}; int[] B = Countsort (A, 5);  for(intnum:b) {System.out.print (num+" "); }}

(2) Results:

0 0 2 2 3 3 3 5

4. Algorithm analysis

The Count sort is a very fast stability-strong sorting method, with time complexity O (n+k), where n is the number of numbers to sort, and K is the group's large value for the number to sort. Counting sorting is very fast for a certain amount of integers, generally faster than other sorting algorithms. However, the limit of the counting sort is large and is limited to the ordering of integers. Counting sorting is the use of spatial complexity to get a quick sorting method with an O (k) space, and the same k as the maximum value to sort.

Sorting algorithm (9)--distribution sorting--distribution Sort [1]--counting sort--counter Sort

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.