Question

In: Computer Science

Mergesort is known to be one of the fastest algorithms for sorting lists of data. In...

Mergesort is known to be one of the fastest algorithms for sorting lists of data. In fact, the runtime of mergesort is proportional to n· logn where n is the size of the list to be sorted. Bubblesort, on the other hand is a much slower algorithm, since it's runtime is proportional to n2 , where n is the size of the list to be sorted.

As an experiment, mergesort is run on a slow computer, bubblesort is run on a fast computer and the times taken by each algorithm are recorded when sorting a list of size 10,000. We are surprised to find that the two runtimes are identical. In other words, the faster computer appears to have compensated for the slowness of bubblesort, relative to mergesort. To see whether this result remains true for bigger input sizes, we will redo the experiment, but use a list of size one hundred million, rather than ten thousand. After recording the new runtimes we will compute the ratio r = (new runtime of bubblesort) /  (new runtime of mergesort).

a) What should we predict the value of r to be? _________________

Your answer should be a whole number. For example, you would answer r = 1, if you think the algorithms still take the same amount of time. Or you would answer r = 2, if you think bubblesort takes twice as long a mergesort, and so on.

b) Justify your answer in Part a using runtime rules of thumb that we discussed in lecture.  (A detailed justification is needed here, involving some arithmetic.)

Solutions

Expert Solution

a)r=2
as this time the number of elements is way larger and fast computer wont compensate the large amount of steps added. this time.

===========================================================================

b)In case of bubble sort its complexity is O(n^2) that is if there are 10 elements to be sorted it would take 100 steps to sort the list.
but in case of merge sort its complexity is O(n*logn) that is in case of 10 elements it would take 10 steps to sort the list.
Bubble sort works best for small data elements but in case of large data it fails due to complexity of O(n^2) due to which there are lot of comparisons to be made.
In this case of having elements of one hundred million the steps in bubble sort will be twice of 100 million but it will be way less in merge sort and also merge sort runtime is not affected as the number of elements are larger or not.
But as bubble sort is being run on the fast computer which showed results same for 10000 elements as in case of 10000 elements number of steps in merge sort will be 40000 and in bubble sort it will be 10^8 the fast computer have compensated the loss of bubble sort and the runtime ratio will be 1 but now in case of 100 million, if we find out the complexity of bubble sort then O(n^2) then it will come to be 10^16 and in case of merge sort the complexity O(n.logn) the number comes out to be 800 million the steps are too much in case of bubble sort and if we compute the ratio of bubble sort and merge sort then it goes way beyond the charts in case of 100 million list elements and the fast computer will not be able to compensate this time and steps difference will be way larger to be compensated and thus, it can be concluded that bubble will take twice the time than the merge sort and runtime ratio will be 2.


Related Solutions

Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the...
Assume your sorting algorithms have to deal with lists that can potentially contain duplicates. Assume the same sorting algorithms as discussed in class / in the textbook. (a) What is the running time of Insertion Sort when all elements in a given list of length N are equal? Explain your answer. (b) Give a Θ-bound on the running time of Mergesort for the case that all elements in a given list of length N are equal (assuming N is a...
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
Each year lists of the fastest-growing and fastest-dying industries in the United States are published by...
Each year lists of the fastest-growing and fastest-dying industries in the United States are published by various sources. Do some research to find three industries from each of these two lists for the current year and briefly explain why you think these industries are either growing or dying. For each industry, explain what is likely happening in capital markets and what the likely effect will be on the industry long-run average cost curve. Updated answer needed please.
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms....
Write a C++ program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions.Counter for...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You...
Write a program to compare those two searching algorithms and also compare two sorting algorithms. You need to modify those codes in the book/slides to have some counters to count the number of comparisons and number of swaps. In the main function, you should have an ordered array of 120 integers in order to test searching algorithms, and the other two identical arrays of 120integers not in order to test sorting algorithms. Display all counters in the main functions. Counter...
Description: The goal of this assignment is to compare the empirical complexity of two sorting algorithms:...
Description: The goal of this assignment is to compare the empirical complexity of two sorting algorithms: a) Heap sort and b) Radix sort. Instructions: - Implement the above two sorting algorithms using Java or any other programming language. - Repeatedly generate random input instances containing 10, 50, 100, 500, 1000, 5000, 10000, 15000, … 50 000. The generated numbers must be between 0 and 100. - Execute both algorithms to sort the randomly generated arrays. - Compare the running time...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative...
Task: Write a program that implements several sorting algorithms and use it to demonstrate the comparative performance of the algorithms for a variety of datasets. Background The skeleton program sorting.cpp contains a main function for testing the operation of several sort algorithms over various data sizes and dataset organisations. The program understands the following arguments: -i insertion sort -s selection sort (default) -q quicksort -a (already) sorted dataset -v reverse-sorted dataset -r random dataset (default) -n no sorting x generate...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the...
C++ Analysis of Sorting Algorithms Design a class AbstractSort that can be used to analyze the number of comparisons performed by a sorting algorithm. The class should have a member function compare that is capable of comparing two array elements, and a means of keeping track of the number of comparisons performed. The class should be an abstract class with a pure virtual member function void sort(int arr[ ], int size) which, when overridden, will sort the array by calling...
Problem Description Objective This practical will test your knowledge on sorting and searching algorithms. In this...
Problem Description Objective This practical will test your knowledge on sorting and searching algorithms. In this practical, you will be implementing a number of algorithms. Each of these algorithms will be constructed in a different class. You should make sure that you know the complexity of the algorithms you implement. Design Think of how you are going to solve the problem and test your implementation with the test cases you designed based on the stages below. Testing Hint: it’s easier...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT