Mình có tham khảo một clip và bài trên mạng về QuickSort giờ thực hành thì nó chả chạy được ngồi một lúc cũng không biết lỗi nên mong ae gíup mình.
public class QuickSort {
public static int arrayInt [] ={2,23,12,20,10,15,20};
public void output(){
for(int i: arrayInt){
System.out.print(i+" ");
}
}
public void Quicksort(int left,int right){
int x = arrayInt[(left+right)/2];//Gán mốc là giá trị ở giữa mãng
int i = left, j = right;//Gán giá trị i chạy từ trái qua phải và j ngược lại
do{
while((i<=j)&&(x>=arrayInt[i])){
i++;
}
while((i<=j)&&(x<=arrayInt[j])){
j--;
}
if(i<j){
int temp = arrayInt[i];
arrayInt[i] = arrayInt[j];
arrayInt[j] = temp;
i++;
j--;
}
}while(i<=j);
if(left<j){
Quicksort(left, j);
}
if(i<right){
Quicksort(i, right);
}
}
public static void main(String[] agrs){
QuickSort met = new QuickSort();
System.out.println("Value before the sort");
met.output();
met.Quicksort(0, arrayInt.length -1);
System.out.println("Value after the sort");
met.output();
}
}
Nó được như sau:
Value before the sort
2 23 12 20 10 15 20