2025-01-16 快速排序(完善尾数基数法)

This commit is contained in:
liangjinglin 2025-01-16 22:32:46 +08:00
parent 64b537761a
commit a041c9c815

View File

@ -12,17 +12,21 @@ public class QuickSort {
/** /**
* 已最后一个数作为基准(有待完善) * 已最后一个数作为基准(有待完善)
*/ */
//todo 1.如果基准数重复了 2.完整递归 private void tailQuicksort(int start, int end){
private void tailQuicksort(){ if(start >= end){
int index = 0; return;
int mid = arr[arr.length-1]; }
for(int i=0; i<arr.length-1; i++){ int index = start;
if(arr[i]<mid){ int mid = arr[end];
for(int i=start; i<end-1; i++){
if(arr[i]<=mid){
swap(i,index); swap(i,index);
index ++; index ++;
} }
} }
swap(index,arr.length-1); swap(index,end);
tailQuicksort(start,index-1);
tailQuicksort(index+1,end);
} }
/** /**
@ -86,7 +90,7 @@ public class QuickSort {
} }
QuickSort quickSort = new QuickSort(arr); QuickSort quickSort = new QuickSort(arr);
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
quickSort.midQuicksort(0,arr.length-1); quickSort.tailQuicksort(0,arr.length-1);
long endTime = System.currentTimeMillis(); long endTime = System.currentTimeMillis();
System.out.println("排序后的序列:"); System.out.println("排序后的序列:");
quickSort.show(); quickSort.show();