Sorting algorithm (iii): counting sort and bucket order __ algorithm

Source: Internet
Author: User

Insert sort, heap sort, merge sort, and so on, in the final result of ordering, the order of each element depends on the comparison between them, we refer to this sort of sorting algorithm as comparison sort . In the worst case, any comparison sort algorithm has to go through Omega (NLGN) comparisons. Therefore, heap sequencing and merge ordering are all asymptotic optimal comparison sorting algorithms.
Count sorting, cardinality ordering, and bucket ordering can break the lower bound because they do not use a comparison sort method. This article mainly introduces counting sort and bucket sort. One, counting sort

Counting sort assumes that each of the N input elements is an integer in the range 0 through K, where K is an integer. When K=o (n), the count sort runs at \seta (n). 1. The basic idea of counting sort

For an element x in an input array, as long as we know the number of elements in this array that are smaller than x, then we can put x directly in the position of the (x+1). This is the basic idea of counting sort.
Based on this idea, one of the main problems in counting order is how to count the elements in an array. Plus, the element in the input array is an integer of the 0-k interval , the value of the INPUT element is represented by the address of another array, and the value of the array represents the number of elements to be counted.

The following is a method for counting the number of elements in an array of integers whose array elements are 0-k intervals .

/**
     * Statistical array elements are 0-k range of integers in the array of elements
     * @param src 
     * @param k element distribution between * * * public
    Static int[] Getarraycount (int[] src,int k) {
        int c[] = new Int[k];
        for (int i:src)
             c[i]++;
        return c;
    }
2. Counting Sort Realization
public class countsort{public static void Main (String args[]) {int k = 10;
        int test[] = Com.sunpro.java.RandomGenerator.randGenerator (10,k);
        Com.sunpro.java.Print.printArray (test);
        Countsort (test, k);
    Com.sunpro.java.Print.printArray (test);
        public static void Countsort (int[] A, int k) {//Initialize count array int[] count = new Int[k];
        Counts for each element in the input array for (int i:a) count[i]++;
        The sum of the elements before the count for (int i = 1; i < K; i++) count[i] = Count[i] + count[i-1];
        Initializes a new array to store the sorted element int[] B = new Int[a.length];
            for (int j = a.length-1 J >= 0; j--) {//place a[j] in the corresponding position b[count[a[j]]-1] = a[j];
        Counter minus one count[a[j]]--;
    } system.arraycopy (b,0, A, 0,a.length); }
}

One important property of counting sorts is stability, and the relative order of elements with the same value in the output array is the same as in the input array array. This stability is more important when sorting data is e-mailed to satellite data. second, bucket sorting

Bucket ordering (bucket sort) assumes that the input data is uniformly distributed. On average, his time cost is O (n). Counting sort assumes that the input data is distributed across a small range of integers, whereas bucket ordering assumes that the input is generated by a random process that distributes the elements evenly and independently across the [0,1] interval. 1. The basic idea of bucket sort

Bucket sorting divides the [0,1) interval into n-identical subgroups of the same size, which are called buckets . The n input elements are then placed in their respective buckets. Since the input is uniform and independent, there is generally no case of a lot of numbers falling in a bucket at the same time. In this way, we want to sort the data in each bucket and then iterate through each bucket, listing the elements in each bucket in order.
A bucket sort of example diagram:

In simple terms, the array arr is divided into n size the same sub range (bucket), each of which is sorted and finally merged. Is it a little bit like the split rule? Because the divide-and-conquer method is to decompose --to solve --to combine such routines. I don't think that's a problem. It can be understood that bucket ordering is a special kind of divide-and-conquer method, the special place is mainly embodied in the first two parts (so I do not know right ~). Specifically, the following:
In the Decomposition section, we use the idea of counting sort, and through decomposition, although there is no comparison, the data is divided into several intervals according to the size basically. For the characteristics of uniform distribution of input data, the range of data distribution can be divided evenly into n sub intervals. Then there is

Max-min = n * width; (1)

The max,min is the maximum minimum value of the input data, n is the number of sub intervals, and the width is the length of the child interval.
After such division, each data x corresponds to the bucket number (0-n-1) is;

index = (x-min)/width = (x-min)/(max-min) * n; (2)

In this way, when we take the N=array.length, the corresponding number of buckets for each data is determined:

index = (x-min)/(max-min) * Array.Length

If we take n= (max-min)/array.length, we have:

index = (x-min)/(max-min) * (max-min)/Array.Length = (x-min)/array.length;

This form looks relatively simple, in the actual programming also has certain convenience. There is nothing to discover about the pros and cons of performance. 2. The implementation of bucket sorting

The first type of grouping scenario:

public static void Bucketsort (int[] A) {//1. Construction bucket//1.1 Determine the number of barrels n int n = a.length;
        1.2 Declares and initializes a list, stores the linked list, list<arraylist<integer>> blist = new arraylist<> (n);
        for (int i = 0; i < n; i++) Blist.add (New arraylist<integer> (5));
        2. Place the elements in the array in a bucket//2.1 the value of the element to determine the maximum int max = Integer.min_value;
        int min = Integer.max_value;
            for (int a:a) {max = Math.max (max, a);
        min = math.min (min, a);
            //2.2 determines the number of each element into the bucket and puts it in for (int i:a) {//2.2.1 determines the number of barrels int len = a.length;
            Plus 1 is to ensure inde< a.length, prevent the program from throwing Indexoutofboundsex; 
            int index = (int) ((i-min)/(max-min+1.0) * a.length);
        2.2.2 into the corresponding bucket Blist.get (index). Add (i); 
        //3. Bucket sort for (int i = 0; i < blist.size (); i++) {Java.util.Collections.sort (Blist.get (i)); }
        //4. merge data int j = 0;
            for (arraylist<integer> arr:blist) {for (int i:arr) {a[j++] = i; }
        }
    }

The second grouping programme:

public static void Bucketsort (int[] arr) {

    int max = Integer.min_value;
    int min = integer.max_value;
    for (int i = 0; i < arr.length i++) {
        max = Math.max (max, arr[i));
        min = math.min (min, arr[i]);

    Bucket number
    int bucketnum = (max-min)/arr.length + 1;
    arraylist<arraylist<integer>> Bucketarr = new arraylist<> (bucketnum);
    for (int i = 0; i < Bucketnum i++) {
        bucketarr.add (new arraylist<integer> ());
    }

    Put each element in bucket for
    (int i = 0; i < arr.length i++) {
        int num = (arr[i]-min)/(arr.length);
        Bucketarr.get (num). Add (Arr[i]);

    Sort each bucket for
    (int i = 0; i < bucketarr.size (); i++) {Collections.sort (
        bucketarr.get (i));
    }

    System.out.println (Bucketarr.tostring ());

}
3. Operation complexity Analysis of bucket sequencing

According to the program we are easy to get, at worst, the time complexity of the bucket order is:

The average case:

The expected time for bucket ordering is:

These specific derivation can refer to the introduction of algorithms. In a word:

As long as the square of the size of all barrels and the total number of elements in a linear relationship, bucket sorting can be completed in linear time.

So how do you calculate "the sum of squares of all barrels is linearly related to the total number of elements"? Well, let's compare this with two options:

public static void Main (String args[]) {//Total small, range small int test1[] = Com.sunpro.java.RandomGenerator.randGener
        Ator (10,10);
        Total small, range large int test2[] = Com.sunpro.java.RandomGenerator.randGenerator (10,1000);
        Total number large, range small int test3[] = Com.sunpro.java.RandomGenerator.randGenerator (100,10);
        Total number large, range large int test4[] = Com.sunpro.java.RandomGenerator.randGenerator (100,1000);
        int test[] = {9, 99, 37, 41, 34, 33, 69, 92, 2, 18};
        Com.sunpro.java.Print.printArray (test);
        System.out.println ("Small sum, small scope: programme II:");
        BucketSort2 (test1);
        SYSTEM.OUT.PRINTLN ("programme I:");
        Bucketsort (test1);
        System.out.println ("Small number, large scope: programme II:");
        BucketSort2 (TEST2);
        SYSTEM.OUT.PRINTLN ("programme I:");
        Bucketsort (TEST2);
        SYSTEM.OUT.PRINTLN ("Large sum, small scope: programme II:");
        BucketSort2 (TEST3);
        SYSTEM.OUT.PRINTLN ("programme I:");
        Bucketsort (TEST3);
        SYSTEM.OUT.PRINTLN ("Large number, large scope: programme II:"); BucketsOrt2 (TEST4);
        SYSTEM.OUT.PRINTLN ("programme I:");
        Bucketsort (TEST4);

    Com.sunpro.java.Print.printArray (test); }

Run Result:

Bucket sort >java bucketsort Total small, small range: Scenario two: [[0, 1, 1, 2, 4, 5, 6, 8, 8, 9]] scheme one: [[0], [1, 1], [2], [], [4], [5], [6], [], [8, 8  ], [9]] The total number is small, the scope is large: Scenario two: [[2], [], [], [], [], [], [], [], [], [], [], [117], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [320], [], [], [342], [], [], [], [], [], [408], [418], [], [], [], [], [], [], [], [], [], [], [], [], [] [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [663], [], [684], [], [], [], [], [], [], [756], [], [], [], [], [], [], [] [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [993], scheme one: [[2], [117], [], [320, 342], [ 408, 418], [], [663, 684], [756], [], [993]] Total number of large, small range: Scenario two: [[[0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2 , 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3.4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9]] scheme one : [[0, 0, 0, 0, 0, 0], [[], [], [], [], [], [], [], [], [1, 1, 1, 1, 1, 1, 1, [], [], [], [], [], [], [], [], [], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [], [], [], [], [], [], [], [], [], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3], [], [], [], [], [], [], [], [], [], [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4], [], [], [], [], [], [], [], [], [], [5, 5, 5, 5, 5, 5, 5, 5, 5], [], [], [], [], [], [], [ [], [], [6, 6, 6, 6, 6, 6, 6, 6, 6, 6], [], [], [], [], [], [], [], [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7], [ [], [], [], [], [], [], [], [], [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8], [], [], [], [], [], [], [], [9, 9, 9, 9, 9, 9, 9, 9, 9], [], [], [], [], [], [], [], [], []] The total number is large, the scope is large: Scenario two: [[1, 11, 18, 30, 35, 58, 77, 100], [112, 128, 134, 139, 140, 141, 146, 159, 159, 184, 185, 193], [202, 216, 227, 236, 238, 241, 256, 257, 273], [300, 302, 308, 309, 310, 342 , 355, 358, 363, 364, 391], [406, 410, 425, 431, 442, 471, 473, 482], [502, 538, 566, 573, 581, 588, 598, 599], [602, 60 9 , 612, 646, 646, 660, 663, 663, 675, 684], [705, 708, 716, 719, 761, 761, 762, 773, 789, 793, 797], [801, 812, 823, 826, 845, 852, 853, 855, 874, 87 7, 880, 880, 882, 900], [904, 923, 932, 957, 969, 977)], scheme one: [[985], [997, 1], [11], [18], [], [30], [], [35], [], [100], [], [112], [128], [134, 139, 140], [141, 146], [159, 159], [], [], [184, 185], [193], [202], [216], [227], [23]; 6, 238], [241], [256, 257], [], [273], [], [300], [302, 308, 309, 310], [], [], [], [342, 345], [355, 358], [363] , [], [391], [406], [410], [425], [431], [442], [], [], [471, 473], [482], [], [502], [], [], [538], [], [], [566], [573] , [581, 588], [598, 599], [602, 609], [612], [], [], [646, 646], [], [660, 663, 663], [675], [684], [], [705, 708], [716] , [719], [], [], [], [761, 761, 762], [773], [], [789, 793, 797], [801], [812], [823, 826], [], [845], [852, 853, 855], [ ], [874, 877], [880, 880, 882], [], [900, 904], [], [923], [932], [], [957], [], [969, 977], [985], [997]]

Well, there's no harm without contrast. Through these four kinds of cases of output, we can get the intuitive conclusion:
For small areas, the second option is certainly not linear. For a wide range of situations, the second scenario is also basically not linear.
And for the first scheme, the basic can remain linear, but in the "large sum, small range" in the case of linear is not good. In fact, this kind of situation is more suitable for counting sort .

Reference documents:
Introduction to Algorithms Thomas

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.