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
"It is necessary to select a metal alloy for an application that requires a yield strength of at least 345 MPa (50,000 psi) whil
nataly862011 [7]

Answer:

Both Brass and 1040 Steel maintain the required ductility of 20%EL.

Explanation:

Solution:-

- This questions implies the use of empirical results for each metal alloy plotted as function of CW% and Yield Strength.

- So for each metal alloy use the attached figures as reference and determine the amount of CW% required for a metal alloy to maintain a Yield Strength Y = 345 MPa.

- Left Figure (first) at Y = 345 MPa ( y -axis ) and read on (x-axis):

                        1040 Steel --------> 0% CW

                        Brass ---------------> 22% CW

                        Copper ------------> 66% CW

The corresponding ductility (%EL) for cold Worked metal alloys can be determined from the right figure. Using the %CW for each metal alloy determined in first step and right figure to determine the resulting ductility.

- Right Figure (second) at respective %CW (x-axis) read on (y-axis)

                       1040 Steel (0% CW) --------> 25% EL

                        Brass (22% CW) -------------> 21% EL

                        Copper (66% CW) ----------> 4% EL

We see that both 1040 Steel and Brass maintain ductilities greater than 20% EL at their required CW% for Yield Strength = 345 MPa.

4 0
4 years ago
A rectangular plate casting has dimensions 200mm x 100mm x 20mm. The riser for this sand casting mold is in the shape of a spher
9966 [12]

Answer:

Diameter of riser =6.02 mm

Explanation:

Given that

Dimensions of rectangular plate is 200mm x 100mm x 20mm.

Volume of rectangle V= 200 x 100 x 20 mm^3

Surface area of rectangle A

A=2(200 x 100+100 x 20 +20 x 200)mm^2

So V/A=7.69

We know that

Solidification times given as

t=K\left(\dfrac{V}{A}\right)^2   -----1

Lets take diameter of riser is d

Given that riser is in spherical shape so V/A=d/6

And

Time for solidification of rectangle is 3.5 min then time for solidificartion of riser is 4.2 min.

Lets take \dfrac{V}{A}=M

\dfrac{M_{rac}}{M_{riser}}=\dfrac{7.69}{\dfrac{d}{6}}

Now from equation 1

\dfrac{3.5}{4.2}=\left(\dfrac{7.69}{\dfrac{d}{6}}\right)^2

So by solving this d=6.02 mm

So the diameter of riser is 6.02 mm.

3 0
3 years ago
PLEASE HELP, THANK YOU
Elena L [17]

Answer:

6,3,2,5,1,4 because they jst are

Explanation:

3 0
3 years ago
Consider the following incomplete code segment, which is intended to print the sum of the digits in num. For example, when num i
ValentinkaMS [17]

Answer:

A) while (num >= 0)

Explanation:

To understand why we need to focus on the module and division operation inside the loop. num % 10 divide the number by ten and take its remainder to then add this remainder to sum, the important here is that we are adding up the number in reverse order and wee need to repeat this process until we get the first number (1%10 = 1), therefore, num need to be one to compute the last operation.

A) this is the correct option because num = 1 > 0 and the last operation will be performed, and after the last operation, num = 1 will be divided by 10 resulting in 0 and 0 is not greater than 0, therefore, the cycle end and the result will be printed.

B) This can not be the option because this way the program will never ends -> 0%10 = 0 and num = 0/10 = 0

C) This can not be the option because num = 1 > 1 will produce an early end of the loop printing an incomplete result

D) The same problem than C

E) There is a point, before the operations finish, where sum > num, this will produce an early end of the loop, printing an incomplete result

6 0
3 years ago
Environmental assessments (EAs) are an important component of any civil engineering project. Delgado Engineering has been contra
Tom [10]

Answer:

Environmental assessments (EAs) are an important component of any civil engineering project. Delgado Engineering has been contracted to design and assess a   multibillion-dollar urban rail center in a city, which is divided in the middle by a river. The center will be built on a previously unused island in the center of the river. For each of the four types of civil engineers (construction engineer, geotechnical engineer, structural engineer, transportation engineer), explain why they should be involved in the project—or why they would not be relevant to the project—and what their role would be. Then describe how the EA would proceed, including what might be included in the environmental impact assessment process, such as the use of geographic information systems. Finally, explain what might be in the environmental impact statement or why you think there may be a finding of no significant impact.

 

6 0
3 years ago
Other questions:
  • A brass alloy is known to have a yield strength of 240 MPa (35,000 psi), a tensile strength of 310 MPa (45,000 psi), and anelast
    11·1 answer
  • This is a classification of back pain based on duration. A) AcuteB) RecurrentC) ChronicD) All of the above
    11·1 answer
  • What change in the sound do you expect to hear when you increase the amplitude of
    14·1 answer
  • A town is designing a rectangular, 4m deep settling tank for treating surface water intake. The tank will have a flow velocity o
    14·1 answer
  • After the 2015 AFC Championship football game between the New England Patriots and the Indianapolis Colts, it was alleged that t
    10·1 answer
  • Describe how you could achieve a high quality finish on your work​
    9·2 answers
  • A 500 turn coil is wound on an iron core. When a 120Vrms 60Hz voltage is applied to the coil, the current is 1A rms. Neglect the
    13·2 answers
  • Niagara Falls is capable of producing a total power output of 2.575 MW at steady-state during the night when flow over the falls
    14·2 answers
  • Water is the working fluid in a Rankine cycle with reheat. The turbine and the pump have isentropic efficencies of 80%. Superhea
    14·1 answer
  • WHICH OF THIS PLS FAST!!!
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!