Counting sorting algorithm

Source: Internet
Author: User

The count sort (counting sort) is a stable linear time sorting algorithm. The count sort uses an extra array of C, where the I element is the number of elements in the array A to be sorted with the value equal to I. The elements in a are then ranked in the correct position according to the array C.

This article address: http://www.cnblogs.com/archimedes/p/counting-sort-algorithm.html, reprint please indicate source address.

characteristics of the counting sort

When the input element is an integer of n 0 to K, its run time is Θ (n + K). The count sort is not a comparison sort, and the sort is faster than any comparison sort algorithm.

Because the length of the array C used to count depends on the range of data in the array to be sorted (equal to the difference between the maximum and minimum values of the array to be sorted plus 1), this makes the count sort for arrays with a large data range, which requires a lot of time and memory. For example, a count sort is the best algorithm for sorting numbers between 0 and 100, but it is not appropriate to sort names alphabetically. However, the count sort can be used in the cardinality sort algorithm to sort a large array of data ranges.

Generally understood, for example, there are 10 people of different ages, the statistics show that 8 people are younger than a, the age of a is ranked in the 9th place, this method can get everyone else's position, also lined up the order. Of course, the age of repetition requires special treatment (guaranteed stability), which is why the final target array is reversed, and the statistics of each number minus 1 of the reason.

The steps of the algorithm are as follows:

    1. Find the largest and smallest elements in the array to be sorted

    2. Count the number of occurrences of each element in the array of I , stored in the item i of array C

    3. Summation of all counts (starting with the first element in C , each item and the previous item are added)

    4. Reverse-Populate the target array: Place each element I in the C (i) of the new array, subtract C (i) by 1 for each element placed

For data 2 5 3 0 2 3 0 3 The procedure is performed as shown in:

Algorithm Implementation

The C code is as follows:

//completed on 2014.10.10 20:37//Language:c99////Copyright (C) Codingwu (mail: [email protected])//Blog Address:http://www.cnblogs.com/archimedes/#include <stdio.h>#include<stdlib.h> int* COUNTINGSORT (int *a, int n, int  k) {    intC[k +1], I, value, POS;  int *b = (int *) malloc (n * sizeof(int ));  for(i =0; I <= K; i++)//InitializeC[i] =0;  for(i =0; I < n; i++) C[a[i]]++;  for(i =1; I <= K; i++) C[i]= C[i] + c[i-1];  for(i = n-1; I >=0; i--) {Value=A[i]; POS=C[value]; B[pos-1] =value; C[value]--; }    returnb;}intMaxarr (int*a,intN//returns the largest element in an array{    intI, Max; Max= a[0]; I=1; int*p; P= A +1;  while(I++ <N) {if(Max < *p) Max= *p; P++; }    returnMax;}intMain () {intA[] = {2,5,3,0,2,3,0,3}; intN, Max; N=sizeof(a)/sizeof(a[0]); Max=Maxarr (A, N);  int *p = Countingsort (A, N, Max);  for(intK =0; K < N; k++) printf (  "%d  "  , p[k]); printf ("\ n");    Free (p); return 0;}

You can refer to the "C language pointer delivery" For questions about the code.

Counting sorting algorithm

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.