Java implementation of bucket sequencing

Source: Internet
Author: User

 PackageCom.edu.hpu.sort.bucket;Importjava.util.LinkedList;Importjava.util.List;ImportCom.edu.hpu.sort.Sort;/*sorting principle: The sequence from the array to remove the number, first 6 is taken out, and then put 6 into the 6th bucket, the process is similar to this: empty bucket [array [0]] = array to be sorted [0] [6 2 4 1 5 9] Array to be sorted [0 0           0 0 0 0 6 0 0 0] Empty barrels [0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present) order from the array to be sorted out the next number, at which time 2 is taken out, put it into the bucket 2nd, is a few barrels [6 2 4 1 5 9]  arrays to be queued [0 0 2 0 0 0 6 0 0 0] Empty barrels [0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present) 3,4,5,6 omitted, the process is the same, all into the bucket and then into the bottom of this [6 2 4 1 5 9] Array [0 1 2 0 4 5 6 0 0 9] Empty bucket [0 1 2 3 4 5 6 7 8 9] Bucket number (not actually present)*/ Public classBucketsortextendsSort {Private intRange = 0;  PublicBucketsort (intrange) {         This. Range =range; } @Override Public int[] Dosort (int[] arr) {@SuppressWarnings ("Unchecked")        //Constructing auxiliary Arrayslist<integer> [] aux =NewLinkedlist[range];  for(inti = 0; i < aux.length; i++) {Aux[i]=NewLinkedlist<integer>(); }         for(inti = 0; i < arr.length; i++){            //find the position of the element in the bucket and add itAux[arr[i]].add (Arr[i]); }         for(inti = 0, j = 0; I < Aux.length && J < Arr.length; i++){             for(intV:aux[i]) {Arr[j]=v; J++; }        }        returnarr; }     Public Static voidMain (string[] args) {sort sort=NewBucketsort (10); Sort.printorder (New int[]{4, 1, 3, 2, 6, 9, 9}); }    }

Java implementation of bucket sequencing

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.