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
Which principle of the software engineering code of ethics has gilbert violated?.
Yuki888 [10]

Answer:

Judgement

Explanation:

Gilbert is required by the Judgement Principle to "disclose those conflicts of interest that cannot reasonably be avoided or escaped." Since Gilbert professionally believes that the software meets specifications, secures documents, and satisfies user requirements, it is not clear if he violated any principle. However, he could have informed his client of his interest in the software and also presented other software packages of different companies from which the client could make its independent choice.

7 0
2 years ago
I will definitely rate 5 stars/brainliest!!! HELP PLEASE!!! State University must purchase 1,100 computers from three vendors. V
romanna [79]
Why 1+12+ Y3 < 1100
Says the state of university Need to purchase 1100 computers in total, we have the following answer on the way top
3 0
2 years ago
Soils with low percolation rates do not need special attention during site engineering. select one: true false
saveliy_v [14]

It is accurate to say that site engineering does not require particular consideration for soils with low percolation rates.

<h3>What are percolation rates?</h3>
  • The rate at which water percolates through the soil is a measure of its ability to absorb and treat effluent, or wastewater that has undergone preliminary treatment in a septic tank.
  • Minutes per inch are used to measure percolation rate (mpi).
  • The process of a liquid gently moving through a filter is called percolation. This is how coffee is typically brewed.
  • The Latin verb percolare, which meaning "to strain through," is the source of the word "percolation." When liquid is strained through a filter, such as when making coffee, percolation occurs.

To learn more about percolation rates, refer to:

brainly.com/question/28170860

#SPJ4

7 0
1 year ago
Which is a common refrigerant for domestic air conditioners?
Reptile [31]

Answer:

Explanation:

The most common HFC used in air conditioners is R-410A. This refrigerant is better than R-22 in terms of “Ozone Depletion” potential and energy efficiency, but it still causes global warming. A few more HFCs that are commonly used are: R-32 in Air Conditioners and R-134A in refrigerators.

4 0
3 years ago
Read 2 more answers
Who has good grades in school like A's B's dm me
siniylev [52]

Answer:

I do

Explanation:

4 0
2 years ago
Read 2 more answers
Other questions:
  • Represent the following sentence by a Boolean expression:
    11·1 answer
  • Consider a process in which a carbon-based fuel is combusted in the presence of 70% excess oxygen (assume that all of the oxygen
    10·1 answer
  • A household refrigerator that has a power input of 450 W and a COP of 1.5 is to cool 5 large watermelons, 10 kg each, to 8 C. If
    7·1 answer
  • A heat engine operates between 2 reservoirs at TH and 18oC. The heat engine receives 17,000 kJ/h from the high temperature reser
    10·1 answer
  • How much memory can a 32 -bit processor support ?
    13·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
  • Why do engineers play a variety of roles in the engineering process?
    6·1 answer
  • Which of the following might a cement mason or concrete finisher be responsible for? (Select all that apply).
    5·1 answer
  • What is a splitter gearbox​
    15·1 answer
  • Types of lubricants on the market include:
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!