Bucket sort _bucketsort

Source: Internet
Author: User

Bucket sort _bucketsort bucket sort of thinking bucket sort of code compilation summary

Bucket sort _bucketsort

Suppose you have 5 million papers, each test paper is 100 points, if you want to these papers according to the score to sort, day chatter, 5 million papers Ah, quick sort. Heap sort. Merge sort. Faced with so many data, the average down above each one of the algorithms at least cost nlogn=5000000log5000000=111267433 nlogn=5000000log5000000=111267433 unit time Ah, nearly 100 million, too slow.

What if we do this, first buy 101 barrels back, respectively, for each barrel of 0-100 labels, we just go through all the papers on one side, the score is N of the test paper into the box number n, when all the papers are put into the corresponding barrel inside, We have sorted all the papers according to the score. The time to traverse through all the data is 5 million times, compared with 100 million times, it saves a lot of time. What is used here is the idea of bucket sorting. bucket sort of thought

Bucket sort, as the name implies is to use bucket of thought to put the data into the corresponding barrels, and then the data in each bucket sorted, and finally put all the data in the bucket in order to get the ordered data we need

For example, we have some of the following data

49 43 11 61 31 71 53 51 71 84

Now, we'll put them in the bucket according to the 10 bits of these numbers.

put data in a bucket

bucket#.0 +++
Bucket#.1 +++ 11
bucket#.2 +++
bucket#.3 +++ 31
bucket#.4 +++ 49 43
Bucket#.5 +++ 53 51
bucket#.6 +++ 61
BUCKET#.7 +++ 71 71
BUCKET#.8 +++ 84
bucket#.9 +++

Note that the above bucket of data is unordered, now using a fast sorting algorithm for each bucket of data to sort, can be a quick sort, insert sort, merge sort inside any kind of

sort the data in the bucket

bucket#.0 +++
Bucket#.1 +++ 11
bucket#.2 +++
bucket#.3 +++ 31
bucket#.4 +++ 43 49
bucket#.5 +++ 51 53
bucket#.6 +++ 61
BUCKET#.7 +++ 71 71
BUCKET#.8 +++ 84
bucket#.9 +++

Notice that the data order in the red-labeled bucket above has changed, and after sorting the elements in each barrel, we need to iterate through each bucket sequentially, taking the data out of the bucket in order to get the ordered sequence.

combine the elements within each barrel
11 31 43 49 51 53 61 71 71 84

In short, the steps of the bucket order are: apply A certain number of barrels according to a set mapping function , the elements in the sequence are mapped to the corresponding bucket (the mapping function of the example above is bucket (i) =I/10) traversing all the buckets , the elements in each bucket are sorted , the sorting algorithm can use bubble sort, insert sort, quick sort, merge sort and so on to fit the algorithm to combine all the elements in the bucket according to the order sequence.

The only difficulty is the mapping function design, mapping function needs to ensure that the number of elements mapped to each bucket is the same, that is, there are n n elements in the sequence, the bucket number is M m, then each bucket within the mapping function map should have n/m n/ M element. We analyze the efficiency of the algorithm, traverse through all elements of the call time O (n) o (n), then all the bucket elements are sorted, the average sort time in each bucket is (n/m) log (n/m) (n/m) log (n/m), so we get the total sort time of O (n) +m∗ (n /m) log (n/m) =o (n) +n (LOGN−LOGM) O (n) +m* (n/m) log (n/m) =o (n) +n (LOGN-LOGM), if the number of buckets is equal to the number of sequence elements, then the algorithm will achieve the optimal efficiency O (n) o (n), But this time, too, is the most space-consuming time.

If the elements in the barrel are unevenly distributed, the efficiency of the algorithms that will result in bucket sequencing is greatly reduced, and we consider the worst-case scenario where the mapping function maps all elements to the same bucket, then inserts the elements into the bucket, and the running time will rise O (N2) O (n^2), So the design of a good performance mapping function is particularly important bucket sorting code

The sort algorithm in the bucket here uses a quick sort

/************************************************* * @Filename: bucketsort.cc * @Author: Qeesung * @Email: Q eesung@qq.com * @DateTime: 2015-05-17 14:57:36 * @Version: 1.0 * @Description: Bucket sort ********************************

/#include <iostream> #include <ctime> #include <cstdlib> #include <vector>
using namespace Std; Bucket sort mapping Function #define BUCKET (i) ((i)/10)/** * Exchange container inside the two elements * @param array input * @param pos1 position 1 * @param pos2 position 2 *
    /void Exchange (Std::vector<int> & Array, int pos1, int pos2) {int temp = ARRAY[POS1];
    ARRAY[POS1] = Array[pos2];
ARRAY[POS2] = temp; /** * Print a container * @param VEC target container/void Printvector (Std::vector<int> & VEC) {for (STD::VECTOR&LT;INT&G T;::iterator i = Vec.begin (); I!= vec.end ();
    ++i) {cout<<*i<< "T";
} cout<<endl; /** * Quick sort of incoming containers * In fact, you can not use this, directly with the algorithm inside the Stablesort (Vec.begin, vec.end, comp) * onThe container can be sorted, it is too lazy to implement the linked list, so use vector to achieve * @param VEC input container * @param left edge of the container * @param right side of the right-hand container * * * * QuickSort (ST
    D::vector<int> & Vec, int left, int right) {if (left >= right) return;
    int i = left-1;
    int x = Vec[right]; for (int k = left; k <= right-1; ++k) {if (Vec[k] <= x)//Exchange two elements {Exchange (VEC,
        ++i, K);
    } Exchange (VEC, ++i, right);
    QuickSort (VEC, left, i-1);
QuickSort (VEC, i+1, right);
    /** * Bucket order, where the elements entered are 0-100 elements inside, create 10 barrels * @param invec input container/void Bucketsort (Std::vector<int> & Invec) {
    const int & bucketnum = 10;
    Create 10 barrels std::vector<std::vector<int> > Buckets (bucketnum);
    Iterate over the input container for (int i = 0; i < invec.size (); ++i) {buckets[bucket (invec[i))].push_back (invec[i)); //Start the quick sort for each bucket for (int i = 0; i < Bucketnum ++i) {quickSort (buckets[i), 0, Buckets[i].sizE ()-1);
    //start to remove data from the bucket invec.clear ();
        for (int i = 0; i < Bucketnum ++i) {std::vector<int> & tempvec = Buckets[i]; cout<< "bucket#."
        <<i<< "+++";
            for (int k = 0; k < tempvec.size (); ++k) {cout.width (4);
            cout<<tempvec[k]<< "T";
        Invec.push_back (Tempvec[k]);
    } cout<<endl;
    int main (int argc, char const *argv[]) {const int & arraysize = 10;
    int inarray[arraysize];
    Srand ((int) (Time (NULL)));
    for (int i = 0; i < arraysize ++i) {Inarray[i] = rand ()%100;
    } std::vector<int> Vec (InArray, inarray+arraysize);
    cout<< "before sort array:" <<endl;

    Printvector (VEC);
    Bucketsort (VEC);
    cout<< "after sort array" <<endl;
    Printvector (VEC);
return 0; }

Run result :
Before sort array:
48 85 2 71 25 48 53 42 50 25

bucket#.0 +++ 2
Bucket#.1 +++
bucket#.2 +++ 25 25
bucket#.3 +++
bucket#.4 +++ 42 48 48
bucket#.5 +++ 50 53
bucket#.6 +++
bucket#.7 +++ 71
bucket#.8 +++ 85
bucket#.9 +++

After sort array:
summary of 2 km

Bucket sorting is suitable for situations in which data can be stacked according to the characteristics of the data, for example, the array is only between 0-100, then we can stack the data in the same way as 10 bits. However, the stacking bucket must ensure that all elements of the last bucket are greater than (ascending)/less than (descending) the previous bucket, otherwise the mapping function is invalid.

The map of the bucket sort is like a hash table map, and if we want to make the bucket order fast and efficient, we need to design a suitable mapping function. And bucket sequencing involves trade-offs between time and space, with the increase in the number of barrels, the performance of the bucket sorting algorithm will also increase (the best case number of barrels and sequence elements equal n=m n=m), while the space occupancy will increase, so in the design bucket sorting algorithm, should be based on the actual situation, to make the corresponding design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.