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
!!!!!!!!!!!!!!PLEASE ANSWER THIS!!!!!!!!!!!!!!!!
dmitriy555 [2]

Answer:

age = int(input())

ticket = 100

total = 0

passengers = 1

while passengers <= 5:

if age < 3:

 continue

else:

 total += ticket

passengers += 1

print(total)

Explanation:

UuU

6 0
2 years ago
Help thank you &lt;3 :DDD
Vika [28.1K]

Answer:

Explanation:

its B

4 0
3 years ago
Choose the true statement from those shown below: A Merchant Account allows you to use SSL on your web site. Disadvantages of us
Slav-nsk [51]

Answer:

The answer is A. that is, a merchant account allows you to use SSL on your website.

Explanation:

SSL means Secure Sockets Layer and this is an encryption-based Internet security protocol.

For an e-commerce merchant website or account, it is advised that an SSL package be installed to prevent any potential loss of private information.

For this reason, a merchant account allows use of SSL on your website because this also boost the confidence of client and customers visiting the website to purchase products.

7 0
3 years ago
Given V1 = 8 vpp, R1 = 6 kΩ, V2 = 5 vpp, R2 = 3 kΩ, V3 = 10 vpp, R3 = 3 kΩ, and Rf = 14 kΩ, find Vout. Vout = vpp. (Round your a
iragen [17]

Answer:

Vout= 93.3V

Explanation:

For this question, consider circuit in the attachment 1.

This is the circuit of an inverting amplifier. In an inverting amplifier

Vout/Vin= -Rf/Rin

To calculate the Vout, we must find Rin and Vin. For this we must solve the input circuit (attachment 2)  using  Thevinine theorem. Thevnine theorem states that all voltage sources in a circuit can be replaced by an equivalent voltage source Veq and and all resistances can be replaced by an equivalent resistance Req. To find out Req all voltage sources must be short circuited (attachment 3)

1/Req= 1/R1+1/R2+1/R3

1/Req=1/6+1/3+1/3

Req=6/5

To find out Veq consider circuit in attachment 4. We will solve this circuit using nodal analysis. In nodal analysis, we use the concept that sum of currents entering a node is equal to the sum of currents leaving a node. So,

I1= I2+I3

(10-Veq)/6= (Veq-5)/3+(Veq-10)/3

Veq=8V

Now the input circuit can be simplified as shown in attachment 5. Solve for Vout using equation

Vout/Veq= -Rf/Req

Vout/8= -14/(6/5)

Vout= - 93.3

It is at an angle of 180° from Veq

7 0
3 years ago
You have a motor that runs at 1.5 amps from a 12 volt power source, how many watts of power is it using?
Leviafan [203]

Answer:

18 Watts

Explanation:

For this problem, we simply need to understand the relationship of power to voltage and current.  This relationship is derived from Ohm's law:

Power = Voltage * Current

Given this equation, we can say the following to find the power consumption of the motor:

Power = 12volts * 1.5amps

Power = 18 Watts

Hence, the motor is consuming 18 Watts of power.

Cheers.

3 0
3 years ago
Other questions:
  • A cable in a motor hoist must lift a 700-lb engine. The steel cable is 0.375in. in diameter. What is the stress in the cable?
    12·1 answer
  • A 10 wt % aqueous solution of sodium chloride is fed to an evaporative crystallizer which operates at 80o C. The product is a sl
    7·1 answer
  • Air flows through a 0.25-m-diameter duct. At the inlet the velocity is 300 m/s, and the stagnation temperature is 90°C. If the M
    8·1 answer
  • Consider the two wood pieces that are connected by a velcro as indicated below. The block is subjected to a tension force P and
    6·2 answers
  • Can some please help me with my questions. The Questions are in these two pictures below. I don't know how to do this question.
    14·1 answer
  • A cylindrical specimen of Aluminium having a diameter of 12.8 mm and gauge length of 50.8 is pulled in tension. Use the data giv
    5·1 answer
  • What does it mean if a manufacturing company "retools"?
    7·2 answers
  • An incompressible viscous fluid flows through a pipe with a flow rate of 1 mL/s. The pipe has a uniform diameter D0 and a length
    13·1 answer
  • What is a combination circuit? A combination circuit:
    6·1 answer
  • Sam’s father bought him a new car for $26,304.00. He expects to pay for it in equal monthly payments for 60 months. How much wil
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!