Count sort (C language version)

Source: Internet
Author: User

First of all, the idea of counting sorting:

The Count sort assumes that all the elements to be sorted are integers between 0 and K; The count sort uses an extra array of Countarray, where the i element is the number of elements in the array to be sorted by the value equal to i . The elements in the array are then ranked in the correct position based on the Countarray of the arrays.

Algorithm of Steps as follows:

    1. Find the largest and smallest elements in the array to be sorted
    2. Count the number of occurrences of the element in the array for each value i , stored in the array countarray
    3. Add to all counts (starting with the first element in Countarray , each item and the previous one)
    4. Reverse-Populate the target array: Place each element I in the countarray[i] item of the new array, and each element will be Countarray[i] minus 1

Stability and complexity:

The counting sort is a stable sorting algorithm, the draw time complexity, the optimal time complexity and the worst time complexity are O (n+k), the space complexity is O (n+k), where n is the number of elements to be ordered, K is the range of the elements to be ordered (0~K).

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include < assert.h> #define RANDMAX 10000void getrandarray (int array[], int size), void countsort (int array[], int size); void Isso    rted (int array[], int size); int main (int argc, char const *argv[]) {int size = 0;    scanf ("%d", &size);    ASSERT (Size > 0);    int *array = (int *) calloc (size, sizeof (int));    Getrandarray (array, size);    Clock_t begin;    clock_t end;    begin = Clock ();    Countsort (array, size);    end = Clock ();        The time it takes to print the sort, under Linux in units for Ms printf ("%ld\n", (End-begin)/1000);    issorted (array, size);    Free (array); return 0;}    Fills an array with pseudo-random numbers arrayvoid getrandarray (int array[], int size) {assert (Array! = NULL && size > 0);    Srand ((unsigned) time (NULL));    int i = 0;    for (i = 0; i < size; ++i) {//Generate pseudo-random numbers within Randmax array[i] = rand ()% Randmax; }}//from small to large sort void countsort (int array[], int size) {assert (Array! = NULL && size > 0); A count array that counts the number of occurrences of each different number in an array of arrays//due to the number of arrays in the array belonging to 0 ...    RANDMAX-1, so the size of the countarray is sufficient to accommodate the value of Randmax int *countarray = (int *) calloc (Randmax, sizeof (int));    Used to hold an ordered sequence of int *sortedarray = (int *) calloc (size, sizeof (int));    Counts the number of occurrences of each different number in an array of arrays, after the end of the loop Countarray[i] indicates the number of occurrences of the value I in the array int index = 0;    for (index = 0; index < size; ++index) {countarray[array[index]]++; }//The number of numbers smaller than index, Countarray[i] indicates the number of elements less than or equal to the value I in the array for (index = 1; index < Randmax; ++index) {C    Ountarray[index] + = countarray[index-1]; } for (index = size-1; index >= 0;--index) {//Because the array starts with a subscript of 0, so subtract one sortedarray[countarray[array[index        ]]-1] = Array[index];    This is reduced to ensure that when there are multiple elements with a value of array[index], the element that appears after the value of Array[index]//is placed behind, that is, to ensure the stability of the order--countarray[array[index]];    } memcpy (Array, sortedarray, size * sizeof (int));    Free (Sortedarray); Free (countarray);} Determines whether an array of arrays is already an ordered void IssortEd (int array[], int size) {assert (Array! = NULL && size > 0);    int unsorted = 0;    int i = 0;            for (i = 1; i < size; ++i) {if (Array[i] < array[i-1]) {unsorted = 1;        Break    }} if (unsorted) {printf ("The array is unsorted!\n");    } else {printf ("The array is sorted!\n"); }}
Counting sorting is a sort algorithm that is not compared, and is said to be sorted faster than any comparison sort algorithm (I have not yet verified it, but it takes 606 milliseconds to sort 100 million 10000 or less, whereas the C language qsort function is 10802 milliseconds, which shows) Because the counting sort needs an array of counts and an array to hold the ordered series, the order of the counts has a higher requirement for memory.

Count sort (C language version)

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.