[Algorithm analysis] counting and sorting

Source: Internet
Author: User

1. The idea of counting and sorting

The basic concepts we have touched before, such as insert sorting, Merge Sorting, fast sorting, and heap sorting, are based on the comparison between set elements, the time complexity of their execution is optimized to O (nlgn), and the running mechanism of counting sorting is not based on the size comparison between set elements. What ??? Before comparison, can we differentiate the size between elements? Yes, the algorithm is so great that I was excited when I first saw it.

The basic idea of counting sorting is to determine the number of elements smaller than X for each input element x. With this information, you can place x directly on the position of the final output array.

2. Calculate the space cost of sorting. Assume that the original array is a [1... n].

Array C [k]: provides a temporary storage area. Here K is defined as: Each element in array A is an integer between 0 and K, and K can be considered as the maximum value in array.

Array B [N]: stores the sorting result.

Note: This is the general consumption of counting and sorting space. We can save the B array storing the sorting result here and directly store the final sorting result in the original array, this will be mentioned below.

3. Applicable Conditions for counting sorting

Counting sorting is a sorting method with a time complexity of θ (N). It is suitable for sorting small-range sets. The small scope here refers to k = O (N), that is, the K value cannot be too large. For example, we want to rank the scores of the national college entrance examination students. Here we know that the scores are between 0-150, that is, K =, the number of College Entrance Examination students is about more than 9 million, which can achieve good performance.

4. Counting and sorting pseudocode

counting-sort(a,b,k)  for i<-0 to k       do c[i] <- 0  for j<-0 to length(a)      do c[a[j]] <- c[a[j]] + 1  for i<-1 to k      do c[i] <- c[i] + c[i-1]  for j <- length[a]-1 down to 0      do b[c[a[j]]] <- a[j]         c[a[j]] <- c[a[j]] -1  

5. Counting and sorting Java implementation code and running results

Public class countsort {public static void count_sort (INT [] A, int [] B, int K) {int [] C = new int [k + 1]; // initialize the Count array C [I] 0 for (INT I = 0; I <C. length; I ++) C [I] = 0; // A [J] is the subscript of array C, the result of this loop is to record the number of occurrences of the corresponding underlying element of A in C for (Int J = 0; j <. length; j ++) {C [A [J] = C [A [J] + 1 ;} // In array C, the previous value is accumulated starting from subscript 1, and the assumption after the loop is: C [5] = 2, // It indicates the meaning: in array A, the <= 5 element has two for (INT I = 1; I <C. length; I ++) {C [I] = C [I] + C [I-1];} // cyclically place the result into the sorted array B for (Int J = B. length-1; j> = 0; j --) {B [C [A [J]-1] = A [J]; c [A [J] -- ;}// the function of printing an array written by myself, public static void printarray (INT [] A) {for (INT I = 0; I <. length; I ++) {system. out. printf ("% 4D", a [I]);} system. out. println ();} // function of the Self-written output string public static void say (string) {system. out. println (string);} public static void main (string [] ARGs) {int [] A = new int [10]; int [] B = new int [10]; random Rand = new random (); // randomization value for (INT I = 0; I <. length; I ++) {A [I] = Rand. nextint (100);} say ("befor count-sort a:"); printarray (a); system. arraycopy (A, 0, B, 0,. length); // copy the array // long start = system. nanotime (); // record start time count_sort (a, B, 100); // long stop = system. nanotime (); // record end time say ("after count-sort a:"); printarray (B); // system. out. printf ("% d", stop-start); // output the time consumed by the algorithm }}

Program running result:

6. Improvement on the counting Sorting Algorithm

As mentioned above, in order to execute the counting sorting algorithm, we need to consume an equal-size array B [1... n]. We can avoid using array B in some way.

For example, the element in array a is 3 2 4 2 1.

After array C is executed for I <-0 to k c [I] ++: [[0] [1] [2] [1] [1]

0 1 2 3 4

The subscript of C indicates the elements in array A, while C [I] indicates the number of times it appears in the array, we can use this relationship to directly Save the final result in array.

The key code is as follows:

Public static void count_sort (INT [] A, int K) {int [] C = new int [k + 1]; // initialize the Count array C [I] 0 for (INT I = 0; I <C. length; I ++) C [I] = 0; // A [J] is the subscript of array C, the result of this loop is to record the number of occurrences of the corresponding underlying element of A in C for (Int J = 0; j <. length; j ++) {C [A [J] = C [A [J] + 1;} int z = 0; // cyclically place the results in the sorted array B for (INT I = 0; I <K; I ++) {While (C [I] --> 0) {A [Z ++] = I ;}}}

Note that the above is gone: for I <-1 to k c [I] <-C [I] + C [I-1.

Running result:

Keep learning every day. Come on !!!!

 

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.