"Introduction to Algorithms-Learning notes" in the order of linear time growth--counting sort

Source: Internet
Author: User

A counting sort is a sort algorithm that can achieve a linear time θ (n) to run time. One of the fastest algorithms in the sorting algorithm, of course, has a strong premise. Let's start with the technical sort (counting sort).

Algorithmic thinking

The count sort assumes that each of the N input elements is an integer between 0 and K, where K is an integer. This allows you to use an array of c[0..k] to record the number of elements in the array you want to sort. When K=o (n), the run time of the count sort is θ (n).

Note: In relation to C[0..K], the key value pair is described, the element to be sorted is the key, the number of the same element is the value. Example: arrays to be sorted <2,3, 6,4, 1, 1, 3, 5, 7> altogether 9 elements, cut each in 0 to 7, so set the array c[0..7], have c[2] = 1, c[3] = 2, because the array only 1 2, two 3

Counting sort basic idea: for each INPUT element x, determine the number of elements less than x, with this information, you can place x in its correct position (when there are elements in the same time, the scheme will be slightly modified).

Pseudo-code implementation of the algorithm

The count sort will have three arrays:
A[1..N] Array to sort
B[1..N] Sorting results storing arrays
C[0..K] Temporary storage area

Count Sort process:
1. Set all elements in C[0..K] to 0
2. Iterate through the array A[1..N], record the number of elements inside the array C
3. Use array c to record the number of elements less than or equal to each element
4. The number of elements less than or equal to each element is the position of this element in the new array
5. Sorting complete

Pseudo-code implementations in the book:

C + + implementation of the algorithm
#include <iostream>using namespace STD;Const intCOUNT = the;//Count sortvoidCounting_sort (intAintAintk);intMain () {intA[count] = {2,3,1,3,4,6,9,6,5,7,1,0,Ten,2,4};The //maximum value is 10, so k = ten    intK =Ten;intB[count];//Sort to place the sorted result in array bCounting_sort (A, B, k);//Output ordered value     for(inti =0; i < COUNT; i++) {cout<< B[i] <<" "; }cout<< Endl; GetChar ();return 0;}//Count sortvoidCounting_sort (intAint* B,intK) {//Declare array C and place all values in C to 0    int*c =New int[k +1]; for(inti =0; I < K +1; i++) {C[i] =0; }///c[a[i]], the number of elements with the same value as the a[i] element     for(inti =0; i < COUNT; i++) {C[a[i]] = C[a[i]] +1; }//C[i] The number of elements that are less than or equal to I     for(inti =1; I < K +1; i++) {C[i] = C[i] + c[i-1]; }//c[i] is the position of this element in the sorted array     for(inti = count-1; I >=0; i--) {b[c[a[i]]-1] = A[i];//This is c[a[i]]-1 because C + + array starts from 0 and pseudocode starts with 1C[a[i]]--;//Consider two elements equal, and the next element equal to the current element is placed in front                             //And since this is a backward-forward traversal, the relative position of the equal number has not changed, so this is a stable sorting algorithm} Free(c);}
    • Although there are four loops inside, the time complexity of each loop is O (n), so the total time complexity is O (n)
    • This sort algorithm does not compare the size of two elements, so it breaks the lower limit of the comparison sort O (NLGN)
    • In addition, the comment in the code also has a write, the relative position of the equal number is not changed during the sorting process, so this is a stable sorting algorithm

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Introduction to Algorithms-Learning notes-sorting with linear time growth--Sort by count

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.