Question

In: Computer Science

Write code in your “main” function that performs the following: a) For each n є {10^3,...

Write code in your “main” function that performs the following:

a) For each n є {10^3, 5x10^3, 10^4, 5x10^4, 10^5}, randomly generate 5 integer arrays of length n. b) Run each of the five comparison sorts you implemented in Step 1 on all the five arrays generated in Step 2.a and record the worst-case actual running time and number of comparisons performed among elements in the input array.

#include<iostream>
#include<cmath>


using namespace std;

void displayArray(int a[], int n)

{

cout<<"\nArray once it’s sorted: "< for(int i=0;i cout< cout<

}

void selectionSort(int a[], int n)

{

for(int i=0;i

{
int min=i;
for(int j=i+1;j if(a[min]>a[j])
min=j;
int temp=a[min];
a[min]=a[i];
a[i]=temp;
}

displayArray(a,n);   
}

void insertionSort(int a[], int n)

{

for(int i=1;i

{
int min=a[i];
int j=i-1;


while(j>=0 && a[j]>min)

{
a[j+1]=a[j];        //shifting
--j;

}
}
displayArray(a,n);
}

void shellSort(int a[], int n)

{

int k= floor(log2(n));
int gap=pow(2,k)-1;


while(gap>=1)

{

for(int i=gap;i

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

a[j]=temp;
}

k--;
gap=pow(2,k)-1; //decrement gap

}

displayArray(a,n);
}

void swap(int* a, int* b)

{
int t=*a;
*a=*b;
*b=t;
}

int partition (int a[], int low, int high)

{
int pivot=a[high];
int i=(low-1);

for(int j=low;j<=high-1;j++)
{

if(a[j] {
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i + 1], &a[high]);
return (i+1);
}

void quickSort(int a[], int low, int high)

{

if (low

{

int pi=partition(a, low, high);

quickSort(a, low, pi-1);
quickSort(a, pi+1, high);

}

}
void merge(int a[], int l, int m, int r)

{

int i, j, k;
int n1=m-l+1;
int n2=r-m;

int L[n1], R[n2];

for (i=0;i L[i]=a[l+i];
for (j=0;j R[j]=a[m+1+j];

i=0;
j=0;
k=l;

while(i

{

if(L[i]<=R[j])

{

a[k]=L[i];
i++;
}
else

{

a[k]=R[j];
j++;

}
    k++;

}


while(i

{

a[k]=L[i];

i++;

k++;
}


while(j

{
a[k]=R[j];
j++;
k++;
}

}


void mergeSort(int a[], int l, int r)

{

if(l

{

int m=l+(r-l)/2;


mergeSort(a, l, m);
mergeSort(a, m+1, r);

merge(a, l, m, r);
}

}

int main()

{

int n;
cout<<"Please enter the total number of elements: ";
cin>>n;
int a[n];
cout<<"Enter your elements:"< for(int i=0;i cin>>a[i];

cout<<"Pick a sorting algorith: "< cout<<"1-Selection sort"< cout<<"2-Insertion sort"< cout<<"3-Shell sort"< cout<<"4-Quick sort"< cout<<"5-Merge sort"<

int choice=0;
cout<<"Enter your choice 1 - 5: ";
cin>>choice;

switch(choice){

case 1:selectionSort(a,n);
break;
case 2:insertionSort(a,n);
break;
case 3:shellSort(a,n);
break;
case 4:quickSort(a,0,n-1);
displayArray(a,n);
break;
case 5:mergeSort(a,0,n-1);
displayArray(a,n);
break;
default:cout<<"Invalid Option"<

}

return 0;
}

Solutions

Expert Solution

#include<iostream>
#include<cmath>


using namespace std;

void displayArray(int a[], int n)

{

cout<<"\nArray once it’s sorted: "< for(int i=0;i cout< cout<

}

void selectionSort(int a[], int n)

{

for(int i=0;i

{
int min=i;
for(int j=i+1;j if(a[min]>a[j])
min=j;
int temp=a[min];
a[min]=a[i];
a[i]=temp;
}

displayArray(a,n);   
}

void insertionSort(int a[], int n)

{

for(int i=1;i

{
int min=a[i];
int j=i-1;


while(j>=0 && a[j]>min)

{
a[j+1]=a[j];        //shifting
--j;

}
}
displayArray(a,n);
}

void shellSort(int a[], int n)

{

int k= floor(log2(n));
int gap=pow(2,k)-1;


while(gap>=1)

{

for(int i=gap;i

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

a[j]=temp;
}

k--;
gap=pow(2,k)-1; //decrement gap

}

displayArray(a,n);
}

void swap(int* a, int* b)

{
int t=*a;
*a=*b;
*b=t;
}

int partition (int a[], int low, int high)

{
int pivot=a[high];
int i=(low-1);

for(int j=low;j<=high-1;j++)
{

if(a[j] {
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i + 1], &a[high]);
return (i+1);
}

void quickSort(int a[], int low, int high)

{

if (low

{

int pi=partition(a, low, high);

quickSort(a, low, pi-1);
quickSort(a, pi+1, high);

}

}
void merge(int a[], int l, int m, int r)

{

int i, j, k;
int n1=m-l+1;
int n2=r-m;

int L[n1], R[n2];

for (i=0;i L[i]=a[l+i];
for (j=0;j R[j]=a[m+1+j];

i=0;
j=0;
k=l;

while(i

{

if(L[i]<=R[j])

{

a[k]=L[i];
i++;
}
else

{

a[k]=R[j];
j++;

}
    k++;

}


while(i

{

a[k]=L[i];

i++;

k++;
}


while(j

{
a[k]=R[j];
j++;
k++;
}

}


void mergeSort(int a[], int l, int r)

{

if(l

{

int m=l+(r-l)/2;


mergeSort(a, l, m);
mergeSort(a, m+1, r);

merge(a, l, m, r);
}

}

int main()

{

int n;
cout<<"Please enter the total number of elements: ";
cin>>n;
int a[n];
cout<<"Enter your elements:"< for(int i=0;i cin>>a[i];

cout<<"Pick a sorting algorith: "< cout<<"1-Selection sort"< cout<<"2-Insertion sort"< cout<<"3-Shell sort"< cout<<"4-Quick sort"< cout<<"5-Merge sort"<

int choice=0;
cout<<"Enter your choice 1 - 5: ";
cin>>choice;

switch(choice){

case 1:selectionSort(a,n);
break;
case 2:insertionSort(a,n);
break;
case 3:shellSort(a,n);
break;
case 4:quickSort(a,0,n-1);
displayArray(a,n);
break;
case 5:mergeSort(a,0,n-1);
displayArray(a,n);
break;
default:cout<<"Invalid Option"<

}

return 0;
}


Related Solutions

Implement each of the following functions and write a basic main() function that tests each. For...
Implement each of the following functions and write a basic main() function that tests each. For convenience, you should be able to find this starter class under Mimir's assignment 4 starter code. Do not change the name, parameters, or returns of any of these functions or of the name of the class itself. There is also no need in this assignment for any global variables. You are strongly encouraged to use your solution for some of these functions in others...
Write a C code to let the main thread create N child threads, where each created...
Write a C code to let the main thread create N child threads, where each created thread will randomly generate an integer between 0 to 10 and put it into a global array variable. After that, the main thread will calculate the sum of all the generated integers. N should be input as a command line argument. Complete the following C code by filling all “???”s in the code sketch. NOTE: when you compile the code, you need to add...
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
(10 pts) Write a function called isInBetween that takes 3 ints, say n, low and high,...
(10 pts) Write a function called isInBetween that takes 3 ints, say n, low and high, and returns a boolean; the function returns true if the first parameter (n) is between the second and third parameters (low and high, INCLUSIVE), and false otherwise. Can safely assume that low is lower than high.
Write the following function and provide a program to test it (main and function in one...
Write the following function and provide a program to test it (main and function in one .py) def repeat(string, n, delim) that returns the string string repeated n times, separated by the string delim. For example repeat(“ho”, 3, “,”) returns “ho, ho, ho”. keep it simple.Python
Hemoglobin is the main protein in red blood cells and performs the function of transporting oxygen....
Hemoglobin is the main protein in red blood cells and performs the function of transporting oxygen. Oxygen is combined into the hemoglobin's prostatic group, heme molecule. A. Describe the changes in the quadratic structure of hemoglobin with oxygen binding and explain the role of the 2,3-BPG. B. Describe the reasons for oxygen deficiency in the highlands and the body's action in adapting to high-altitude hypoxic environments. C. Describe the bohr effect that affects the oxygenation of hemoglobin.
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
Money performs its function in three ways. For each of the following situations, identify and explain...
Money performs its function in three ways. For each of the following situations, identify and explain the ONE unique and particular function of money that is being emphasized: John expects to be laid off in two months. He needs to have access to a rainy day fund and decides to increase the balance in his savings account. Jeremiah wants to compare the value of movie tickets and DVDs, and checks the price of each of these goods as quoted in...
Q1. Write an application that performs each of the following tasks: a) Specify that class “Pieceworker”...
Q1. Write an application that performs each of the following tasks: a) Specify that class “Pieceworker” inherits from class Employee. b) Create instance variables “Firstname” and “ID” in superclass and a toString to display. c) Add “branch” and toString in PieceWorker. d) Show overriding to display the variables of the subclass. Q2 Draw an inheritance hierarchy for students at a university. Use Student as the superclass of the hierarchy, and then extend Student with classes Undergraduate Student and Graduate Student....
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n...
Consider the following C code that outlines Fibonacci function int fib (int n) { if (n == 0) return 0; else if (n==1) return 1; else return fib(n-1) + fib (n-2); } For this programming assignment, write and test an ARMv8 program to find Fibonacci (n). You need to write a main function that calls the recursive fib function and passes an argument n. The function fib calls itself (recursively) twice to compute fib(n-1) and fib (n-2). The input to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT