C ++ implementation of radix sort

Bucket sort  

  Speaking before the radix sort, will speak about bucket sort, both have greater relevance.

  A bucket sort ordering, for example, with N an integer, the N integer ranging from 1 ~ M (0 ~ M-1 too), you can create an array COUNT, size is M, all the elements of the first is initialized to 0, each element is called a barrel, the array has M barrel. Then be clocked into the ordered set of numbers, it is assumed that number A read, the array element corresponding to COUNT [A] plus the value of 1, i.e., the number of fall of the bucket recorded data bucket. After reading, the output sequence of the non-0 bucket index (count [index] is how many times the output), the data output of the data is sorted.

  For example, the six 9,3,1,1,0,5 bucket sort data, the establishment of an array, since a maximum of 9 data, preferably the M = 10 (array size of 10) to establish array count [10], the data is read, the first read 9, the count [9] = 1, 3 is read, the count [3] = 1, ...... count [1] = 2, ...... and so on, after reading, count [10] = {1,2,0,1,0,1,0,0,0,1}, some elements in the array is 0, the data corresponding to the number output is not 0 , the element is how many times the output, the output is 0,1,1,3,5,9, that is sorted correctly.

Radix Sort

  Radix sort is a bucket sort of promotion, when the data is too large bucket would require too much, so the use of multiple passes of the bucket sort. How a multi-pass method, the method is the low priority bucket sort (also high prioritized), assuming a series of data

73, 22, 93, 43, 55, 14, 28, 65, 39, 81
The first digit of the numerical value, the numerical visited by assigning them to buckets numbered 0 to 9:
0
1   81
2   22
3   73 93 43
4   14
5   55 65
6
7
8   28
9   39

The second step

Next, the value of these buckets are re-connected in series, become the following series:
81, 22, 73, 93, 43, 14, 55, 65, 28, 39
Then make another assignment, this time is allocated according to the tens:
0
1   14
2   22 28
3   39
4   43
5   55
6   65
7   73
8   81
9   93

third step

Next, the value of these buckets are re-connected in series, become the following series:
14, 22, 28, 39, 43, 55, 65, 73, 81, 93
This time the whole series has been sorted; if the object has ordered more than three digits, then continued until the above action until the maximum number of digits.
 
C ++ code to achieve
#include " pch.h " 
#include <the iostream>
 the using  namespace STD; 

int maxbit ( int Data [], int n-) // find the maximum number of bits of a set of data 
{
     int COUNT = . 1 ;
     int D = 0 ;
     for ( int I = 0 ; I <n-; I ++ ) 
    { 
        int TEMP = Data [I];
         the while (TEMP / 10 ! = 0 ) 
        { 
            COUNT ++; 
            TEMP = TEMP / 10 ; 
        } 
        IF (D < COUNT) 
            D = COUNT; 
        COUNT = . 1 ; 
    } 
    return D; 
} 
void radixsort ( int Data [], int n-) // for radix sort 
{
     int D = maxbit (Data , n-);     // get the maximum number of bits, i.e., d times for bucket sort 
    int * tmp = new new  int [n-];         // space tmp is used to store the data points after each bucket sort 
    int * COUNT = new new  int[ 10 ];     // COUNT to point to the number of digits of each bucket falls after storage bucket sort 
    
    int the radix = . 1 ;
     int J, K;
     for ( int I = . 1 ; I <= D; I ++ ) 
    { 
        for ( int I = 0 ; I < 10 ; I ++ ) 
            COUNT [I] = 0 ;             // every time the respective assigned data before the tub is initialized to 0 
        for ( int I = 0 ; I <n-; I ++) // for Scheduling in d 
        { 
            K = (Data [I] / the radix)%10 ; // find a bit (ones, tens, hundreds ...) is what the individual data 
            COUNT [k] ++; // corresponding bucket data plus 1, that is, how many counts put the barrel 
        }
         for (J = . 1 ; J < 10 ; J ++) // object is to solve how the data sequentially into the data in the barrel space tmp referred 
            COUNT [J] COUNT = [J - . 1 ] + COUNT [J];
         for ( int I = N- . 1 ; I> = 0 ; i--) // the data inside the data sorted into the order indicated tmp space, note that data to start playback from behind 
        { 
            K = (data [ I] / the radix)% 10 ; 
            tmp [COUNT [K] - . 1 ] =Data [I]; 
            COUNT [K] - ; 
        } 
        for ( int I = 0 ; I <n-; I ++) // the data to each bucket sort Data 
            Data [I] = tmp [I]; 
        the radix = * 10 ; 
    } 
    Delete [] tmp; // release the memory 
    Delete [] COUNT; // release the memory 
} 

int main () 
{ 
    int Data [ 10 ] = { 73 is , 22 is , 93 , 43 is , 55 , 14, 28 , 65 , 39 , 81 }; 
    radixsort (Data, 10 );
     for ( int I = 0 ; I < 10 ; I ++) // the output data sorted array 
        COUT << Data [I] << "  " ; 
    System ( " PAUSE " );
     return  0 ; 
}

operation result:

 

 

 

Guess you like

Origin www.cnblogs.com/cs0915/p/12119023.html