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 magnitude of the maximum stress that exists at the tip of an internal crack having a radius of curvature of 3 × 10-4
Vladimir [108]

Answer:

maximum stress is 2872.28 MPa

Explanation:

given data

radius of curvature = 3 × 10^{-4} mm

crack length = 5.5 × 10^{-2} mm

tensile stress = 150 MPa

to find out

maximum stress

solution

we know that  maximum stress formula that is express as

\sigma m = 2 ( \sigma o ) \sqrt{\frac{a}{\delta t}}     ......................1

here σo is applied stress and a is half of internal crack and t is radius of curvature of tip of internal crack

so put here all value in equation 1 we get

\sigma m = 2 ( \sigma o) \sqrt{\frac{a}{\delta t}}  

\sigma m = 2(150) \sqrt{ \frac{\frac{5.5*10^{-2}}{2}}{3*10^{-4}}}  

σm = 2872.28 MPa

so maximum stress is 2872.28 MPa

8 0
2 years ago
The Energy Losses Associated with Valves and Fittings: a)- are generally associated with a K factor b)- are generally associated
madam [21]

Answer:

a)Are generally associated with factor.

Explanation:

We know that losses are two types

1.Major loss  :Due to friction of pipe surface

2.Minor loss  :Due to change in the direction of flow

As we know that when any hindrance is produced during the flow of fluid then it leads to generate the energy losses.If flow is along uniform diameter pipe then there will not be any loss but if any valve and fitting placed is the path of fluid flow due to this direction of fluid flow changes and  it produce losses in the energy.

Lot' of experimental data tell us that loss in the energy due to valve and fitting are generally associated with K factor.These losses are given as

Losses=K\dfrac{V^2}{2g}

8 0
3 years ago
For some transformation having kinetics that obey the Avrami equation (Equation 10.17), the parameter n is known to have a value
OleMash [197]

Answer:

t = 25.10 sec

Explanation:

we know that Avrami equation

Y = 1 - e^{-kt^n}

here Y is percentage of completion  of reaction = 50%

t  is duration of reaction = 146 sec

so,

0.50 = 1 - e^{-k^146^2.1}

0.50 = e^{-k306.6}

taking natural log on both side

ln(0.5) = -k(306.6)

k = 2.26\times 10^{-3}

for 86 % completion

0.86 = 1 - e^{-2.26\times 10^{-3} \times t^{2.1}}

e^{-2.26\times 10^{-3} \times t^{2.1}} = 0.14

-2.26\times 10^{-3} \times t^{2.1} = ln(0.14)

t^{2.1} = 869.96

t = 25.10 sec

5 0
3 years ago
To determine if a product or substance being used is hazardous, consult:__________.
qwelly [4]

Answer:

Option B: An MSDS

Explanation:

A dictionary is used to check up the meaning of general words and not for checking if a substance being used is hazardous. Option A is wrong.

MSDS means "Material Safety Data Sheet" and it contains documents with information that relates to occupational health & safety for checking various substances and products. Thus, option B is correct.

SAE stands for Society of Automotive Engineering and their standards pertain to mainly Automobiles. Thus option C is wrong.

EPA guidelines are mainly for checking facility and environmental health and safety compliance. Thus, option D is wrong.

3 0
3 years ago
A 30 mm thick AISI 1020 steel plate is sandwiched between two 10 mm thick 2024-T3 aluminum plates and compressed with a bolt and
denis-greek [22]

Answer:

275 MPa

Explanation:

Regardless of what it is holding, the stiffness of a bolt depends on its own material properties and geometry.

The stiffness is:

k = E * \frac{A}{l}

I assume this one is made of steel, because regular bolts are steel.

The Young's modulus for steel is E = 210 GPa

The longitude is given. (But note that in a real application you have to consider the length up to the nut.)

The section is (using the nominal diameter of 10 mm)

A = \frac{\pi * d^2}{4} = \frac{\pi * 0.01^2}{4} = 7.85e-5 m^2

Then:

k  = 2.1e11 * \frac{7.85e-5}{0.06} = 275e6 Pa = 275 MPa

5 0
3 years ago
Other questions:
  • A PMOS device with VT P = −1.2 V has a drain current iD = 0.5 mA when vSG = 3 V and vSD = 5 V. Calculate the drain current when:
    12·1 answer
  • (Practice work, not graded)
    11·1 answer
  • What type of footwear protects your toes from falling objects and being crushed?
    6·2 answers
  • Rain falls on a 1346 acre urban watershed at an intensity of 1.75 in/hr for a duration of 1 hour. The catchment land use is 20%
    10·1 answer
  • A 150-lbm astronaut took his bathroom scale (a spring scale) and a beam scale (compares masses) to the moon where the local grav
    13·1 answer
  • Burn in hell i watched your stupid video and i still could not get the answer
    14·1 answer
  • A rectangular open channel is 20 ft wide and has a bed slope of 0.007. Manning's roughness coefficient n is 0.03. It is in unifo
    10·1 answer
  • What is the role of the architects in modern development​
    15·1 answer
  • Eugene runs a company that manufactures bricks. The manufacturing process consumes a lot of energy and causes pollution, which t
    9·2 answers
  • A brake caliper is considered a suspension item.<br> True<br> False
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!