Java Sorting Algorithm _ 004 bucket sorting

Source: Internet
Author: User
Package WZS. Sort; import java. util. arrays; // The bucket sort or so-called box sort. Algorithm The working principle is to divide the array into a limited number of buckets. // Each bucket is sorted individually (it is possible to use another sorting algorithm or to continue to use the bucket sorting in the progressive manner ). Bucket sorting is an inductive result of nest sorting. // When the values in the array to be sorted are evenly distributed, the linear time (NTH) is used for Bucket sorting )). However, bucket sorting is not a comparative sorting, and it is not affected by the lower limit of O (n log n. Public class binsort {public static void main (string [] ARGs) {int intarray [] = {10, 3, 5, 7, 9, 1, 4, 2, 6, 8}; basket (intarray); system. out. println ("sorted:" + arrays. tostring (intarray);} public static void basket (INT data []) // data is the array to be sorted {int n = data. length; int bask [] [] = new int [10] [N]; int index [] = new int [10]; int max = integer. min_value; For (INT I = 0; I <n; I ++) {max = max> (integ Er. tostring (data [I]). Length ())? MAX: (integer. tostring (data [I]). length () ;}string STR; For (INT I = max-1; I> = 0; I --) {for (Int J = 0; j <N; j ++) {STR = ""; if (integer. tostring (data [J]). length () <max) {for (int K = 0; k <max-integer. tostring (data [J]). length (); k ++) STR + = "0";} STR + = integer. tostring (data [J]); bask [Str. charat (I)-'0'] [index [Str. charat (I)-'0'] ++] = data [J] ;}int Pos = 0; For (Int J = 0; j <10; j ++) {for (int K = 0; k <index [J]; k ++) {data [POS ++] = bask [J] [k] ;}} for (INT x = 0; x <10; X ++) index [x] = 0 ;}}}

Output result:

 
After sorting: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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.