Question: (java) Suppose that the implementation of sorting by quick sort given below (see below) were changed as follows: Procedure partition is retained, but quickSort is
(java)
Suppose that the implementation of sorting by quick sort given below (see below) were changed as follows: Procedure partition is retained, but quickSort is eliminated, so that its work is done directly in sort. Is this change a good idea? What purpose does quickSort have? Discuss.
---------------------------------------------------------------------------------------------------------------
/** OVERVIEWS: ... */
public class Arrays {
public static void sort (int[] a) {
if (a == null) return;
quicksort(a, a.length - 1);
}
private static void quicksort(int[] a, int low,
int high) {
if (low >= high) return;
int mid = partition(a, low, high);
quickSort(a, low, mid);
quickSort(a, mid + 1, high);
}
private static int partition(int[] a, int i, int j) {
int x = a[i];
while (true) {
while (a[j] > x) j--;
while (a[i] < x) i++;
if (i < j) { // need to swap
int temp = a[i]; a[i] = a[j]; a[j] = temp;
j--; i++;
} else return j;
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
