Menu

(Solved) : 1 Modified Quicksort Reduce Number Swaps Using Following Approach Keep Leftindex Starting Q33389751 . . .

1. Modified Quicksort I

Reduce the number of swaps using the following approach:

Keep a leftIndex starting at the beginning of the arrayand a rightIndex starting from the end of the array. Thesetrack the current elements being examined that should be stored onthe left of the pivot and on the right of the pivot.

The two variables move in alternating steps: leftIndexincrements and rightIndex decrements.

The left scan stops if it sees a larger element than the pivot;the right scan stops if it sees a smaller element than the pivot;then swap the two.

This continues until left crosses right#include <iostream>using namespace std;void quickSort(int a[], int low, int high);int partition(int a[], int low, int high);void swap(int &a, int &b);int main(){ system(“pause”);}void quickSort(int a[], int low, int high){ if (low < high) { int pivotIndex = partition(a, low, high); // divide quickSort(a, low, pivotIndex – 1); // conquere the left quickSort(a, pivotIndex + 1, high); // conquere the right }}int partition(int a[], int low, int high){ int pivot = a[low]; int pivotIndex = low; for (int i = low + 1; i <= high; i++) { if (a[i] < pivot) { pivotIndex++; swap(a[i], a[pivotIndex]); } } swap(a[low], a[pivotIndex]); return pivotIndex;}void swap(int & a, int & b){ int temp = a; a = b; b = temp;}

Expert Answer


Answer to 1 Modified Quicksort Reduce Number Swaps Using Following Approach Keep Leftindex Starting Q33389751 . . .

OR