Introduction to algorithms-bucket sorting

Source: Internet
Author: User
Import Java. util. arraylist; import Java. util. iterator;/*** bucket sorting: the idea of Bucket sorting is to divide the interval [0, 1) into N subintervals of the same size. This is called a bucket, then, N inputs are distributed to each bucket. * Because the input numbers are evenly distributed independently in [), there is usually no large number in a bucket. * To get the result, sort the number in each bucket first, and then list the elements in each bucket in order. * The time complexity of Bucket sorting is O (n) */public class bucketsort {/***. Algorithm To sort buckets in arr. The sorting result is still in arr * @ Param arr */static void bucketsort (double arr []) {int n = arr. length; arraylist arrlist [] = new arraylist [N]; // evenly distributes the numbers in arr to [0, 1). Each bucket is a list, the element for (INT I = 0; I <n; I ++) {int temp = (INT) math. floor (N * arr [I]); If (null = arrlist [temp]) arrlist [temp] = new arraylist (); arrlist [temp]. add (ARR [I]) ;}// sort the numbers in each bucket by insert (INT I = 0; I <n; I ++) {If (null! = Arrlist [I]) insert (arrlist [I]);} // merge the sorting results of each bucket into int COUNT = 0; For (INT I = 0; I <N; I ++) {If (null! = Arrlist [I]) {iterator iter = arrlist [I]. iterator (); While (ITER. hasnext () {double D = (double) ITER. next (); arr [count] = D; count ++ ;}}}} /*** sort each bucket by insert. * @ Param list */static void insert (arraylist list) {If (list. size ()> 1) {for (INT I = 1; I <list. size (); I ++) {If (double) list. get (I) <(double) list. get (I-1) {double temp = (double) list. get (I); Int J = I-1; For (; j> = 0 & (double) list. get (j)> (double) list. get (J + 1); j --) list. set (J + 1, list. get (j); list. set (J + 1, temp) ;}}}/*** test ..... * The test data here is an array containing n elements, and each element must meet 0 <= arr [I] <1 */public static void main (string [] ARGs) {double arr [] = {0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23}; bucketsort (ARR); For (INT I = 0; I <arr. length; I ++) system. out. println (ARR [I]);}

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.