The data structure of hark and the counting sort of algorithm practice

Source: Internet
Author: User

Algorithm description

The counting sort is a linear sort, and its time complexity is much larger than the usual comparison sort. (The Count is O (n), and the comparison sort does not exceed O (NLOG2NJ)).

In fact, most of the counting sort is well understood, the only thing that is very painful to understand is that the data accumulated in order to ensure the stability of the algorithm, everyone listen to me to tell you know:

1, first of all, first out to sort the maximum value of the array, if our array is int[] Arraydata = {2, 4, 1, 5, 6, 7, 4, 65, 42}, then the maximum value is 65. (Code 17-21 Line is looking for maximum value)

2, then create a count array, the length of the count array is our array length to be sorted +1. That is 65+1=66. The function of a count array is to store the frequency of numbers appearing in the array to be sorted. For example, 4 appears two times, then the Count array arraycount[4]=2. OK, now you should understand why the count array length is 66 instead of 65? Because in order to store 0

Then create an array that stores the returned results, and the array length is the same as our original data length. (lines 24 and 26)

3, counting (code 29 to 31 lines)

4, the count array to accumulate the quantity, namely Arraycount[i]+=arraycount[i-1] (code 35 line to code 37 line).

The goal is to the stability of the data, this piece I actually looked at a long time to understand ... Proving my qualifications is really bad again. I'll try to explain:

In fact, this with the back of that step together to understand should be easier.

For example, if we count the array is 1 2 1 2 1, then represents 0 appeared once, 1 appeared two times, 2 appeared once, 3 appeared two times.

This is very easy to understand. Then let's look at the problem in a different perspective.

We can get the index position of each number according to this count array, that is, the position where the number 0 appears is index 0, the problem of the number 1 is index 1, 2, the position where the number 2 appears is index 3, the position where the number 4 appears is index 4, 5 ....

OK, as you can see, this index position is cumulative, so we need arraycount[i]+=arraycount[i-1] to store the index maximum value for each number. This is for the output behind.

5, finally, the original data from the forward output, and then each number can find the last implementation index of the counter. The numbers are then stored in the result array of the actual index. The index of the array is then counted--and the result is out.

PS: The counting sort actually eats the memory specially, therefore the application scenario is the maximum value determination and is not big, must be a positive integer.

Complexity of Time:

O (N+k)

Please check the code below: because there is a cycle of n, there is also a cycle of k, so the complexity of time is n+k

Complexity of space:

O (N+k)

Compare the code below: A count array of k+1 length is required, and an n-length result array is required, so the spatial complexity is n+k

Code

is using the Java

/* * Count sort */public class Countingsort {public static void main (string[] args) {int[] Arraydata = {2, 3, 1, 5, 6, 7, 4, 65 ,};int[] Arrayresult = Countintsort (Arraydata); for (int integer:arrayresult) {System.out.print (integer); System.out.print ("");}} public static int[] Countintsort (int[] arraydata) {int maxnum = 0;//fetch maximum for (int i:arraydata) {if (i > Maxnum) {max Num = i;}} Count array int[] Arraycount = new Int[maxnum + 1];//structure array int[] Arrayresult = new int[arraydata.length];//start count for (int I:arr Aydata) {arraycount[i]++;} For the Count Array i=i+ (I-1)//The purpose is to guarantee the stability of the data for (int i = 1; i < arraycount.length; i++) {arraycount[i] = Arraycount[i] + array COUNT[I-1];} for (int i = arraydata.length-1; I >= 0; i--) {arrayresult[arraycount[arraydata[i]]-1] = Arraydata[i];arraycount[ar raydata[i]]--;} return arrayresult;}}

Results

Reference:

http://blog.csdn.net/sjin_1314/article/details/8655061

Http://www.cnblogs.com/eaglet/archive/2010/09/16/1828016.html

The data structure of hark and the counting sort of algorithm practice

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.