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]
3 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]3 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
Air pressure is higher above an airfoil.<br> true or false
attashe74 [19]

Answer: true

Explanation:

it flows faster over the top of the wing because the top is more curved than the bottom of the wing. However

6 0
3 years ago
An aluminium alloy tube has a length of 750 mm at a temperature of 223°C. What will be its length at 23°C if its coefficient of
uranmaximum [27]

Answer:

Final length= 746.175 mm

Explanation:

Given that Length of aluminium at 223 C is 750 mm.As we know that when temperature of material increases or decreases then dimensions of material also increases or decreases respectively with temperature.

Here temperature of aluminium decreases so the final length of aluminium decreases .

As we know that

\Delta L=L\alpha\Delta T

Now by putting the values

\Delta L=750\times \25.5\times 10^{-6}\times 200

ΔL=3.82 mm

So final length =750-3.82 mm

Final length= 746.175 mm

3 0
3 years ago
A company, studying the relation between job satisfaction and length of service of employees, classified employees into three le
Wewaii [24]

Answer:

Below see details

Explanation:

A) It is attached. Please see the picture

B) First to calculate the overall mean,  

μ=65∗25/75+80∗25/75+95∗25/75  

μ=65∗25/75+80∗25/75+95∗25/75 = 80

Next to calculate E(MSTR) = σ2+(1/r−1) ∑ni(μi−μ)^2 = 5634

And E(MSE) = σ^2= 9

C) Yes, it is substantially large than E(MSE) in this case.

D) If we sampled 25 employees from each group, we are likely to get a F statistics to indicate differences of job satisfactions among three types of length of service of employees.

3 0
3 years ago
"Mass burn technology uses unprocessed municipal solid waste to generate heat which is used to produce electricity."
Flura [38]

Answer:

True

Explanation:

Mass burn technology is a type of waste-to-energy technology commonly used in the mass-burn system, where unprocessed municipal solid waste is burned in a large incinerator with a boiler, to  generate heat used in the production of electricity.

6 0
3 years ago
Read 2 more answers
Which type of irrigation conserves more water than other types of irrigation?
vlada-n [284]
Drip irrigation

Drip irrigation is one of the most efficient types of irrigation systems. The efficiency of applied and lost water as well as meeting the crop water need ranges from 80% to 90%
6 0
2 years ago
Other questions:
  • At a 4 percent annual growth rate in GDP per capita, it will take
    15·1 answer
  • In the contemporary approach to control systems, benefits of continuous monitoring include which one of the following? Multiple
    9·1 answer
  • g A pump is required to deliver 100 gpm at a head of 100 ft, but the pump rated capacity is 150 gpm at a head of 100 ft. If the
    9·1 answer
  • What can happen to you if you are in a crash and not wearing a seat belt?<br> Explain.
    13·2 answers
  • Realize the function f(a, b, c, d, e) = Σ m(6, 7, 9, 11, 12, 13, 16, 17, 18, 20, 21, 23, 25, 28)using a 16-to-1 MUX with control
    13·1 answer
  • Write Python expressions using s1, s2, and s3 and operators and * that evaluate to: (a) 'ant bat cod'
    14·1 answer
  • name the process by which mild steel can be converted into high carbon steel and explain it briefly ?​
    12·1 answer
  • The three suspender bars AB, CD, and EF are made of A-36 steel and have equal cross-sectional areas of 500 mm2. Determine the av
    9·1 answer
  • Which is an alloy made up of iron and carbon and has high compressive and tensile strength?
    11·1 answer
  • In a medical lab, Sandrine is working to isolate one element from a sample of liquid material. She uses a centrifuge, a machine
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!