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
A cylindrical specimen of this alloy 12 mm in diameter and 188 mm long is to be pulled in tension. Assume a value of 0.34 for Po
kicyunya [14]

This question is incomplete, the missing image in uploaded along this answer below.

Answer:

The required stress is 200 Mpa

Explanation:

Given the data in the question;

diameter D = 12 mm = 12 × 10⁻³ m

Length L = 188 mm = 188 × 10⁻³ m

Poisson's ratio v = 0.34

Reduction in diameter Δd = 0.0105 mm = 0.0105 × 10⁻³ m

The transverse strain will;

εˣ = Δd / D

εˣ = -0.0105 × 10⁻³ /  12 × 10⁻³ m

εˣ = -0.00088

The longitudinal strain will be;

E^z = - ( εˣ  / v )

E^z = - ( -0.00088  / 0.34 )

E^z = - ( - 0.002588 )

E^z = 0.0026

Now, Using the values for strain, we get the value of stress from the graph provided in the question, ( first image uploaded below.

From the graph, in the Second image;

The stress is 200 Mpa

Therefore, The required stress is 200 Mpa

8 0
2 years ago
A converging-diverging nozzle has an area ratio of 5.9. (1) Determine the (P0/Pt) values corresponding to the 1st, 2nd, and 3rd
nata0808 [166]

Answer:

Check the explanation

Explanation:

The Total pressure is the overall of fixed or static pressure p, the dynamic pressure q, as well as gravitational head. Total pressure can also be referred to as the measure of the overall energy of the airstream, and is the same to static pressure plus velocity pressure.

kindly check the step by step solution in the attached image below to Determine the (P0/Pt) values corresponding to the 1st, 2nd, and 3rd critical points.

5 0
3 years ago
Think about how could you design, build, and test a light maze. What specific behavior of light will be essential to the success
Mrac [35]

Answer:

Reflection

Explanation:

The specific behavior of light that will be essential to ensure the success of your design is "Reflection". This is because light maze makes use of a mirror and it's the light that is reflected that we see with our eyes. Also, the manner in which light is reflected off objects will affect the colors that are reflected as well.

4 0
3 years ago
What type of test can show a chemical engineer if a material remains in a system and accumulates or if it moves right through?
UkoKoshka [18]

A chemical engineer can clearly see from this kind of test if a substance stays in a system and builds up or if it just passes through.

<h3>What is a chemical engineer?</h3>
  • Processes for manufacturing chemicals are created and designed by chemical engineers.
  • To solve issues involving the manufacture or usage of chemicals, fuel, medications, food, and many other goods, chemical engineers use the concepts of chemistry, biology, physics, and math.
  • A wide range of sectors, including petrochemicals and energy in general, polymers, sophisticated materials, microelectronics, pharmaceuticals, biotechnology, foods, paper, dyes, and fertilizers, have a significant demand for chemical engineers.
  • Chemical engineering is undoubtedly difficult because it requires a lot of physics and math, as well as a significant number of exams at the degree level.

To learn more about chemical engineer, refer to:

brainly.com/question/23542721

#SPJ4

7 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
Other questions:
  • Write a program that removes all spaces from the given input. You may assume that the input string will not exceed 50 characters
    9·2 answers
  • A square aluminum plate 5 mm thick and 200 mm on a side is heated while vertically suspended in quiescent air at 40C. (A) Determ
    8·1 answer
  • A 0.25in diameter steel rod BC is securely attached between two identical 1in diameter copper rods (AB and CD). Find the torque
    11·1 answer
  • If 100 J of heat is added to a system so that the final temperature of the system is 400 K, what is the change in entropy of the
    5·1 answer
  • Heat is transferred at a rate of 2 kW from a hot reservoir at 800 K to a cold reservoir at 300 K. Calculate the rate at which th
    8·1 answer
  • Turn the motor around in the circuit. What happens?
    12·1 answer
  • Question 3 (5 points)
    7·1 answer
  • The housing for a certain machinery product is made of two components, both aluminum castings. The larger component has the shap
    10·1 answer
  • Who had launched the highest number of internet satellites as of March 2020?
    14·1 answer
  • How many meters per second is 100 meters and 10 seconds
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!