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

C++ Assignment Make sure to implement proper coding conventions to receive full

ID: 3708194 • Letter: C

Question

C++ Assignment Make sure to implement proper coding conventions to receive full credit Execute each subtask and complete the analysis table below Make sure source code and output are clearly visible. Functions or classes are ok, the time analysis after the successful implementation of each sort is critical to receiving the full credit. Task 1: Functions or classes are ok Create an array that holds 1000 random floats between 1-1000 Implement Selection sort to sort the values in ascending order. Least ? Greatest Measure the time it takes to execute the sort in milliseconds. Please run the sort 3 times Task 2: Functions or classes are ok. Create an array that holds 1000 random floats between 1-1000 Implement Insertion sort to sort the values in ascending order. Least - Greatest Measure the time it takes to execute the sort in milliseconds Please run the sort 3 times Task 3: Functions or classes are ok Create an array that holds 1000 random floats between 1-1000 Implement Quick Sort to sort the values in ascending order. Least -Greatest Measure the time it takes to execute the sort in milliseconds. Please run the sort 3 times . Please attach Photos of Source Code and Output for the tasks. » Please write one source code that operates all of the three tasks in one program like the example given below.

Explanation / Answer

C++   CODE:

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int arr[10000], n;

void swap(int *xp, int *yp);

void in_sort(int *arr,int n);

void sel_sort(int *arr,int n);

int partition(int arr[], int m, int p);

void interchange(int arr[], int i, int j);

void QuickSort(int arr[], int p, int q);

int main()

{

int i;

time_t start, end,t;

time_t diff;

printf(" Enter the number of elements: ");

scanf("%d", &n);

srand((unsigned) time(&t));

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

{

arr[i]=rand()%n;

}

printf(" The elements are ");

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

{

printf("%d ", arr[i]);

}

clock_t tic = clock();

for(int m=0;m<3;m++){

QuickSort(arr, 0, n-1);

in_sort(arr,n);

sel_sort(arr,n);

}

clock_t toc = clock();

printf(" The sorted array is ");

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

printf("%d ", arr[i]);

printf("Elapsed: %f seconds ", (double)(toc - tic) / CLOCKS_PER_SEC);

return 0;

}

----

int partition(int arr[], int m, int p)

{

int v=arr[m];

int i=m;

int j=p;

do

{

do

i++;

while(arr[i]<v);

do

j--;

while(arr[j]>v);

if(i<j)

interchange(arr, i, j);

}while(i<=j);

arr[m]=arr[j];

arr[j]=v;

return j;

}

----------------

void interchange(int arr[], int i, int j)

{

int p;

p=arr[i];

arr[i]=arr[j];

arr[j]=p;

}

-----

void QuickSort(int arr[], int p, int q)

{

int j, k, temp;

if(p<q) //If there are more than 1 element, divide p into 2 sub-problems

{

k=rand() % (q-p+1)+p;

interchange(arr, k, p);

j=partition(arr, p, q+1); //j is the position of partioning element

QuickSort(arr, p, j-1);

QuickSort(arr, j+1, q);

}

}

void in_sort(int *arr,int n){

int i, key, j;

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

key = arr[i];

j = i-1;

greater than key, to one position ahead

of their current position */

while (j >= 0 && arr[j] > key){

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = key;

}

}

void sel_sort(int *arr,int n){

int i, j, min_idx;

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

min_idx = i;

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

if (arr[j] < arr[min_idx]){

min_idx = j;

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

}

}

}

}

void swap(int *xp, int *yp){

int temp = *xp;

*xp = *yp;

*yp = temp;

}