[英]Ascending Quicksort into Descending order
正如你們現在看到的,我被困在這個問題上,我不知道如何將這個升序程序以相同的格式轉換為降序,如果有人可以幫助我,那真的是你的好意。 我目前是一名學生。
public class QuickSort {
public static void main(String[] args) {
int[] arr = {4, 5, 1, 2, 3, 3};
quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public static void quickSort(int[] arr, int start, int end) {
int partition = partition(arr, start, end);
if (partition - 1 > start) {
quickSort(arr, start, partition - 1);
}
if (partition + 1 < end) {
quickSort(arr, partition + 1, end);
}
}
public static int partition(int[] arr, int start, int end) {
int pivot = arr[end];
for (int i = start; i < end; i++) {
if (arr[i] < pivot) {
int temp = arr[start];
arr[start] = arr[i];
arr[i] = temp;
start++;
}
}
int temp = arr[start];
arr[start] = pivot;
arr[end] = temp;
return start;
}
}
你只需要改變
if (arr[i] < pivot)
到
if (arr[i] > pivot)
結果 :
5, 4, 3, 3, 2, 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.