2025-01-16 快速排序(完善尾数基数法)
This commit is contained in:
parent
64b537761a
commit
a041c9c815
@ -12,17 +12,21 @@ public class QuickSort {
|
||||
/**
|
||||
* 已最后一个数作为基准(有待完善)
|
||||
*/
|
||||
//todo 1.如果基准数重复了 2.完整递归
|
||||
private void tailQuicksort(){
|
||||
int index = 0;
|
||||
int mid = arr[arr.length-1];
|
||||
for(int i=0; i<arr.length-1; i++){
|
||||
if(arr[i]<mid){
|
||||
private void tailQuicksort(int start, int end){
|
||||
if(start >= end){
|
||||
return;
|
||||
}
|
||||
int index = start;
|
||||
int mid = arr[end];
|
||||
for(int i=start; i<end-1; i++){
|
||||
if(arr[i]<=mid){
|
||||
swap(i,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);
|
||||
long startTime = System.currentTimeMillis();
|
||||
quickSort.midQuicksort(0,arr.length-1);
|
||||
quickSort.tailQuicksort(0,arr.length-1);
|
||||
long endTime = System.currentTimeMillis();
|
||||
System.out.println("排序后的序列:");
|
||||
quickSort.show();
|
||||
|
Loading…
Reference in New Issue
Block a user