1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
iren2701 [21]
4 years ago
13

Implement a quick sort algorithm that will accept an integer array of size n and in random order. Develop or research three diff

erent approaches as how you can pick the pivot. Write the code so the quick sort can choose among the above three different ways of picking the pivot. Run the algorithm for different sets of random data and make sure that for each set of data, you run the algorithm with all three options for the pivot selection. Record the number of key operations for each case. Report the numbers in a way that will allow you to draw a conclusion about which pivot selection approach was more efficient and how much variations you see between the different runs.
Engineering
1 answer:
Nostrana [21]4 years ago
8 0

Answer:

#include <cstdlib>  

#include <iostream>  

#include <array>  

using namespace std;  

const string APP_NAME = "Quick Sort Algorithm";  

const array<string, 3> MENU_OPTIONS = {  

"Simulate with Random data",  

"Enter data",  

"Exit program"  

};  

void printMenuOptions() {  

cout << endl << "---------------------------" << endl;  

cout << APP_NAME << endl;  

cout << "---------------------------" << endl;  

for (int i=0; i<MENU_OPTIONS.size(); i++) {  

cout << i+1 << ". " << MENU_OPTIONS[i] << endl;  

}  

cout << endl << "Select an option: ";  

}  

int getRandomInt(int min, int max) {  

return min + (static_cast<int>(rand() % (max - min + 1)));  

}  

bool inArray(int value, int* arr, int size) {  

bool found = false;  

for (int i=0; i<size; i++) {  

if (arr[i] == value) {  

found = true;  

}  

}  

return found;  

}  

void generateRandomArrays(int size, int* arr0, int* arr1, int* arr2, int* arr3) {  

int value;  

bool ok = false;  

for (int i=0; i<size; i++) {  

while (!ok) {  

value = getRandomInt(1, size*10);  

if (!inArray(value, arr0, size)) {  

arr0[i] = value;  

arr1[i] = value;  

arr2[i] = value;  

arr3[i] = value;  

ok = true;  

}  

}  

ok = false;  

}  

}  

void print(int* data, int size) {  

for (int i=0; i<size; i++) {  

cout << data[i] << " ";  

}  

}  

int getPivot(int first, int last, int approach) {  

int pivot;  

switch (approach) {  

case 2:  

pivot = first;  

break;  

case 3:  

pivot = last;  

break;  

case 1:  

default:  

pivot = (first + last) / 2;  

}  

return pivot;  

}  

void swap(int* data, int i, int j) {  

int temp = data[i];  

data[i] = data[j];  

data[j] = temp;  

}  

int quickSort(int* data, int first, int last, int approach) {  

int ops = 0;  

int i = first;  

int j = last;  

int pivot = getPivot(i, j, approach);  

while (i <= j) {  

while (data[i] < data[pivot]) {  

i++;  

}  

while (data[j] > data[pivot]) {  

j--;  

}  

if (i <= j) {  

ops++;  

swap(data, i, j);  

i++;  

j--;  

}  

}  

if (j > first) {  

ops += quickSort(data, first, j, approach);  

}  

if (i < last) {  

ops += quickSort(data, i, last, approach);  

}  

return ops;  

}  

void simulate(int size, bool display) {  

int* data0 = new int[size];  

int* data1 = new int[size];  

int* data2 = new int[size];  

int* data3 = new int[size];  

int ops1, ops2, ops3;  

generateRandomArrays(size, data0, data1, data2, data3);  

ops1 = quickSort(data1, 0, size-1, 1);  

ops2 = quickSort(data2, 0, size-1, 2);  

ops3 = quickSort(data3, 0, size-1, 3);  

if (display) {  

cout << "Unsorted Array: ";  

print(data0, size);  

}  

cout << endl << endl << "> QuickSort #1: pivot is at the median" << endl;  

cout << "Swaps done: " << ops1 << endl;  

if (display) {  

cout << "Sorted Array: ";  

print(data1, size);  

}  

cout << endl << endl << "> QuickSort #2: pivot is at the start" << endl;  

cout << "Swaps done: " << ops2 << endl;  

if (display) {  

cout << "Sorted Array: ";  

print(data2, size);  

}  

cout << endl << endl << "> QuickSort #3: pivot is at the end" << endl;  

cout << "Swaps done: " << ops3 << endl;  

if (display) {  

cout << "Sorted Array: ";  

print(data3, size);  

}  

}  

void enterArray(int size, bool display) {  

// declare some variables  

int* data0 = new int[size];  

int* data1 = new int[size];  

int* data2 = new int[size];  

int* data3 = new int[size];  

int ops1, ops2, ops3;  

int value;  

for (int i=0; i<size; i++) {  

cout << "Enter value " << i+1 << " of " << size << ": ";  

cin >> value;  

data0[i] = value;  

data1[i] = value;  

data2[i] = value;  

data3[i] = value;  

}  

ops1 = quickSort(data1, 0, size-1, 1);  

ops2 = quickSort(data2, 0, size-1, 2);  

ops3 = quickSort(data3, 0, size-1, 3);  

if (display) {  

cout << "Unsorted Array: ";  

print(data0, size);  

}  

cout << endl << endl << "> QuickSort #1: pivot is at the median" << endl;  

cout << "Swaps done: " << ops1 << endl;  

if (display) {  

cout << "Sorted Array: ";  

print(data1, size);  

}  

cout << endl << endl << "> QuickSort #2: pivot is at the start" << endl;  

cout << "Swaps done: " << ops2 << endl;  

if (display) {  

cout << "Sorted Array: ";  

print(data2, size);  

}  

cout << endl << endl << "> QuickSort #3: pivot is at the end" << endl;  

cout << "Swaps done: " << ops3 << endl;  

if (display) {  

cout << "Sorted Array: ";  

print(data3, size);  

}  

}  

int main(int argc, char** argv) {  

int choice;  

char option;  

int num;  

bool end = false;  

bool display = false;  

while (!end) {  

printMenuOptions();  

cin >> choice;  

switch (choice) {  

case 1:  

cout << endl << "Enter size of array (elements will be integers randomly generated): ";  

cin >> num;  

if (num > 0) {  

cout << "Values will be randomly generated from 1 to " << num*10 << endl;  

cout << "Do you want to display the sorted arrays? <y/N>: ";  

cin >> option;  

display = (option == 'y') ? true : false;  

simulate(num, display);  

} else {  

cout << endl << "Incorrect size." << endl;  

}  

break;  

case 2:  

cout << endl << "Enter size of array (you will enter the numbers): ";  

cin >> num;  

if (num > 0) {  

cout << "Do you want to display the sorted arrays? <y/N>: ";  

cin >> option;  

display = (option == 'y') ? true : false;  

enterArray(num, display);  

} else {  

cout << endl << "Incorrect size." << endl;  

}  

break;  

case 3:  

end = true;  

break;  

default:  

cout << endl << "Incorrect option. Try again." << endl;  

}  

}  

return 0;  

}

You might be interested in
One kilogram of water fills a 150-L rigid container at an initial pressure of 2 MPa. The container is then cooled to 40∘C. Deter
lukranit [14]

The pressure of water is 7.3851 kPa

<u>Explanation:</u>

Given data,

V = 150×10^{-3} m^{3}

m = 1 Kg

P_{1} = 2 MPa

T_{2}  = 40°C

The waters specific volume is calculated:

v_{1} = V/m

Here, the waters specific volume at initial condition is v_{1}, the containers volume is V, waters mass is m.

v_{1} = 150×10^{-3} m^{3}/1

v_{1} = 0.15 m^{3}/ Kg

The temperature from super heated water tables used in interpolation method between the lower and upper limit for the specific volume corresponds 0.15 m^{3}/ Kg and 0.13 m^{3}/ Kg.

T_{1}= 350+(400-350) \frac{0.15-0.13}{0.1522-0.1386}

T_{1} = 395.17°C

Hence, the initial temperature is 395.17°C.

The volume is constant in the rigid container.

v_{2} = v_{1}= 0.15 m^{3}/ Kg

In saturated water labels for T_{2}  = 40°C.

v_{f} = 0.001008 m^{3}/ Kg

v_{g} = 19.515 m^{3}/ Kg

The final state is two phase region v_{f} < v_{2} < v_{g}.

In saturated water labels for T_{2}  = 40°C.

P_{2} = P_{Sat} = 7.3851 kPa

P_{2} = 7.3851 kPa

7 0
3 years ago
The radiator of a steam heating system has a volume of 35 L and is filled with superheated water vapor at 200 kPa and 200°C. At
STatiana [176]
The radiator of a steam heating system has a volume of 20 L and is filled with superheated water vapor at 200 kPa and 200°C. At this moment both the inlet and ...
7 0
3 years ago
What are three demonstration drive tips for the vc-turbo engine?.
Oliga [24]

The three (3) demonstration drive tips for a VC-turbo engine are:

  1. Use your gears when overtaking and driving up a long hill.
  2. Warm up your engine before accelerating.
  3. Ensure your oil level is at the optimum gauge or level.

<h3>What is a VC-turbo engine?</h3>

A VC-turbo engine can be defined as a technologically-advanced internal combustion engine that is design and developed to be faster, especially by combining the torque and efficiency of an advanced diesel powertrain with the power of a high-performance gas engine.

<h3>The demonstration drive tips.</h3>

Basically, the three (3) demonstration drive tips for a VC-turbo engine include the following:

  1. Use your gears when overtaking and driving up a long hill.
  2. Warm up your engine before accelerating.
  3. Ensure your oil level is at the optimum gauge or level.

Read more on drive tips here: brainly.com/question/23968178

4 0
3 years ago
What is the resistance of a resistor if the current flowing through it is 3mA and the voltage across it is 5.3V?
Flura [38]

Answer: 1766.667 Ω = 1.767kΩ

Explanation:

V=iR

where V is voltage in Volts (V), i is current in Amps (A), and R is resistance in Ohms(Ω).

3mA = 0.003 A

Rearranging the equation, we get

R=V/i

Now we are solving for resistance. Plug in 0.003 A and 5.3 V.

R = 5.3 / 0.003

= 1766.6667 Ω

= 1.7666667 kΩ

The 6s are repeating so round off to whichever value you need for exactness.

6 0
1 year ago
A cannon ball is fired with an arching trajectory such that at the highest point of the trajectory the cannon ball is traveling
ELEN [110]

Answer:

The radius of curvature is 979 meter

Explanation:

We have given velocity of the canon ball v = 98 m/sec

Acceleration due to gravity g=9.81m/sec^2

We know that at highest point of trajectory angular acceleration is equal to acceleration due to gravity

Acceleration due to gravity is given by a_c=\frac{v^2}{r}, here v is velocity and r is radius of curvature

So \frac{98^2}{r}=9.81

r = 979 meter

So the radius of curvature is 979 meter

8 0
4 years ago
Other questions:
  • The cost of fixing a fault for a software product identified during the implementation phase is approximated at $32,000 USD. How
    9·1 answer
  • Provide main reasons for the short shot during the injection molding.
    13·1 answer
  • A charge of +2.00 μC is at the origin and a charge of –3.00 μC is on the y axis at y = 40.0 cm . (a) What is the potential at po
    5·1 answer
  • Paragraph summary on airplane history
    8·1 answer
  • On a supply and demand graph, equilibrium is the point where
    8·1 answer
  • What is the best way to cut a taper
    5·2 answers
  • Why dose bob not let humans touch him one and only Ivan
    8·1 answer
  • 10. If you pulled up on the rope shown for the device below with 75 lbs. force, how much weight
    15·1 answer
  • Sometimes we need to create heat, such as in circuit breakers and rear window
    5·2 answers
  • Safety measures to be taken during technical drawing<br>​
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!