Sorting algorithm Summary - sort count (C ++ to achieve)

Counting sequencing (counting sort)

  Ordering based on the comparison element is not counted, but an algorithm for determining the correct position of the element by using the array index. Counting sequencing data that the core values ​​of the input keys stored in the array into the additional space in the open. A linear sort time complexity of counting the time complexity of sorting algorithm O (n + k) (k is an integer in the range).

j is a brief description, the integral space is determined in a range of, establishing a greater length of the array, such as when the elements of the input integer n is between 0 and k, equal to the array to establish a length greater than k. Each value of the index position in the array represents an integer number of times corresponding to the array appears. According to the statistical results, directly through the array, the array index value of the output element, the element is the value of a few, it is output several times.

 Simple implementation:

class Solution
{
public:
    void coutSort(int* data, int length)
    {
        if (data == nullptr || length <= 0)
            return;

        // determine the maximum number of columns 
        int max = Data [ 0 ];
         for ( int I = . 1 ; I <length; ++ I)
        {
            if (data[i] > max)
                max = data[i];
        }

        // determine the length of the array and initialize statistical 
        int * = coutData new new  int [+ max . 1 ];
         for ( int I = 0 ; I <= max; ++ I)
            coutData [I] = 0 ;
         // iterate, count the number of times each appear 
        for ( int I = 0 ; I <length; ++ I)
             ++ coutData [Data [I]];
         // sorted array, a the number of emerged times, then several times in the data accumulated in the output 
        int index = 0 ;
         for ( int I = 0 ; I <= max; ++ I)
        {
            for (int j = 0; j < coutData[i]; ++j)
            {
                data[index++] = i;
            }
        }
    }
};

Optimized version (stable sort):

(1) identify the array to be sorted in the largest and smallest element

(2) count the number of times each array element value appearing i, i-th item stored in the array C

(3) all accumulated count (starting from the first element C, each in a front and a sum)

(4) Reverse filled destination array: i on each element of the new array C (i) of each element will be put C (i) subtracting 1

class Solution
{
public:
    int* coutSort(int* data, int length)
    {
        if (data == nullptr || length <= 0)
            return nullptr;

        // determine the maximum number of columns 
        int max = Data [ 0 ];
         int min = Data [ 0 ];
         for ( int I = . 1 ; I <length; ++ I)
        {
            if (data[i] > max)
                max = data[i];
            if (data[i] < min)
                min = data [i];
        }
        int D = max - min;
         // determine statistical array length and initialize 
        int * = coutData new new  int [D + . 1 ];
         for ( int I = 0 ; I <= D; ++ I)
            coutData [I] = 0 ;
         // iterate, count the number of times each appear 
        for ( int I = 0 ; I <length; ++ I)
                 ++ coutData [Data [I] - min];
         // count array do deformation, the latter element is equal to the sum of the foregoing elements 
        for ( int I = . 1 ; I <= D; ++ I)
            coutData [I] + = coutData [I - . 1 ];
     // reverse traversal original series to find the right position from the statistical array, output to the result array 
        int * sortedArray = new new  int [length];
         for ( int I = length - . 1 ; I> = 0 ; i-- )
        {
            sortedArray [coutData [data [i] - min] - 1 ] = data [i];         // find data [i] corresponding to coutData value, number value, indicates how much the original sorting, (because from 1, the Save 1) 
            coutData [Data [I] - min] -;         // then the value of the corresponding coutData minus 1, represents the next encounter this value, the original ordering is. 
        }
         Return sortedArray;
    }
};

Optimized version of the test:

void test()
{
    int data[] = { 95, 98, 97, 90, 98, 93, 92, 91 };
    int length = sizeof(data) / sizeof(int);

    Solution M;
    int* array = M.coutSort(data, length);

    for (int i = 0; i < length; ++i)
        cout << array[i] << "  ";
    cout << endl;
}

Limitations:

1. When the maximum and minimum values ​​of the array gap is too large, not suitable for counting sequencing. Will result in a waste of space and time complexity will also increase.

2. When the array elements is not an integer, such as minority, not suitable for counting sequencing.

Guess you like

Origin www.cnblogs.com/wyr-123-wky/p/11093919.html