Detailed explanation of radix sorting and java implementation

The detailed explanation of radix sorting and the implementation of java is very clear. I attach the code I wrote myself to reduce the space bit n*10 to n;


package suanfa;




import java.util.Arrays;
import java.util.Scanner; import java.util.concurrent.CountDownLatch;

public class Main {
	
	public static void main (String[] args) {
		int[] arr = {34,52,33,45,35};
	//	int[] arr1 = {1,2,3,4,5};
		radixSort(arr,getDigit(arr));
		System.out.println(Arrays.toString(arr));
	}
	
	public static void radixSort(int [] arr ,int digit) {
		int[] count = new int[10];//Record the subscript of each bucket;
		int k;
		int[] bucket = new int [arr.length];//Auxiliary sorting array
		for(int i=1;i<=digit;i++) {
			for(int j=0;j<count.length;j++) {//Initialize the calculation bucket subscript array
				count[j] = 0;
			}
			for(int j=0;j<arr.length;j++) {
				count[getDigitNum(arr[j], i)]++;//Record the number of i-th value bitNum;
				
			}
			System.out.println(Arrays.toString(count));
			for(k=1;k<count.length;k++) {//Record the subscript of the kth bucket
				count[k] = count[k]+count[k-1];
				
			}
			System.out.println(Arrays.toString(count));
			for(int j = arr.length-1;j>=0;j--) {
				bucket[count[getDigitNum(arr[j], i)]-1] = arr[j];
				count[getDigitNum(arr[j], i)]--;
			}
			for(int j=0;j<arr.length;j++) {
				arr[j] = bucket[j];
			}
		}
		
	}
	public static int getDigitNum(int bitNum,int i) {//Get the value bitNum of the i-th bit;
		//System.out.println(((int)Math.pow(10, i-1))%10);
		return bitNum/((int)Math.pow(10, i-1))%10;
	}
	public static int getDigit(int[] arr) {
		int maxNum = 0;
		int digit = 0;
		for(int i =0 ;i< arr.length;i++) {//Find the maximum number;
			if(arr[i] > maxNum) {
				maxNum = arr[i];
			}
		}
		while(maxNum>0) {//Find the maximum number of digits
			digit++;
			maxNum /= 10;
		}
		System.out.println(digit);
		return digit;
	}
}

The idea of ​​bucket sorting solves a programming problem: there are n numbers, find the maximum difference between adjacent two-digit numbers, and the time complexity is O(n). Obviously, if this question is sorted first and then compared, the time will be at least nO(logn), so the idea of ​​bucket sorting is used, that is, to find the maximum and minimum values, divide this part of the difference space into n+1 parts, and then establish n+1 If you divide these numbers into these buckets, it is obvious that there must be an empty bucket, so the difference between two adjacent numbers must not be in the same bucket (because the difference in the same bucket must be smaller than the interval value of the bucket) ,Such as

34,38,33,31,43

The maximum and minimum difference is 12, divided into six points,

(1)【31-33) :31;

(2)【33-35): 33,34;

(3) [35-37): empty;

(4)【37-39):38;

(5) [39-41): empty;

(6)【41-43】:43

Obviously, the maximum difference between the values ​​of the same bucket is less than 2, and the minimum difference between adjacent buckets is >= 2; so the problem transformation bit is to find the comparison between the difference between the maximum value and the minimum value of adjacent non-empty buckets. That is, 33-31, 38-34; 43-38, which are worth comparing to get the maximum difference of 5.

	public static int maxGap(int[] nums) {
		if (nums == null || nums.length < 2) {
			return 0;
		}
		int len = nums.length;
		int min = Integer.MAX_VALUE;
		int max = Integer.MIN_VALUE;
		for (int i = 0; i < len; i++) {
			min = Math.min (min, nums [i]);
			max = Math.max(max, nums[i]);
		}
		if (min == max) {
			return 0;
		}
		boolean[] hasNum = new boolean[len + 1];
		int[] maxs = new int[len + 1];
		int[] mins = new int[len + 1];
		int bid = 0;
		for (int i = 0; i < len; i++) {
			bid = bucket(nums[i], len, min, max);
			mins[bid] = hasNum[bid] ? Math.min(mins[bid], nums[i]) : nums[i];
			maxs[bid] = hasNum[bid] ? Math.max(maxs[bid], nums[i]) : nums[i];
			hasNum[bid] = true;
		}
		int res = 0;
		int lastMax = maxs [0];
		int i = 1;
		for (; i <= len; i++) {
			if (hasNum[i]) {
				res = Math.max(res, mins[i] - lastMax);
				lastMax = maxs [i];
			}
		}
		return res;
	}

	public static int bucket(long num, long len, long min, long max) {
		return (int) ((num - min) * len / (max - min));
	}







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324839477&siteId=291194637