Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c++ please! The second picture is what pops up after clicking \"sample progra

ID: 3884279 • Letter: I

Question

In c++ please! The second picture is what pops up after clicking "sample program using include level" Sorting Program For this assignment, you are to write a comparative algorithm that will compare th efficiency of three sorting routines Here is the approach you should use: 1. Write a Bubble Sort function with a test program 2. Write a Shell Sort function with a test program. 3. Write a Quick Sort function (using stacks, rather than recursion) with a test program. . Write the main program (that will include the other 3 programs) Notes In the test programs, the logic should be as follows: Print the random numbers Sort the numbers keeping track of the costs Print the sorted numbers Print the cost Use INCLUDE LEVELin all of the programs, which includes the sorting routines and a test program. Sample program using include level) The main progr should have the following logic. Generate 20 random numbers Print the random numbers Move the original random numbers into another array Bubble Sort the other array Print the sorted numbers AND the cost of sorting the numbers Move the original random numbers into another array Shell Sort the other array Print the sorted numbers AND the cost of sorting the numbers Move the original random numbers into another array Quick Sort the other array Print the sorted numbers AND the cost of sorting the numbers To compute the cost, you assign values to every swap and every compare. A compare costs 1 unit, and a swap costs 6 units

Explanation / Answer

#include<iostream>

#include<vector>

#include<array>

#include<algorithm>

#include<map>

#include<set>

#include<hash_map>

#include<hash_set>

#include<list>

#include<queue>

#include<cmath>

#include<stack>

#include<deque>

#include<unordered_map>

#include<unordered_set>

#include<string>

#include<memory>

#include<thread>

#include<stdlib.h>

#define ull unsigned long long int

#define mod 1000000007

using namespace std;

void swap(int *xp, int *yp)

{

int temp = *xp;

*xp = *yp;

*yp = temp;

}

void bubbleSort(int arr[], int n, int *cost) {

int i, j;

for (i = 0; i < n - 1; i++) {

for (j = 0; j < n - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

swap(&arr[j], &arr[j + 1]);

*cost = *cost + 6;

}

*cost = *cost + 2;

}

*cost = *cost + 1;

}

}

int shellSort(int arr[], int n, int *cost)

{

for (int gap = n / 2; gap > 0; gap /= 2)

{

for (int i = gap; i < n; i += 1)

{

int temp = arr[i];

int j;

for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {

arr[j] = arr[j - gap];

*cost = *cost + 6 + 2;

}

arr[j] = temp;

*cost = *cost + 1 + 6;

}

*cost = *cost + 1;

}

return 0;

}

int partition(int arr[], int l, int h, int *cost)

{

int x = arr[h];

int i = (l - 1);

for (int j = l; j <= h - 1; j++)

{

if (arr[j] <= x)

{

i++;

swap(&arr[i], &arr[j]);

*cost = *cost + 6;

}

*cost = *cost + 1;

}

swap(&arr[i + 1], &arr[h]);

*cost = *cost + 6;

return (i + 1);

}

void quickSort(int arr[], int l, int h, int *cost)

{

int *stack = new int [h - l + 1];

int top = -1;

stack[++top] = l;

stack[++top] = h;

while (top >= 0)

{

h = stack[top--];

l = stack[top--];

int p = partition(arr, l, h,cost);

if (p - 1 > l)

{

stack[++top] = l;

stack[++top] = p - 1;

}

if (p + 1 < h)

{

stack[++top] = p + 1;

stack[++top] = h;

}

*cost = *cost + 2;

}

}

void print(int arr[], int n) {

int i = 0;

for (i = 0; i < n; i++)

cout << arr[i] << " ";

cout << endl;

}

int main() {

int i = 0;

int arr1[21], arr2[21], arr3[21], orig[21];

for (i = 0; i < 20; i++) {

orig[i] = rand() % 100;

}

cout << "Random Numbers : ";

print(orig, 20);

int cost = 0;

for (i = 0; i < 20; i++)

arr1[i] = orig[i];

bubbleSort(arr1, 20, &cost);

cout << "After Bubble Sort : ";

print(arr1, 20);

cout << " Cost : " << cost << endl;

cost = 0;

for (i = 0; i < 20; i++)

arr2[i] = orig[i];

shellSort(arr2, 20, &cost);

cout << "After Shell Sort : ";

print(arr2, 20);

cout << " Cost : " << cost << endl;

cost = 0;

for (i = 0; i < 20; i++)

arr3[i] = orig[i];

quickSort(arr3, 0,20-1, &cost);

cout << "After Quick Sort : ";

print(arr3, 20);

cout << " Cost : " << cost << endl;

return 0;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote