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

Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
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...
Fill in the code for the following C functions. Function srl performs a logical right shifting...
Fill in the code for the following C functions. Function srl performs a logical right shifting using arithmetic right shift (given by value xsra), followed by other operations not including right shifts or division. Function sra performs an arithmetic right shift using a logical right shift (given by value xsrl), followed by other operations not including right shift or division. you may use the computation 8*size of (int) to determine w, the number of bits in data type int. The...
Write the following assembly code file into C code. .globl main .text main:    # Tests...
Write the following assembly code file into C code. .globl main .text main:    # Tests simple looping behaviour    li t0, 60    li t1, 0 loop:    addi t1, t1, 5    addi t0, t0, -1    bne t1, t0, loop    bne t1, zero, success failure:    li a0, 0    li a7, 93 # a7 is what determines which system call we are calling and we what to call write (64)     ecall # actually issue...
(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.
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....
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.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT