Sorting algorithm - bucket sort

Foreword

Bucket sort (Bucket sort) or so-called box sort is a sorting algorithm, the principle is to be assigned to a limited number of array bucket. Each individual bucket and then sorted (it is possible to re-use or other sorting algorithm recursively continue using bucket sort sorting), and finally turn to record individual bucket list to remember the ordered sequence. Bucket sort is a summarized result pigeonhole sort of. When the value in the array to be sorted is evenly distributed, bucket sort using a linear time (Θ (n)). But not compare sorting bucket sort, he is not affected by O (n log n) lower bound.

The basic idea 

Thinking bucket sort of divide and conquer nearly complete thought.
Bucket sort is assumed to be sorted in one set of independent uniformly distributed in a range, and this range is divided into several sub-ranges (tub).
Then based on a mapping function F, the key k to be sorted column is mapped to the i th bucket (i.e., subscript i's bucket array B), then the key k as an element to B [i] in the (per buckets B [i] are a group of size N / M sequences).
The combined data is then ordered in each tub up: comparing Sorting (fast discharge may be used) for all elements in each bucket B [i] in. Followed by enumeration output B [0] ... .B entirety [M] that is in an ordered sequence.
NOTE: The mapping function is generally f = array [i] / k; k ^ 2 = n; n is the number of all the elements
In order to make more efficient bucket sort, we need to do two things:
In the case of sufficient additional space situation, maximize the number of buckets
Uniform distribution of the mapping function can be used to input data into the N tub of K
At the same time, for sorting bucket elements, choose which sort algorithm compares the impact is critical to performance.

Implementation logic

Set a quantitative array as an empty bucket.
Search sequence, and to project into a corresponding bucket to.
Each not empty the bucket sort.
From the house is not empty barrel projects and then back into the original sequence.
Show
Illustrated step by step: the array has array = [63, 157, 189, 51, 101, 47, 141, 121, 157, 156, 194, 117, 98, 139, 67, 133, 181, 13, 28, 109], its bucket sort: 
 

Algorithm code

Here mainly to facilitate the expression of ideas bucket sort, the use of a vector, a single bucket also used the sort stl
 1 //桶排序
 2 void BucketSort(int *arr, int n)
 3 {
 4     int max = arr[0];
 5     for (int i = 1; i < n; i++)
 6     {
 7         if (arr[i] > max)
 8             max = arr[i];
 9     }
10     vector<int> *bucket = new vector<int>[max / 10 + 1];
11     for ( int I = 0 ; I <n-; I ++ )
 12 is      {
 13 is          int Change = arr [i] / 10 ; // bucket number elements arr [i] is located 
14          bucket [Change] .push_back (arr [i]) ;
 15      }
 16      for ( int I = 0 ; I <n-; I ++ )
 . 17      {
 18 is          IF (bucket [I] .size ()> 0 )
 . 19          {
 20 is              Sort (bucket [I] .begin (), bucket [I ] .end ()); // barrel sort 
21          }
 22     }
 23 is      int index = 0 ;
 24      for ( int I = 0 ; I <n-; I ++) // tub traverse 
25      {
 26 is          for ( int J = 0 ; J <bucket [I] .size (); J ++) // traverse barrel element 
27          {
 28              ARR [index ++] = bucket [I] [J];
 29          }
 30      }
 31 is }

 Analysis of Algorithms

  • The average time complexity: O (n + k)
  • Best time complexity: O (n + k)
  • Worst time complexity: O (n ^ 2)
  • Space complexity: O (n * k)
  • Stability: Stable
Using a linear time O (n) in the best case bucket sort, bucket sort time complexity, and depends on the data between the respective bucket sort time complexity, because the time complexity of the other portions are O (n). Obviously, the smaller the bucket partition, the data between respective bucket the less time will be used to sort less. But the corresponding space consumption will increase.
 
 [Reference: https: //blog.csdn.net/developer1024/article/details/79770240]
 

Guess you like

Origin www.cnblogs.com/WindSun/p/11361055.html
Recommended