Thuật toán này em viết còn cần phải sửa đổi gì ạ , em cảm ơn ạ
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package newpackage;
/**
*
* @author Admin
*/
import Hocdelamgi.quick;
import java.util.Scanner;
public class quickSort {
public static int divide(int[] arr, int r, int l) {
int pivot = arr[r];
int i = l;
int j = r - 1;
while (true) {
while (arr[i] < pivot && i <= j) {
i++;
}
while (arr[j] > pivot && i <= j) {
j--;
}
if (l > r) {
break;
}
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
int temp = arr[i];
arr[i] = arr[r];
arr[r] = arr[i];
return i;
}
public static void quick(int[] arr, int l, int r) {
if (l > r) {
int pivot = divide(arr, l, r);
quick(arr, l, pivot - 1);
quick(arr, pivot + 1, r);
}
}
public static void display(int[] arr) {
int n = arr.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
System.out.println("]\n");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Nhập số lượng mảng : ");
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
System.out.println("Phần tử mảng thứ " + i);
arr[i] = sc.nextInt();
}
sc.close();
System.out.println("Mảng trước khi sắp xếp : ");
System.out.println("-----------------------");
display(arr);
quickSort(arr, 0, n - 1);
divide(arr, 0, n - 1);
System.out.println("Mảng sau khi được sắp xếp : ");
System.out.println("--------------------------");
display(arr);
}
}
EDIT (@library): Tớ dùng markdown để format code của cậu, nên đừng xóa nó đi nhé!