Câu hỏi của em đã comment trong đoạn code ở dưới ạ, mong mọi người giúp đỡ. Em cảm ơn nhiều ạ !!
import java.util.Scanner;
public class QuickSort
{
public static void qSort(int[] arr, int l, int r)
{
int pivot = arr[(l + r) / 2];
int i = l;
int j = r;
while (i < j)
{
while (arr[i] < pivot)
{
i++;
}
while (arr[j] > pivot)
{
j--;
}
/*Cho em hỏi về đoạn code ở dưới ạ
Khi thay hàm if ở dưới thành 2 hàm if gồm i < j và i == j thì khi chạy chương
trình lại không đúng */
/* if (i < j)
{
int tempValue = arr[i];
arr[i] = arr[j];
arr[j] = tempValue;
i++;
j--;
}
if (i == j)
{
i++;
j--;
} */
//EM FORMAT HƠI LỖI MONG MỌI NGƯỜI THÔNG CẢM :DD
if (i <= j)
{
int tempValue = arr[i];
arr[i] = arr[j];
arr[j] = tempValue;
i++;
j--;
}
}
if (i < r)
{
qSort(arr, i, r);
}
if (j > l)
{
qSort(arr, l, j);
}
}
public static void printArray(int[] arr)
{
for (int i : arr)
{
System.out.print(i + " ");
}
}
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
sc.close();
qSort(arr, 0, n - 1);
printArray(arr);
}
}
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?