5 minutes to understand Radix Sort

5 minutes to understand Radix Sort

Foreword

Radix sort without compare and swap, but the use of distribution and collection are two basic sorting operation realized. Radix sort is divided into two: the first is LSD, ordered from lowest-order bit; the second is MSD, ordered from most significant bit.

Radix sort idea introduced

Assignment: the digital, each bit in the range 0-9, the container 10 is required (which may be referred we barrel), reference numeral which is 10 barrels 0-9. Every trip sorting, we take each element in the bit value sequentially into the bucket.

2. Collection: After a trip to the sort is complete, we order from 0-9 buckets order to take elements.

3. Continue distribution and collection, sorting is done until the maximum number of digits.

 

Algorithm description:

Sort the data to be: 12, 34, 2, 123, 25, 59, 37

The use of LSD, from a low start sorting

The first round: take a number of bits, into a corresponding barrel, such that digit 2 of 12, into barrel number 2; 4 digits is 34, into the tub 4

0    
1    
2 12 2
3 123  
4 34  
5 25  
6    
7 37  
8    
9 59  

 

After the first round, the data obtained: 12, 2, 123, 34, 25, 37, 59

Second round: take ten digits, into the bucket. 2 For example, ten digits is 0, No. 0 into the barrel

0 2  
1 12 123
2 25  
3 34 37
4    
5 59  
6    
7    
8    
9    

After the second round, the data obtained: 2, 12, 123, 25, 34, 37, 59

Third round: take hundreds digit, into the bucket

0 2 12 25 34 37 59
1 123          
2            
3            
4            
5            
6            
7            
8            
9            

Finally, the data orderly 2, 12, 25, 34, 37, 59, 123

Radix sort of code implementation

private static void radixSort(int[] arr) {

// store the maximum value, is temporarily recorded as the first element

int max = arr[0];

// Get the maximum value of the array to be sorted in

for (int v : arr) {

if (v > max) {

max = v;

}

}

// represents the bucket list with a total of 10 barrels, each barrel is a list of corresponding elements

List<List<Integer>> list = new ArrayList<List<Integer>>();

for(int i = 0; i < 10; i ++) {

list.add(new ArrayList<Integer>());

}

// determine the number of cycles round

for(int i = 0, factor = 1; i < max; factor *= 10, i ++) {

for(int j = 0; j < arr.length; j ++) {

// The corresponding bit (bit / ten ...) taken through the number, and the data into the bucket

list.get((arr[j] / factor) % 10).add(arr[j]);

}

// traverse barrel, which will be placed in the data array arr

for(int j = 0, k = 0; j < list.size(); j ++) {

while(!list.get(j).isEmpty()) {

arr[k] = list.get(j).get(0);

list.get(j).remove(0);

k++;

}

}

}

}

 

to sum up

Radix sort is a method by recording the value of keywords you sort of step by step. Keyword is generally applicable to the recording of an integer type, for sorting character strings and is not suitable.

 

Guess you like

Origin www.cnblogs.com/qfchen/p/11322151.html