[Turn] Count and sort

Reprinted from: http://blog.csdn.net/pi9nc/article/details/12220851

 

1) Introduction to the algorithm

    Counting sort is a stable sorting algorithm. Counting sort uses an additional array C, where the ith element is the number of elements in the array A to be sorted whose value is equal to i. Then according to the array C to arrange the elements in A to the correct position. It can only sort integers.

 

2) Algorithm description and analysis

        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 whose value is i in the array, and store it in the i-th item of array C

         3. Accumulate all counts (starting from the first element in C, each item is added to the previous item, which is actually converted to an address according to the number)

         4. Reverse filling of the target array: put each element i in the C(i) item of the new array, and subtract 1 from C(i) for each element placed

 

        When the input elements are n integers between 0 and k, its running time is O(n + k). Counting sort is not a comparison sort, and it sorts faster than any comparison sort algorithm.

        Since the length of the array C used for counting depends on the range of data in the array to be sorted (equal to the difference between the maximum value and the minimum value of the array to be sorted plus 1), this makes counting sorting for arrays with a large data range, requiring a large number of time and memory. For example: Counting sort is the best algorithm for sorting numbers between 0 and 100, but it is not suitable for sorting people's names alphabetically. However, counting sort can use the algorithm used in radix sort to sort arrays with large data ranges.

 

3) Algorithm diagram

        We use counting sort to sort an out-of-order integer array.

        First create a temporary array (the length is the maximum interval of the input data), for each integer k of the input array, we put "1" in the kth position of the temporary array. As shown below



         In the above figure, the first line represents the input data, the second line represents the created temporary data, the subscript of the temporary array represents a certain value of the input data, and the value of the temporary array represents the number of a certain value in the input data.

         If there are duplicate values ​​in the input data, then we increase the corresponding value of the temporary array (for example, there are 3 5 in the above figure, so the value of the array marked with 5 is 3). After "initializing" the temporary array, we have a sorted input data.


 

        We traverse this array in order, interpret the subscript as data, and the value of this position represents the number of repetitions of the data, remember to get a sorted array.

4) Algorithm code

#include <stdlib.h>  
#include <string.h>  
#include <stdio.h>  
/**************************************************************
 Function: count and sort.
 Parameters: data : the array to sort
        length : the number of elements in the array
        maxValue : The maximum value of the array of elements in the array +1 (this needs +1 because the array index starts from 0)
 Return value: 0 for success; -1 for failure.        
 *************************************************************/  
int ctsort(int *data, int length, int maxValue)  
{  
    int * counts = NULL, /* array of counts */  
        * temp = NULL; /* save the sorted array */  
    int i = 0;  
    /*Apply for array space*/  
    if ((counts = (int *) malloc( maxValue * sizeof(int))) == NULL)  
        return -1;  
    if ((temp = (int *) malloc( length * sizeof(int))) == NULL)  
        return -1;  
    /*Initialize count array*/  
    for (i = 0; i < maxValue; i ++)  
        counts[i] = 0;  
    /* Elements appearing in the array, and the number of occurrence records */  
    for(i = 0; i < length; i++)  
        counts[data[i]] += 1;  
    /*In the adjustment element count, adding the previous number is actually converting the previously stored number into an address*/
    for (i = 1; i < maxValue; i++)  
        counts[i] += counts[i - 1];
    /*Use the record value in the count array to sort, and save the temp after sorting*/  
    for (i = length -1; i >= 0; i --){  
        temp[counts[data[i]] - 1] = data[i]; /*Start from the last element to find where it should be stored*/
        printf("temp[%d]:%d\n",counts[data[i]] - 1,temp[counts[data[i]] - 1]); //Check out the temp array
        counts[data[i]] -= 1;  
    }  
 
    memcpy(data,temp,length * sizeof(int));  
    free(counts);  
    free(temp);  
    return 0;  
}  
intmain()  
{  
    int a[8] = {2,0,2,1,4,6,7,4};  
    int max = a[0],  
        i = 0;  
    /* get the value in the array */  
    for ( i = 1; i < 8; i++){  
        if (a[i] > max)  
            max = a[i];  
    }  
    ctsort(a,8,max+1);//Because the array index starts from 0, so add 1  
    for (i = 0;i < 8;i ++)  
        printf("%d\n",a[i]);  
}

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327043215&siteId=291194637