Counting sorting (linear)

Source: Internet
Author: User
Public class CountSort {public static void main (String [] args) {int [] a = {3, 1, 6, 0, 3, 0, 1, 5, 3, 6}; int max = getMax (a); arrDisplay (a, "Before mySort:"); a = mySort (a, max); arrDisplay (a, "After mySort: ");} public static int [] mySort (int [] a, int max) {int [] res = new int [. length]; int [] c = new int [max + 1]; for (int I = 0; I <res. length; I ++) {res [I] = 0 ;}for (int I = 0; I <c. length; I ++) {c [I] = 0;} int temp = 0; /* the number of times that an element appears and the number of times is placed in the position indicated by this element in the c array cannot take the maximum value during the traversal process, that is, getMax cannot be merged, because the size of c is required in advance; can it be directly converted to the maximum integer value? This is impossible. Super Memory */for (int I = 0; I <. length; I ++) {temp = a [I]; c [temp] = c [temp] + 1;} for (int I = 1; I <c. length; I ++) {c [I] = c [I] + c [I-1];} // must start from the back, this ensures stability for (int I =. length-1; I> = 0; I --) {temp = a [I]; res [c [temp]-1] = temp; // you do not have to worry about being less than 0, because it will not be reduced once it is equal to 0, because the original array does not have the element c [temp] = c [temp]-1;} return res ;} public static int getMax (int [] a) {int max = a [0]; for (int I = 1; I <. length; I ++) {if (a [I]> max) max = a [I];} return max;} public static void arrDisplay (int [], string str) {System. out. println (str); for (int I = 0; I <. length; I ++) System. out. print (a [I] + ""); System. out. println ();}}

1. algorithm Overview

By counting the number of occurrences of elements and then sorting, an auxiliary array is required. The size is the maximum element value (think about the counting process). In order to better understand the counting sorting, let's first imagine if all elements in an array are non-negative integers (the array subscript is an integer) and all elements are within 0-max (this value is smaller due to memory, for each element in the array, if I can know how many items in the array are less than or equal to this element, the position of this element in the sorted array can be accurately given.

Limitations: the above description shows that integers are needed (if there are negative numbers, the positive and negative numbers are sorted separately), and the maximum value must be within the range of an open array.

The algorithm is stable. The fifth step of the algorithm ensures the stability from the back to the back. I hope you can understand it carefully ......

Ii. Algorithm Description

  1. Obtain the maximum element max (depending on the algorithm implementation process, this method needs to be independent, that is, max needs to be determined in advance)
  2. Enable auxiliary arrays c [max] and res [original array size] and initialize them to 0
  3. Count: count the number of occurrences of an element and place the number of occurrences in the position indicated by this element in the c Array
  4. For the c array, the number of items in the array is less than or equal to the value of the element in the array is obtained from the subscript 1.
  5. Save res [c [temp]-1] (minus one because the subscript starts from 0) and update c [temp] for each element in the original array -- (do not worry about less than 0, because once it is equal to 0, it will not be reduced, because the original array does not have this element)

Iii. Java Implementation of the algorithm is as follows:

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.