Question: import java.util.Comparator; public class Quicksort { /* This function takes last element as pivot, places the pivot element at its correct position in sorted array,
import java.util.Comparator;
public class Quicksort {
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
int partition(Term arr[], int low, int high, Comparator
{
Term pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j { // If current element is smaller than or // equal to pivot if (comp.compare(arr[j], pivot) <= 0) { i++; // swap arr[i] and arr[j] Term temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // swap arr[i+1] and arr[high] (or pivot) Term temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp; return i+1; } /* The main function that implements QuickSort() arr[] --> Array to be sorted, low --> Starting index, high --> Ending index */ void sort(Term arr[], int low, int high, Comparator { if (low < high) { /* pi is partitioning index, arr[pi] is now at right place */ int pi = partition(arr, low, high, comp); // Recursively sort elements before // partition and after partition sort(arr, low, pi-1, comp); sort(arr, pi+1, high, comp); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
