基数排序是按照低位先排序,然后收集;在按照高位排序,然后在收集;依次类推,直到最高位。有时候有些属性是有优先级顺序的,先按低优先级排序,在按高优先级排序。最后的次序就是高优先级高的在前,高优先级相同的低优先级高的在前。
时间复杂度:O(n+m)
空间复杂度:O(n+m)
稳定性:稳定
package 排序算法;
import shixianClass.ArrayLoopQueue;
import java.util.Arrays;
import java.util.Random;
public class RadixSort {
public static void main(String[] args) {
int []arr=new int[20];
Random random=new Random();
for (int i=0;i<arr.length;i++){
arr[i]=random.nextInt(50);
}
radixSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void radixSort(int[] arr) {
//1.找到最大值
int max=arr[0];
for (int i=1;i<arr.length;i++){
if(arr[i]>max){
max=arr[i];
}
}
//2.计算位数
int radix=(max+"").length();
//3.创建桶
ArrayLoopQueue<Integer> [] backets=new ArrayLoopQueue[10];
for (int i=0;i<backets.length;i++){
backets[i]=new ArrayLoopQueue<>();
}
for (int r=1;r<=radix;r++){
for (int i=0;i<arr.length;i++){
backets[getIndex(arr[i],r)].offer(arr[i]);
}
int index=0;
for (int i=0;i<backets.length;i++){
while (!backets[i].isEmpty()){
arr[index++]=backets[i].poll();
}
}
}
}
private static int getIndex(int num, int r) {
int ret=0;
ret=num%10;
for (int i=1;i<=r;i++){
ret=num%10;
num/=10;
}
return ret;
}
}
执行结果
[0, 3, 9, 12, 15, 15, 25, 26, 30, 30, 32, 33, 34, 38, 43, 44, 44, 46, 46, 47]
测试
package 排序算法;
import java.util.Arrays;
import java.util.Random;
public class TestSort {
/*
数据分布情况
1.完全随机
2.大致有序
3.方差小
*/
public static int[] getTotalRandom(){
Random random=new Random();
int [] arr=new int[40000];
for (int i=0;i<arr.length;i++){
arr[i]=random.nextInt(10000);
}
return arr;
}
public static void main(String[] args) {
int [] arr1=getTotalRandom();
int [] arr2= Arrays.copyOf(arr1, arr1.length);
int [] arr3= Arrays.copyOf(arr1, arr1.length);
int [] arr4= Arrays.copyOf(arr1, arr1.length);
int [] arr5= Arrays.copyOf(arr1, arr1.length);
int [] arr6= Arrays.copyOf(arr1, arr1.length);
int [] arr7= Arrays.copyOf(arr1, arr1.length);
int [] arr8= Arrays.copyOf(arr1, arr1.length);
int [] arr9= Arrays.copyOf(arr1, arr1.length);
int [] arr10= Arrays.copyOf(arr1, arr1.length);
int [] arr11= Arrays.copyOf(arr1, arr1.length);
int [] arr12= Arrays.copyOf(arr1, arr1.length);
/* testSelectionSort(arr1);
testBubbleSort(arr2);
testInsertSort(arr3);
testShellSort(arr4);
testMergSort(arr5);
testHeapSort(arr6);*/
testQuickSort01(arr7);
testQuickSort02(arr8);
testQuickSort09(arr9);
testCounting(arr10);
testBucketSort(arr11);
testRadixSort(arr12);
}
private static void testRadixSort(int[] arr12) {
long startTime=System.currentTimeMillis();
RadixSort.radixSort(arr12);
long endTime=System.currentTimeMillis();
System.out.println("基数排序"+(endTime-startTime));
}
private static void testBucketSort(int[] arr11) {
long startTime=System.currentTimeMillis();
BucketSort. buckrtSort(arr11);
long endTime=System.currentTimeMillis();
System.out.println("桶排序"+(endTime-startTime));
}
private static void testCounting(int[] arr10) {
long startTime=System.currentTimeMillis();
CountingSort.countingSort(arr10);
long endTime=System.currentTimeMillis();
System.out.println("计数排序"+(endTime-startTime));
}
private static void testQuickSort09(int[] arr9) {
long startTime=System.currentTimeMillis();
QuickSort03.quickSort03(arr9,0,arr9.length-1);
long endTime=System.currentTimeMillis();
System.out.println("三路快排"+(endTime-startTime));
}
private static void testQuickSort02(int[] arr8) {
long startTime=System.currentTimeMillis();
QuickSort02.quickSort02(arr8,0,arr8.length-1);
long endTime=System.currentTimeMillis();
System.out.println("双路快排"+(endTime-startTime));
}
private static void testQuickSort01(int[] arr7) {
long startTime=System.currentTimeMillis();
QuickSort01.quickSort(arr7,0,arr7.length-1);
long endTime=System.currentTimeMillis();
System.out.println("单路快排"+(endTime-startTime));
}
private static void testHeapSort(int[] arr6) {
long startTime=System.currentTimeMillis();
HeapSort.heapSort(arr6);
long endTime=System.currentTimeMillis();
System.out.println("堆排序"+(endTime-startTime));
}
private static void testInsertSort(int[] arr3) {
long startTime=System.currentTimeMillis();
BubbleSort.bubbleSort(arr3);
long endTime=System.currentTimeMillis();
System.out.println("冒泡排序"+(endTime-startTime));
}
private static void testBubbleSort(int[] arr2) {
long startTime=System.currentTimeMillis();
insertionSort.lnsertionSortUpper(arr2);
long endTime=System.currentTimeMillis();
System.out.println("插入排序"+(endTime-startTime));
}
private static void testSelectionSort(int[] arr) {
long startTime=System.currentTimeMillis();
SelectionSort.selectionSort(arr);
long endTime=System.currentTimeMillis();
System.out.println("选择排序"+(endTime-startTime));
}
private static void testShellSort(int[] arr) {
long startTime=System.currentTimeMillis();
ShellSort.shellSort(arr);
long endTime=System.currentTimeMillis();
System.out.println("希尔排序"+(endTime-startTime));
}
private static void testMergSort(int[] arr) {
long startTime=System.currentTimeMillis();
MergSort.mergSort(arr,0,arr.length-1);
long endTime=System.currentTimeMillis();
System.out.println("归并排序"+(endTime-startTime));
}
}
执行结果
选择排序2084
插入排序142
冒泡排序2855
希尔排序8
归并排序279
堆排序2660
单路快排5
双路快排7
三路快排9
计数排序2
桶排序942
基数排序20