Question

write programs of insertion sort, and mergesort. Find the input size n, that mergesort starts to...

write programs of insertion sort, and mergesort. Find the input size n, that mergesort starts to beat insertion sort in terms of the worst-case running time. You can use clock_t function (or other time function for higher precision) to obtain running time. You need to set your input such that it results in the worst-case running time. Report running time of each algorithm for each input size n.

Homework Answers

Answer #1

We will be using clock_t function available into ctime header library in c++.clock() time depends on OS that is how OS allocate resources to process and that's why clock() time may be faster or slower than actual clock.

We will implement standard algorithm of merge sort and insertion sort. and will compare time taken by both algorithm.

For worst case input, its clear from algorithm of merge sort that all three cases (best,worst and average) time complexity is same so we just need to look for insertion sort's worst case, which is happened when input is sorted in reverse order. let's take input array as [10000,999,998,...,1] . 10000 to 1

Worst case time complexity for merge sort is O(nlogn)

Worst case time complexity for insertion sort O(n^2)

below is code for comparison. also I put screenshot of code and output. please note that clock time for merge sort is too less so its rounded up equal to zero.

//=====================================================================//

//c++ program for comparison of merge sort and insertion sort
#include <bits/stdc++.h>
using namespace std;

void merge_array(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;

/* creating temp arrays */
int L[n1], R[n2];
/* Copy data to temp arrays L[] and R[] */
for (i=0;i<n1;i++)
L[i]=arr[l+i];
for (j=0;j<n2;j++)
R[j]=arr[m+1+j];

/* Merging both temp arrays back into arr[l..r] in increasing order*/
i=0; // Initial index of left subarray
j=0; // Initial index of right subarray
k=l; // Initial index of merged subarray
while(i<n1&&j<n2)
{
if(L[i]<=R[j])
{
arr[k]=L[i];
i++;
}
else
{
arr[k]=R[j];
j++;
}
k++;
}
/* Copy the rest elements*/
while(i<n1)
{
arr[k]=L[i];
i++;
k++;
}
while(j<n2)
{
arr[k]=R[j];
j++;
k++;
}
return ;
}

/*standard implementation of merge sort */
void merge_sort(int arr[], int l, int r)
{
if (l < r)
{
int m = l+(r-l)/2;
// Sort both, first and second halves
merge_sort(arr, l, m);
merge_sort(arr, m+1, r);
merge_array(arr, l, m, r);
}
return ;
}

/*standard implementation of insertion sort */
void insertion_sort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i];
j = i - 1;

while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
return ;
}

int main()
{
int n = 10000; //size of input array
int tmp = 10000;
int arr[n];
/*creating array in decreasing order for worst case */
for(int i=0;i<10000;i++)
{
arr[i] = tmp;
tmp--;
}

clock_t insertion_time_req; //variable to store time required for insertion sort
clock_t merge_time_req; //variable to store time required for merge sort

insertion_time_req = clock();
insertion_sort(arr, n); //calling procedure for insertion sort
insertion_time_req = clock() - insertion_time_req;

/*same array as above */
tmp = 10000;
for(int i=0;i<10000;i++)
{
arr[i] = tmp;
tmp--;
}

merge_time_req = clock();
merge_sort(arr, 0, n-1); //calling procedure for merge sort
merge_time_req = clock() - merge_time_req;
//here we will type cast clock to second
cout << "Processor time taken for insertion sort : " << (float)insertion_time_req/CLOCKS_PER_SEC << " seconds" << endl;
cout << "Processor time taken for merge sort : " << (float)merge_time_req/CLOCKS_PER_SEC << " seconds" << endl;
return 0;
}
//=====================================================================//

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort...
Write a program to implement bubble sort, insertion sort, selection sort, merge sort and quick sort (pivot = first index) algorithms. Compute the CPU processing time for all the algorithms for varying input sizes as follows: N = 102, 103, 104, 105, and 106 Use a random number generator to generate the inputs. Obtain the inputs from the following input ranges: 1- 103, 1 - 106, 1 – 109, 1 - 1012 Write down your results as a table (with...
Suppose you can use a library that contains two familiar sorting functions: BUBBLESORT(A) and MERGESORT(A) Both...
Suppose you can use a library that contains two familiar sorting functions: BUBBLESORT(A) and MERGESORT(A) Both functions take as input an array A of length n and sort it. Their running times are O(n2) and O(n log n) respectively. The library also contains another sorting function, with the following pseudocode: - function STRANGESORT(A) - if (length(A)1000) then - return BUBBLESORT(A) - else - return MERGESORT(A) What is the worst-case running time of STRANGESORT? Please justify your answer.
Consider the following insertion sort algorithm. void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending...
Consider the following insertion sort algorithm. void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending order by insertion sort. { for (int k = 1; k < n; k++) { // At this point, a[0]..a[k-1] are already in order. // Insert a[k] where it belongs among a[0]..a[k]. You need to write code for this insertion as the body of the for-k loop. }//endfor k } a) Write the code for the body of the for-k loop to complete the...
void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending order by insertion sort. { for...
void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending order by insertion sort. { for (int k = 1; k < n; k++) { // At this point, a[0]..a[k-1] are already in order. // Insert a[k] where it belongs among a[0]..a[k]. You need to write code for this insertion as the body of the for-k loop. } //endfor k } a) Write the code for the body of the for-k loop to complete the insertion sort routine. b) Count...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers...
Implement functions for insertion sort, quicksort, heapsort and merge sort that input an array of integers and sort it in-place. Write a program that generates random integer arrays (hint: use seed appropriately to avoid generating same sequences) of lengths 10, 100, 1000, 10,000, 100,000, 1000,000, and then sorts each using each of the sorting functions from (a), and measures the time in nanoseconds. The program will repeat this process 30 times and will compute the average execution time for each...
Let A[1, . . . , n] be an array of n distinct numbers. If i...
Let A[1, . . . , n] be an array of n distinct numbers. If i < j and A[i] > A[j], then the pair (i, j) is called an inversion of A. 1. Which arrays with distinct elements from the set {1, 2, . . . , n} have the smallest and the largest number of inversions and why? State the expressions exactly in terms of n. 2. For any 0 < a < 1/2, construct an array for...
In this problem, you will write an implementation of BubbleSort. Your function should take in a...
In this problem, you will write an implementation of BubbleSort. Your function should take in a single line representing an array of integers, and output a single line containing the list in ascending order. For example, if you receive the following input followed by a newline: 8 7 6 5 4 3 2 1 then you should display the following output followed by a newline: 1 2 3 4 5 6 7 8 Starter code for reading the input and...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Write a C program that prompts the user to input as many unsigned(n) as the user...
Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number. For each number the user types, output whether that number is prime or not. You won't need...