Four sorting algorithms (bucket sort)

First, the introduction of bucket sort algorithm.

  We have previously said that the counting sort algorithm.

  This time, if we have such a sequence of data to be sorted:

int  x[14]={-10, 2, 3, 7, 20, 23, 25, 40, 41, 43,60, 80, 90, 100};

  If the count in accordance with the scope of our sorting algorithm, then the data to be sorted is: -10 to 100

  In order to achieve this we have to traverse the data set, this time the need for additional open up a size (100 (-10) + 1) space, space complexity of this time far more than we expected, which is the cause a waste of space.

  So we can say, counting sort there is a drawback: the difference between the data inside the max and min in the number to be sort of data is not too large compared to otherwise wasted space

  Bucket sort algorithm for counting the equivalent of doing a sorting algorithm to do an upgrade.

  Just count sorting algorithm for each count is a sort numbers, and bucket sorting algorithm will be relaxed this range, into a range.

Second, the process of bucket sort algorithm.

  1, for example, for the above to be a sort of data, we need to divide it into a series of ranges from min to min, which is equivalent to one with a "bucket."

  2, data corresponding to the range corresponding to the range of the tub into the inside.

  3, and then using the data for each of a range of other sort of sorting algorithms, such that internal data of each bucket is ordered, has been ordered to move to the dataset.

 

  There are two steps above the more critical:

  The first one is for the bucket of range partitioning, which requires a mapping rule, if we put a small bucket division, this time almost every data is a barrel; a, a waste of space. If the division is too large, all the data into a bucket inside, the equivalent of no division.

  The second is the selection sort algorithm inside each bucket, the overall stability of the final impact of this bucket sorting algorithm.

Third, examples

  Above for a data to be sorted, this time we already know max = 100, min = -10.

  Then we design a mapping rule, a rule such as design, making the -10 to 50 into a bucket, 50-100 divided into a bucket.

  We can design such a rule:

  ƒ (x) = x / 30 - min / 30

  So that we can put the above array divided into four groups:

According to the results grouping data
0 -10, 2, 3, 7
1 20, 23 ,25 ,40, 41
2 60
3 80,  90, 100

  And then be sorted according to each packet.

Fourth, the algorithm

/ * Algorithm: Sort the tub C ++ Version * / 
 
#include <the iostream> 
#include <Vector> 
#include <algorithm> 
#include <math.h>
 the using  namespace STD;
  // this effect algorithm is located in the array A for A [l] to a [h] is divided into the elements to the sorting size buckets 
void bksort ( a float a [], int L, int H, int size) { 
    Vector < a float > B [size]; // has a size data size allocated buckets 
    int min = A [L];
     for ( int I = L; I <= H; I ++) { // conveniently obtained minimum value 
      if(A [I] < min) 
           min = A [I]; 
    } 
    for ( int I = L; I <= H; I ++ ) {
         int BI = (A [I] -min) / size; // element A [ i] bucket number where the mapping rule modification 
        B [BI] .push_back (a [i]); // the elements a [i] is pressed into the tub 
    }
     for ( int I = 0 ; I <size; I ++ ) 
        Sort (B [I] .begin (), B [I] .end ()); // barrel sort 
    int IDX = L; // pointer to the array a subscript 
    for ( int I = 0 ; I <size; I ++ ) { // traverse barrel 
        for (int J = 0 ; J <B [I] .size (); J ++) { // iterate tub element 
            A [++ IDX] = B [I] [J]; 
        } 
    } 
} 
 
int main () {
     a float A [] {= 0.78 , 0.17 , 0.39 , 0.26 , 0.72 , 0.94 , 0.21 , 0.12 , 0.23 , 0.68 }; 
    bksort (A, 2 , . 9 , . 3 );
     for ( int I = 0 ; I < 10;i++)
        cout<<A[i]<<" ";
}

 

Fifth, the complexity analysis

  Mainly it depends on the complexity of sorting algorithms.

  If the sorting algorithm is quick-sort, you can reach Θ (n) (results bigwigs)

Sixth, reference links:

  Computational complexity look at this: https: //blog.csdn.net/bqw18744018044/article/details/81738883

  Process algorithm described in this reference are: https: //www.jianshu.com/p/204ed43aec0c

 

  

Guess you like

Origin www.cnblogs.com/fantianliang/p/11881381.html