Use .cpp file below to write c++ code. Change split() function to use the midian of three methods. Show the status of the list F= [ 18, 5, 1, 3, 9, 7, 2, 12, 99, 6, 21] at the end of each phase of recursive Quick sort using median of three rule to determine the pivot key. Then write a non recursive version of this whole program using the median of three rule.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Use .cpp file below to write c++ code. Change split() function to use the midian of three methods. Show the status of the list F= [ 18, 5, 1, 3, 9, 7, 2, 12, 99, 6, 21] at the end of each phase of recursive Quick sort using median of three rule to determine the pivot key. Then write a non recursive version of this whole program using the median of three rule.

#include <iostream>
using namespace std;

static const int numValues = 11;

static int values[numValues] = { 18, 5, 1, 3, 9, 7, 2, 12, 99, 6, 21 };

void display();

void swap(int& item1, int& item2)
{
   int temp = item1;
   item1 = item2;
   item2 = temp;
}
int split(int first, int last)
{
   int splitVal = values[first];
   int saveF = first;
   bool onCorrectSide;
   first++;
   do
   {
       onCorrectSide = true;
       while (onCorrectSide)
       {
           if (values[first] > splitVal)
               onCorrectSide = false;
           else
           {
               first++;
               onCorrectSide = (first <= last);
           }
       }
       onCorrectSide = (first <= last);
       while (onCorrectSide)
       {
           if (values[last] <= splitVal)
               onCorrectSide = false;
           else
           {
               last--;
               onCorrectSide = (first <= last);
           }
       }
       if (first < last)
       {
           swap(values[first], values[last]);
           first++;
           last--;
       }
   } while (first <= last);
   swap(values[saveF], values[last]);
   display();
   return last;
}
void quickSort(int first, int last)
{
   if (first < last)
   {
       int splitPoint;
       splitPoint = split(first, last);
       //values[first].......values[splitPoint-1] <= splitVal
       //Values[splitPoint] = splitValue
       //values[splitPoint+1].......values[last] > splitVal
       quickSort(first, splitPoint - 1);
       quickSort(splitPoint + 1, last);
   }
}

int main()
{
   cout << "......................" << endl;
   cout << "Array Before Sorting" << endl;
   display();
   cout << "......................" << endl;
   quickSort(0, 10);

   cout << "......................" << endl;
   cout << "Array After Sorting" << endl;
   display();
   cout << "......................" << endl;
   return 0;
}

void display()
{
   for (int i = 0; i < numValues; i++)
       cout << values[i] << " ";
   cout << endl;
}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education