The sorting of three big linear sorts of buckets

Source: Internet
Author: User
Tags sorts

I. Concept INTRODUCTION

There are the author of the sort is also known as the bucket sort (the order of the elements in each bucket is sorted by counting), get the array C directly from the go after the traversal, output array value of the group subscript, for 0 does not output (or into the original array, unstable), but I think this statement is not rigorous ( One obvious problem is that the output will be a double for loop, but it also means that the order of the pigeon nest is not an issue, because the bucket ordering requires the input data to be within the [0,1] range (the counting order requires integers; in fact, it is all integers, or decimals, to divide the buckets), first the interval [0,1] Divides into n the same size sub-range, called the bucket, and then distributes the N input number to each bucket. Because the input number is evenly spaced and distributed independently on [0,1], there is generally no case of a lot of chatter in a bucket. To get the results, sort the numbers in each bucket, and then list the elements in the buckets in order.

Enclose the pigeon Nest sort Core source code:

    Public voidPigeonsort (int[]Array,intMax) {int[] C =New int[Max];//max is the maximum value in array arrays     for(intI=0; i<Array. length; i++) c[Array[I]] ++;The //c array is just the number of statistical elements that occur    intK =0; for(intI=0; i<max; i++) for(intj=1; j<=c[i]; J + +)Array[k++] = i; }
Two. Algorithm description

For example, to sort n integer a[1..n in the [1..1000] range, you can set the bucket to a range of size 10, specifically, set the set b[1] to store [1..10] integers, set b[2] to store (10..20] integers, ... Set B[i] Store ((i-1) *10, i*10] integer, i =,.. 100. There are a total of 100 barrels. Then scan the A[1..N] from start to finish, and put each a[i] into the corresponding bucket b[j]. And then the 100 barrels in each bucket in the number of sorting, then can be bubbling, selection, and even fast, in general, any sort method can be. Finally, the numbers in each bucket are output sequentially, and the numbers in each bucket are output from small to large, so that a sequence of all the numbers is ordered.

Indicates that the bucket sort action is on an input array with 10 numbers.

Three. Java implementation of the algorithm
Import Java.util.arraylist;import Java.util.collections;import java.util.Iterator; Public classBucketsort { Public Static void Bucketsort(DoubleArray[]) {intlength = Array.Length; ArrayList arrlist[] =NewArraylist[length];/ * * Each bucket is a list, storing the elements that fall on this bucket * Last cardinal sort I used a count sort of implementation, in fact, can also use the following methods, interested readers may wish to try (I think too complex) * But the efficiency is not high ( Using a dynamic Array) * /        //Divide the bucket and fill in the elements         for(inti =0; i < length; i++) {//0.7 to 0.79 in the 8th bucket, number 7, first barrel 0 to 0.09            inttemp = (int) Math.floor (Ten* Array[i]);if(NULL= = Arrlist[temp]) arrlist[temp] =NewArrayList ();        Arrlist[temp].add (Array[i]); }//Insert a sort of number in each bucket         for(inti =0; i < length; i++) {if(NULL! = Arrlist[i]) {//Here The sorting method is variable, but as soon as possible, except for the three linear sorts, there is no collections                //And the sort in arrays is good, because this is the quick shot after tuning                in the//arrays, the copyof and fill methods are used in the base order .Collections.sort (Arrlist[i]); }        }//output similar pigeon nest sort        intCount =0; for(inti =0; i < length; i++) {if(NULL! = Arrlist[i]) {Iterator iter = Arrlist[i].iterator (); while(Iter.hasnext ())                    {Double d = (double) iter.next ();                    Array[count] = D;                count++; }            }        }    }/* * Each element satisfies 0<=array[i]<1, it seems to be the same length, * If the same decimal place (digit), you can make the decimal number as an integer, and finally divided by 10^digit * can Random.nextint (101 )/100 * /     Public Static void Main(string[] args) {DoubleArray[] = {0.78,0.17,0.39,0.26,0.72,0.94,0.21,0.12,0.23,0.68}; Bucketsort (array); for(inti =0; i < Array.Length; i++) System. out. print (Array[i] +" "); System. out. println (); }}
Four. Algorithm application

In the interview of the massive data processing problems, such as to the number of billions of dollars per day to sort, direct sorting even if the use of NLGN algorithm, is still a very scary thing, memory can not accommodate so much data, when the bucket order to effectively reduce the magnitude of the data, and then reduce the order of magnitude of the data, Can get a better effect. In addition, it is said that the bucket sort of tuple sort, the individual think or the base sort processing tuple is better, after all, is a multi-keyword sort, just to compare a single number

Original address:
Http://www.cnblogs.com/hxsyl/p/3214379.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The sorting of three big linear sorts of buckets

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.