Java radix sort algorithm to achieve

1, brief radix sorting algorithm

About radix sort algorithm can obtain a lot of information in many ways. Radix sort (radix sort), also known sorting bucket (bucket sort), with respect to the common comparator ordering, dispensing a radix sort ordering, sorting is completed by assigning numbers to all be in a position to cover the original array and finally the process of.

Radix sort algorithm:

  • Sorting algorithm is implemented in a non-comparative method
  • Radix sort algorithm is a stable sorting algorithm
  • Time radix sort algorithm complexity: assignment requires O (n), the collection of O (r), where r is the number of the distribution list, to r = 10 as an example, there are 0 to 9 to the chain 10 so the original sequence classification. And d, i.e. the number of bits (e.g., the maximum number is 1234, the number of bits is 4, 4 = d), that is - the number of times "assigned Accumulate". Thus the time complexity is O (d * (n + r)).
  • Space radix sort algorithm complexity: requires additional auxiliary space, the radix sorting process, on the base for any number of bits for "barreling" operation, we require n + r temporary space

2, radix sort algorithm basics

    Radix sort algorithm is against every sort, so we need accurate data for a given calculated every bit of it is the number:

1  // This function returns the k-th bit of the integer i What 
2      public  static  int getFigure ( int i, int k) {
 . 3          int [] = {A 1, 10, 100, 1000, 10000, 100000 };
 . 4          return (I / A [K -. 1]) 10% ;
 . 5      }

3, thinking algorithm radix sorting algorithm

  • Set int [] count for each store array element number is the same number
  • Set int [] bucket as the bucket, used to access an array, the number of all the tub and the length of the array must be consistent
  • The maximum number of digits in the current element of the array is cyclic, each cycle is the same operation as
  • First find the number of bits per array element need to compare the current value of the number, use a loop to statistics data for each barrel in the present case the comparison of the number of bits
  • Using count [i] is determined to put the data, because the count [i] is stored in a certain sort is performed when the array of all the number of the bit number i is, after completion of execution of the cycle count [i] is the i-th position of the right-most boundary of the barrel
  • The data in the array using a loop of a respective bucket charged, attention is loaded from the rear forward
  • The data bucket all taken out, one by one re-assigned to arr
  • Repeat the procedure above loop, until finally obtain a final result.

4, Java radix sort algorithm to achieve

 1 package com.baozi.com.test.paixu;
 2 
 3 import java.util.Arrays;
 4 
 5 /**
 6  * @author BaoZi
 7  * @create 2019-05-27-11:24
 8  */
 9 public class BitNumberSort {
10     public static void main(String[] args) {
11         //定义整型数组
12         int[] nums = new int[]{21, 56, 88, 195, 354, 1, 35, 12, 6, 7, 4567, 87, 980, 12345};
13         int[] nums1 = new int[]{1,23,345,21,432,324,4563,56789,6543,233};
14         BitNumberSort.lsd_RadixSort(nums1, 5);
15         System.out.println(Arrays.toString(nums1));
16     }
17 
18     public static void lsd_RadixSort(int[] nums, int max) {
19         int[] count = new int[10];
20         int[] bucket = new int[nums.length];
21         for (int i = 1; i <= max; i++) {
22 
23             for (int k = 0; k < count.length; k++) {
24                 count[k] = 0;
25             }
26 
27             for (int k = 0; k < nums.length; k++) {
28                 count[getFigure(nums[k], i)]++;
29             }
30 
31             for (int k = 1; k < 10; k++) {
32                 count[k] = count[k] + count[k - 1];
33             }
34 
35             for (int k = nums.length - 1; k >= 0; k--) {
36                 int index = getFigure(nums[k], i);
37                 bucket[count[index] - 1] = nums[k];
38                 count[index]--;
39             }
40             for (int k = 0, j = 0; k < nums.length; k++, j++) {
41                 nums[k] = bucket[j];
42             }
43         }
44     }
45 
46     private static int getFigure(int i, int j) {
47         int[] a = new int[]{1, 10, 100, 1000, 10000, 100000};
48         return (i / a[j - 1]) % 10;
49     }
50 }

 

Guess you like

Origin www.cnblogs.com/BaoZiY/p/10929948.html