Sorting of classical algorithms (II): bucket sorting and Pigeon nest sorting

Source: Internet
Author: User

Nest sorting:

The nest sorting, also known as the base classification, is a time complexity (Cosine (n )) it is also the most efficient sorting algorithm that inevitably traverses every element and sorts it. however, it is useful only when the difference value (or the value can be mapped to the difference value) is within a very small range. when multiple unequal elements are involved and these elements are placed in the same "Pigeon nest", the efficiency of the algorithm will be reduced. in order to make it easier and keep the nest sorting to adapt to different situations, for example, two elements ending in the Same bucket must be equal. We usually seldom use the nest sorting because it is rarely flexible, simplicity, especially when the speed exceeds other sorting algorithms. in fact, bucket sorting is more practical than nest sorting.
1 package offer; 2 3 4 public class bucketsort {5 6 static void bucketsort (INT data [], int min, int max) 7 {8 int bucksize = max-min + 1; 9 int bucket [] = new int [bucksize]; 10 int datalength = data. length; 11 // print the array 12 system. out. println ("Before sorting:"); 13 for (INT I = 0; I <datalength; I ++) 14 system. out. print (data [I] + ""); 15 system. out. println ("\ n"); 16 // establishes the number of each element 17 for (INT I = 0; I <datalength; I ++) 18 bucket [DATA [I]-min] ++; 19 // start sorting (implement 1) 20 // For (INT I = 0, j = 0; I <bucksize;) 21 // {22 // If (bucket [I]> 0) 23 // {24/data [J] = I + min; 25 // J ++; 26 // bucket [I] --; 27 //} 28 // else + + I; 29 //} 30 // second 31 Int J = 0; 32 for (INT I = 0; I <bucksize; I ++) 33 for (int K = 0; k <bucket [I]; ++ K) 34 data [J ++] = I + min; 35 36 // print array 37 system. out. println ("sorted:"); 38 for (INT I = 0; I <datalength; I ++) 39 system. out. print (data [I] + ""); 40 41} 42 public static void main (string [] ARGs) 43 {44 int data [] = {1, 2,-2, 3,, 3,-,-3,-,-, 4}; 45 bucketsort (data,-); 46} 47 48}

 

Bucket sorting: it is assumed that the input is a real number that is evenly distributed across [0, 1) intervals generated by a random process. Divide the interval [0, 1) into n sub-intervals of the same size (bucket), the size of each barrel 1/N: [0, 1/n), [1/N, 2/N), [2/N, 3/N ),..., [K/N, (k + 1)/n ),... Allocate N input elements to these buckets, sort the elements in the bucket, and connect the bucket to input 0 ≤ A [1 .. n] <1 secondary array B [0 .. n-1] is a pointer array pointing to a bucket (linked list ). Here we introduce the bucket Sorting Algorithm and Its Application: http://hxraid.iteye.com/blog/647759

Sorting of classical algorithms (II): bucket sorting and Pigeon nest sorting

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.