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
What is the correct procedure for mounting the m240 on the m122a1 tripod after the pintle is attached to the receiver?
cluponka [151]

Using the front sight adjusting tool, loosen (turn counterclockwise) the adjusting screw on the front sight assembly the desired amount. Then tighten (turn clockwise) the opposite side screw on the left exactly the same number of clicks.

<h3>What is a Machine gun ?</h3>

The term "machine gun" refers to a rifled, autoloading, fully automatic weapon intended for continuous direct fire using rifle rounds. Other automatic weapons, such as automatic rifles, are often intended to fire in brief bursts rather than continuously, and are not regarded as real machine guns because of this.

  • Machine guns and other automatic weapons vary in that they fire rounds continually until the shooter lets off of the trigger after pulling it once. Fully automatic guns are uncommon compared to semi-automatic rifles.

Learn more about Machine gun here:

brainly.com/question/1358898

#SPJ4

8 0
1 year ago
An engineer is trying to build a new measurement tool. Which step should the engineer complete first? A. Design a model of the t
JulijaS [17]

Answer:

B. Precisely define the problem that is to be solved.

Explanation:

Engineering can be defined as the scientific and technological principles that is used for the design, development, operation and control of tools, machines or equipments, structures and systems. These machines, tools, systems and structures are typically designed and developed for the purpose of solving peculiar problems relating to human life. Simply stated, engineering is focused on proffering solutions to real life problems through a design process.

<em>Generally, the design process comprises of series of steps used for the development of various tools, machines, structures and systems. In a chronological order, the basic steps of a design process are;</em>

1. Define the problem: to proffer a solution to any problem, you have to precisely define the problem that is to be solved. Therefore, this is the first step of the design process.

2. Conduct a research: the engineer should collect or gather data (informations) relating to the project.

3. Brainstorming and analysis of data: this is the stage where the engineer conceptualize his or her ideas.

4. Create a prototype or simulated model of the product.

5. Product analytics and test: this is where the product is being used and tested for any flaw, error or defects. Thus, troubleshooting is also required at this stage.

<em>Hence, if an engineer is trying to build a new measurement tool; the first step the engineer should complete is to precisely define the problem that is to be solved so as to have a good clear-cut understanding of the problem. </em>

8 0
3 years ago
A wastewater treatment plant discharges 1.0 m3/s of effluent having an ultimate BOD of 40.0 mg/ L into a stream flowingat 10.0 m
kondaur [170]

Answer:

a) 6.4  mg/l

b) 5.6 mg/l

Explanation:

Given data:

effluent Discharge Q_w = 1.0 m^3.s

Ultimate BOD L_w = 40 mg/l

Discharge of stream Q_r = 10 m^3.s

Stream ultimate BOD L_r = 3  mg/l

a) Ultimate BOD of mixture= \frac{Q_w l_w + Q_r L_r}{Q_w + Q_r}

                                         = \frac{1*40 + 10*3}{10 +1} = 6.4 mg/l

b) utlimate BOD at 10,000 m downstream

t =\frac{distance}{speed} = \frac{10,000}{\frac{Q_r +Q+w}{55}} \times \frac{hr}{3600} \times  \frac{day}{24 hr}

putting Q_r + Q_w = 1+ 10 = 11 m^3/s

t = 0.578  days

we know

L_t = L_o e^{-kt}

L_t = 6.4 \times e^{-0.22 \times 0.578}

L_t = 5.6 mg/l

7 0
3 years ago
The driveshaft of an automobile is being designed to transmit 238 hp at 3710 rpm. Determine the minimum diameter d required for
AURORKA [14]

Explanation:

Below is an attachment containing the solution.

4 0
3 years ago
Inspection with considering a variable uses gages to determine if the product is good or bad. True or False?
irina1246 [14]

Answer:A. 40% B.50% C. 60% Od 70%

Explanation:A. True B. False

4 0
3 years ago
Read 2 more answers
Other questions:
  • Make sure that the switch is on (if the drill is electric), the chuck key is not removed before you plug in the drill or turn it
    11·2 answers
  • A device that helps increase field worker productivity by providing reliable location and time
    13·1 answer
  • A Barnes and Books is interested in purchasing a two-story building for a new
    5·1 answer
  • What should one do with a load if one is going to leave a jack under a load?
    11·1 answer
  • g Clay sized particles in a river are likely to be transported as _______________________, whereas gravel sized particles are li
    5·1 answer
  • I really need help with my last topic,Hazard communication,if anyone can help me as soon as possible,that could be my Christmas
    12·1 answer
  • Describe how to mix and apply body filler?
    13·1 answer
  • which type of irrigation fluid is typically used for endoscopic procedures using monopolar electrosurgery
    15·1 answer
  • The three construction crafts that require a MINIMUM of a 4-year college degree are
    11·1 answer
  • when a unit load is secured to a pallet, it is more difficult for pilferage to take place. true false
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!