专业做网站优化价格,济南做网站找哪家好,河南郑州暴雨伤亡,高唐网站建设基数排序是一种非比较排序算法#xff0c;它将待排序的数字按照位数进行排序。基数排序的思想是先按照个位数进行排序#xff0c;然后按照十位数进行排序#xff0c;接着按照百位数进行排序#xff0c;以此类推#xff0c;直到最高位排序完成。 基数排序的步骤如下#x… 基数排序是一种非比较排序算法它将待排序的数字按照位数进行排序。基数排序的思想是先按照个位数进行排序然后按照十位数进行排序接着按照百位数进行排序以此类推直到最高位排序完成。 基数排序的步骤如下 代码思路 class RadixSort{public static void redixSort(int[] arr){if (arrnull || arr.length 2){return;}redixSort(arr,0,arr.length-1,maxbits(arr));}//求最大数有多少位private static int maxbits(int[] arr) {int max Integer.MIN_VALUE;for(int a: arr){maxMath.max(max,a);}int res0;while (max ! 0){res;max/10;}return res;}private static void redixSort(int[] arr, int l, int r, int digit) {final int radix10;int i0,j0;//定义一个与arr长度相等的数组int[] help new int[r-l1];//有多少位就循环几次从个位开始for (int d1;ddigit;d){//count和count‘都用count表示//count[0] 当前位d位是0的数字有多少个//count[1] 当前位d位是0和1的数字有多少个//count[2] 当前位d位是0和1和2的数字有多少个//count[i] 当前位d位是0~i的数字有多少个int[] countnew int[radix];//count[0..9]//countfor (il;ir;i){jgetDigit(arr[i],d);count[j];}//count’for (i1;iradix;i){count[i]count[i]count[i-1];}//从右往左遍历对应的数放到help中for (ir;il;i--){jgetDigit(arr[i],d);help[count[j]-1] arr[i];count[j]--;}//help数组赋值给结果数组for (il, j0;ir;i,j){arr[i] help[j];}}}//取出当前数对应位数的数如x109d1相当于取109个位上的数即9private static int getDigit(int x,int d){return ((x/((int) Math.pow(10,d-1))) % 10);}
}