Research on the algorithm of------------bucket sorting based on algorithm introduction

Source: Internet
Author: User

To illustrate the process of sorting buckets, suppose there are now a={0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68}, the buckets are sorted as follows:

Study on the count sort we know ———— count sort is assumed that the input is made up of integers in a small range, whereas bucket ordering assumes that the input is produced by a random process that distributes the elements evenly and independently across the interval [0,1]. When the input of the bucket sort conforms to the uniform distribution, it can be expected to run linearly. The idea of barrel sequencing is: Divide the interval [0,1] into n equal-sized sub-ranges, become buckets (buckets), then distribute n input numbers into buckets, sort the number of buckets, and then list the elements in each bucket in order.

The pseudo-code for the bucket ordering is given, assuming that the input is an array of n elements, and each element satisfies 0≤a[i]<1, and an auxiliary array b[0....n-1] is required to hold the linked list (bucket). The pseudo-code looks like this:

Bucket_sort (A)
n = Length (A)
For i= 1 to N
Do insert a[i] into list B
For I=0 to N-1
Do sort list b[i] with insertion sort
Concatenate the list b[0], b[1],,,b[n-1] together in order

The Real bucket ordering is now based on pseudo-code, which uses the C + + method and the functions of the list and iterator in the STL . and sorting the elements in the bucket using the insertion sort algorithm.

<pre name= "code" class= "CPP" > #include <iostream> #include <vector> #include <list> #include <cstdlib>using namespace Std;void bucket_sort (float *datas, size_t length) {int I, j;int index;float fvalue;size_t l size;list<float> *retlist = new List<float>[length];list<float>::iterator iter;list<float>: : Iterator Prioiter, enditer;for (i = 0; i<length; ++i) {index = static_cast<int> (datas[i] *);//insert a new ele Mentretlist[index].push_back (Datas[i]); lsize = Retlist[index].size (); if (Lsize > 1) {//get The last element in the list [Index]iter =--retlist[index].end (); fvalue = *iter;enditer = Retlist[index].begin ();//insert the last element in right PO  Sitionwhile (iter! = enditer) {//get The second last element in the List[index]prioiter =--iter;//back up iter to the last element in the List[index]iter++;//compare-valuesif (* (prioiter)-*iter > 0.000001) {Float temp = * (Prioiter); * (prioiter) = *iter;*iter = temp;} iter--;}The right inserted position* (++iter) = Fvalue;}} Copy the result to DATASJ = 0;for (int i = 0; i<length; i++) {for (iter = Retlist[i].begin (); ITER! = Retlist[i].end () ; ++iter) datas[j++] = *iter;a}delete[] retlist;} int main () {float datas[10] = {0.78f, 0.17f, 0.39f, 0.76f, 0.23f, 0.67f, 0.48f, 0.58f, 0.92f, 0.12f};bucket_sort (datas, 1 0); cout << "After bucket_sort the result is:" << endl;for (int i = 0; i<10; i++) cout << datas[i] << ""; cout << endl;exit (0);}






Research on the algorithm of------------bucket sorting based on algorithm introduction

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.