Kids Learn data structure (10): Cardinal sort

Source: Internet
Author: User
Tags pow

Kids Learn data structure (10): Cardinal sort one, basic idea

Unify all the values to be compared (positive integers) to the same digit length, with a short number of digits preceded by 0. Then, start with the lowest bit (that is, single digit) and order one at a time. Thus, from the lowest bit to the highest order, the sequence becomes an ordered series.
Unlike other sorts, cardinality sorting does not involve a number of interchanges.
Cardinality sorting is a stable sorting algorithm.

8.png Two, the main steps

From the above calculation process, we can see that the cardinal sort is mainly three steps:
1. Assign all the elements to the corresponding buckets (since integers have a total of 10 0~9 per digit, it usually takes 10 barrels)
2. Assemble all the elements in the bucket and put them back into the array.
3, loop the above two steps in turn, the number of cycles is the maximum element maximum number of digits

Third, the code implementation
1Import java.util.ArrayList;2Import Java.util.Arrays;34PublicClassSort {56PublicStaticvoidRadixsort(Int[]Array) {7Gets the maximum number8int max =array[0];9for (int i =1; I <Array.Length; i++) {10if (Array[i] > Max) {Max =Array[i];12}13}1415int digitcount =0;16Determine the number of digits, the number of digits that are sorted17while (Max >0) {digitcount++;Max/=10;20}21st22A two-dimensional array list consists of 10 one-dimensional array list1;Arraylist<arraylist<integer>>List =New Arraylist<> ();24for (int i =0; I <10; i++) {arraylist<integer> sublist =New Arraylist<> ();26List.add (sublist);27}2829Digitcount sub-allocation and collection;30for (int i =0; i < Digitcount; i++) {31assigning array elements;32for (int num:Array) {33Get the number of i+1 digits;34int x = num% (int) Math.PowTen, i +1)/(int) Math.Pow, i);35List.get (x). Add (num);System.out.println ("I =" + i +", List" +"= " +list);37}3839int index =0;4041Rearrange elements in an array42for (int k =0; K <10; k++) {43while (List.get (k). Size () >0) {arraylist<integer> sublist =List.get (k);45Array[index] = Sublist.get (0);46Move the No. 0 element of an divisor groupSublist.remove (0);index++;49}50}For execution, all 10 arrays in the list are emptied.51}52}5354public static void main(string[] args) { int[] arr = {135, 242, 192, , 
                                                                                                                                                    
                                                                                                                                                     345, one, one, and one 
                                                                                                                                                       };  System.out.println ("Original array:" + arrays.tostring (arr)); Radixsort (arr); System.out.println ("Sorted array:" + arrays.tostring (arr)); 59}           
                                                                                                                                                             * 
Analysis

Here's a look at line 30th ~ 51st row.
(1) i = 0 o'clock, sorted for single digit
x = 135% 10/1 = 5
List.get (5) This is the 6th array to get from the list (since the subscript starts at 0, so get (i) corresponds to the i+1 number of groups),
List.get (5). Add (135) is to put 135 in the 6th array. At this point, List = [[], [], [], [], [], [135], [], [], [], []]

x = 242% 10/1 = 2
List.get (2). Add (242) is to put 242 in the 3rd array. At this point, List = [[], [], [242], [], [], [135], [], [], [], []]

......

After the loop of line 32nd to 37th is all done, List = [[], [11], [242, 192], [93], [24], [135, 345], [], [], [], [19]]

Line 42nd ~ 50th Row,
k = 0 o'clock, the No. 0 array is empty and does not enter a while loop
K = 1 o'clock, array[0] = 11,11 is removed, 1th array becomes empty, while loop stops
K = 2 o'clock, array[1] = 242,242 is moved out, the 2nd array becomes {192},array[2] = 192,192 is removed, the 2nd array becomes empty, while the loop stops
K = 3 o'clock, array[3] = 93,93 is removed, 3rd array becomes empty, while loop stops
K = 4 o'clock, array[4] = 24,24 is removed, 4th array becomes empty, while loop stops
K = 5 o'clock, array[5] = 135,135 is moved out, the 5th array becomes {345},array[6] = 345,345 is removed, the 5th array becomes empty, while the loop stops
K = 6 o'clock, the 6th array is empty and does not enter a while loop
K = 7 o'clock, the 7th array is empty and does not enter a while loop
K = 8 o'clock, the 8th array is empty and does not enter a while loop
K = 9 o'clock, array[7] = 19,19 is removed, the 9th array becomes empty
At this point, the for loop ends.

At this point, List = [[],[],[],[],[],[],[],[],[],[]],array = [11, 242, 192, 93, 24, 135, 345, 19], the effect is achieved by single-digit ordering.

(2) i = 1 o'clock, sorting for 10 digits, at which point the array is not the original array, but the last array that was obtained in the previous step, that is, array = [11, 242, 192, 93, 24, 135, 345, 19]

11 = 135 100/10 = 1
List.get (one). Add (11) is put 11 in the 2nd array. At this point, List = [[], [11], [], [], [], [], [], [], [], []]

x = 242% 100/10 = 4
List.get (4). Add (242) is to put 242 in the 5th array. At this point, List = [[], [11], [], [], [242], [], [], [], [], []]

......

After the 32nd to 37th line of the loop has been executed, List = [[], [11, 19], [24], [135], [242, 345], [], [], [], [], [192, 93]]

Line 42nd ~ 50th Line after the loop executes, list = [[[],[],[],[],[],[],[],[],[],[]], array = [11, 19, 24, 135, 242, 345, 192, 93], the effect of sorting by 10 digits is reached.

(3) i = 2 o'clock, sorted for the number of hundred, array is the result of the previous step, that is, array = [11, 19, 24, 135, 242, 345, 192, 93]
After the loop of line 32nd to 37th is all done, list = [[11, 19, 24, 93], [135, 192], [242], [345], [], [], [], [], [], []]
Line 42nd ~ 50th Line after the loop executes, list = [[[],[],[],[],[],[],[],[],[],[]], array = [11, 19, 24, 93, 135, 192, 242, 345], the effect is sorted by the number of hundred. This is also the final sort result.

Run results
array: [135, 242, 192, 93, 345, 11, 24, 19]Sorted array: [11, 19, 24, 93, 135, 192, 242, 345]
Iv. Complexity of Time

for arrays [135, 242, 192, 93, 345, 11, 24, 19],
The 30th line of code has a for, executed 3 times, because the maximum number is 3 digits
The 32nd line of code has a for, executed 8 times because there are a total of 8
The 42nd line of code has a for loop, and the 43rd line of code has a while loop. These two layers of loops add up and actually loop 8 times because there are 8 numbers in total.
So in this example, there is a total of 3 (8 + 8) cycles.

We set the maximum number of digits of the array to D, the array has n elements, you need to loop D(n + N) = 2d*n times. So the time complexity of the radix sort is O (d *n).



Kids Learn data structure (10): Cardinal sort

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.