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

c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort...
c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort Under the following scenarios for input data: 1. Uniform random 2. Almost sorted (90% sorted – 1 in 10 is out of place) 3. Reverse sorted On data of sizes 5,000, 10,000, … in increments of 5,000 up to …, 50,000 -Attach a screenshot of a program compilation below -Attach a screenshot of a successful program run below -Attach a graph (either line graph...
Sorting algorithms are one kind of algorithm whose performance may depend upon the data. Choose one...
Sorting algorithms are one kind of algorithm whose performance may depend upon the data. Choose one of the sorting algorithms or any other algorithm and explain whether the there are any differences in the best, average and worst cases. If there are no differences, explain why not. If there are differences, describe the data in the different cases and explain how the performance differs in each case.
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...
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.
Implementation of Quick sort and heap sorting algorithms in C++
Implementation of Quick sort and heap sorting algorithms in C++
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...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would...
Comparing (Sort Algorithms) Both of the two sorting algorithms will do "sort" on arrays which would contain x randomly generated integers where the value of x would be 10000, 20000, 40000 and 80000 (inputs). The parts of the program should be followed as..... 1. Set x = 10000, randomly generate x integers. Call qsort function to sort these integers and get the execution time. 2. Randomly generate another x integers. Call your own sorting algorithm and get the execution time....
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...
How would you show two linked lists are equal? (Java for Data Structures and Algorithms)
How would you show two linked lists are equal? (Java for Data Structures and Algorithms)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT