Question: Java QuickSort Issue - why isn't my code sorting correctly? We are given an array 'A' with 'n' items in the array. The base call

Java QuickSort Issue - why isn't my code sorting correctly?

We are given an array 'A' with 'n' items in the array. The base call in the starter code is quickSort(A, 0, n - 1); Low is the first index of the array and high is the last index of the array. The pivot MUST be A[high] for this assignment.

//sort the elements in the subarray A[low,...,high] private static void quickSort(int[] A, int low, int high) { if (low < high) { int i = partition(A, low, high); quickSort(A, low, i - 1); quickSort(A, i + i, high); } } //partition A[low,...,high] into two subarrays by using a pivot (we will pick the last element A[high] as the pivot), and return the index of the pivot in the resulting partition private static int partition(int[] A, int low, int high) { int pivot = A[high]; int i = low - 1; for (int j = low; j < high; j++) { if (A[j] <= pivot) { i++; int temp = A[i]; A[i] = A[j]; A[j] = temp; } } int temp = A[i+1]; A[i+1] = A[high]; A[high] = temp; return i + 1; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!