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
An air conditioner removes heat steadily from a house at a rate of 750 kJ/min while drawing electric power at a rate of 6 kW. De
Paraphin [41]

Answer:

a. 2.08, b. 1110 kJ/min

Explanation:

The power consumption and the cooling rate of an air conditioner are given. The COP or Coefficient of Performance and the rate of heat rejection are to be determined. <u>Assume that the air conditioner operates steadily.</u>

a. The coefficient of performance of the air conditioner (refrigerator) is determined from its definition, which is

COP(r) = Q(L)/W(net in), where Q(L) is the rate of heat removed and W(net in) is the work done to remove said heat

COP(r) = (750 kJ/min/6 kW) x (1 kW/60kJ/min) = 2.08

The COP of this air conditioner is 2.08.

b. The rate of heat discharged to the outside air is determined from the energy balance.

Q(H) = Q(L) + W(net in)

Q(H) = 750 kJ/min + 6 x 60 kJ/min = 1110 kJ/min

The rate of heat transfer to the outside air is 1110 kJ for every minute.

5 0
3 years ago
A nozzle receives an ideal gas flow with a velocity of 25 m/s, and the exit at 100 kPa, 300 K velocity is 250 m/s. Determine the
Margaret [11]

Given Information:

Inlet velocity = Vin = 25 m/s

Exit velocity = Vout = 250 m/s

Exit Temperature = Tout = 300K

Exit Pressure = Pout = 100 kPa

Required Information:

Inlet Temperature of argon = ?

Inlet Temperature of helium = ?

Inlet Temperature of nitrogen = ?

Answer:

Inlet Temperature of argon = 360K

Inlet Temperature of helium = 306K

Inlet Temperature of nitrogen = 330K

Explanation:

Recall that the energy equation is given by

$ C_p(T_{in} - T_{out}) = \frac{1}{2} \times (V_{out}^2 - V_{in}^2) $

Where Cp is the specific heat constant of the gas.

Re-arranging the equation for inlet temperature

$ T_{in}  = \frac{1}{2} \times \frac{(V_{out}^2 - V_{in}^2)}{C_p}  + T_{out}$

For Argon Gas:

The specific heat constant of argon is given by (from ideal gas properties table)

C_p = 520 \:\: J/kg.K

So, the inlet temperature of argon is

$ T_{in}  = \frac{1}{2} \times \frac{(250^2 - 25^2)}{520}  + 300$

$ T_{in}  = \frac{1}{2} \times 119  + 300$

$ T_{in}  = 360K $

For Helium Gas:

The specific heat constant of helium is given by (from ideal gas properties table)

C_p = 5193 \:\: J/kg.K

So, the inlet temperature of helium is

$ T_{in}  = \frac{1}{2} \times \frac{(250^2 - 25^2)}{5193}  + 300$

$ T_{in}  = \frac{1}{2} \times 12  + 300$

$ T_{in}  = 306K $

For Nitrogen Gas:

The specific heat constant of nitrogen is given by (from ideal gas properties table)

C_p = 1039 \:\: J/kg.K

So, the inlet temperature of nitrogen is

$ T_{in}  = \frac{1}{2} \times \frac{(250^2 - 25^2)}{1039}  + 300$

$ T_{in}  = \frac{1}{2} \times 60  + 300$

$ T_{in}  = 330K $

Note: Answers are rounded to the nearest whole numbers.

5 0
3 years ago
Those in this career install, maintain, and repair electrical wiring,
Ad libitum [116K]
I’m pretty sure it’s C and G
8 0
3 years ago
Read 2 more answers
Which statement about tensile stress is true? A. Forces that act perpendicular to the surface and pull an object apart exert a t
svp [43]

Answer:

A. Forces that act perpendicular to the surface and pull an object apart exert a tensile stress on the object.

Explanation:

Tensile stress is referred as a deforming force, in which force acts perpendicular to the surface and pull an object apart, attempting to elongate it.

The tensile stress is a type of normal stress, in which a perpendicular force creates the stress to an object’s surface.

Hence, the correct option is "A."

3 0
3 years ago
To measure the voltage drop across a resistor, a _______________ must be placed in _______________ with the resistor. Ammeter; S
gulaghasi [49]

Answer:

The given blanks can be filled as given below

Voltmeter must be connected in parallel

Explanation:

A voltmeter is connected in parallel to measure the voltage drop across a resistor this is because in parallel connection, current is divided in each parallel branch and voltage remains same in parallel connections.

Therefore, in order to measure the same voltage across the voltmeter as that of the voltage drop across resistor, voltmeter must be connected in parallel.

4 0
3 years ago
Other questions:
  • Is air conditioner a refrigerator?
    10·1 answer
  • An electric field is expressed in rectangular coordinates by E = 6x2ax + 6y ay +4az V/m.Find:a) VMN if point M and N are specifi
    9·1 answer
  • Lately, you have noticed some repetitive stress in your wrist. Which sign is most likely the cause of that stress and pain?
    7·1 answer
  • The air velocity in the duct of a heating system is to be measured by a Pitot-static probe inserted into the duct parallel to th
    13·1 answer
  • Which of the following refers to software designed to alter system files and utilities on a victim’s system with the intention o
    15·1 answer
  • A weighted, frictionless piston-cylinder device initially contains 5.25 kg of R134a as saturated vapor at 500 kPa. The container
    7·1 answer
  • A magnesium oxide component must not fail when a tensile stress of 14 MPa is applied. Determine the maximum allowable surface cr
    8·1 answer
  • (1) Estimate the specific volume in cm3 /g for carbon dioxide at 310 K and (a) 8 bar (b) 75 bar by the virial equation and compa
    10·1 answer
  • Multimeter and the LCD is showing Hz. What's she measuring?
    11·1 answer
  • Tech A says that some relays are equipped with a suppression diode in parallel with the winding. Tech B says that some relays ar
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!