Question

In: Computer Science

merge sort Determine the best, average, and worst case inputs for merge sort.

merge sort

Determine the best, average, and worst case inputs for merge sort.

Solutions

Expert Solution

Answer:

Time complexity of Merge Sort is O(n*Log n) in all the 3 cases (worst, average and best) as merge sort always divides the array in two halves and takes linear time to merge two halves.

Explanation:

We recursively divide the input array of size n into two parts of sizes n/2.This is the divide step which just calculates the indices of the two sub-arrays A[0..n/2] and A[n/2…n].So this step takes constant time i.e Θ(1).

Now,in the conquer step,we have to sort the two sub arrays and merge them. So,the complexity for this step is accounted in solving further smaller sub-problems of sizes n/4,n/8… and ultimately 1.

In the combine step,we are combining the elements of the smaller sub-arrays. Adding all of them,the number of elements is n. So we have to merge n elements in sorted order which will take Θ(n) time.

Since the divide step takes constant time,we can consider both collectively as Θ(n).

So the recurrence relation for time taken by merge sort will be:

T(n) = 2T(n/2) + Θ(n)

There is a 2 T(n/2) term since we have 2 sub problems each of size n/2 and Θ(n) time is required for divide and combine steps.

We can solve the above recurrence relation using the second case of the master theorem which leads us to the complexity Θ(nlogn).

Please give thumbsup, if you like it. Thanks.


Related Solutions

Given the below code, compute the values of the best case, worst case and average case...
Given the below code, compute the values of the best case, worst case and average case knowing that the basic operation in the first loop takes 4 ns to execute and the basic operation in the second loop takes 5 ns to execute. int A[20]; for (int i=0; i < 20; i++) {           cin >> A[i]; } for (i=0; i < 20; i++) {           if (A[i] % 3 == 0)                     break; }
Analyze the worst-case, best-case, and average-case number of comparisons of sequential search if exactly three-tenths of...
Analyze the worst-case, best-case, and average-case number of comparisons of sequential search if exactly three-tenths of the time, the element x to search for is not in the list and if x is in the list, it is equally likely to be in any position.
Accountants are counted on to provide management with analyzing data to determine best- and worst-case scenarios....
Accountants are counted on to provide management with analyzing data to determine best- and worst-case scenarios. As future planning becomes more complex, these what-if analyses can increase in complexity and usefulness. Identify and discuss at least three (3) types of what-if analyses that an accountant should be able to perform to measure a firm’s performance over a period. Be sure to include the type of data that will be needed to support this analysis. Justify your response.
Accountants are counted on to provide management with analyzing data to determine best- and worst-case scenarios....
Accountants are counted on to provide management with analyzing data to determine best- and worst-case scenarios. As future planning becomes more complex, these what-if analyses can increase in complexity and usefulness. Identify and discuss at least three (3) types of what-if analyses that an accountant should be able to perform to measure a firm’s performance over a period. Be sure to include the type of data that will be needed to support this analysis. Justify your response.
prove the worst case of quick sort using subsetution, what is T(n) of quick sort and...
prove the worst case of quick sort using subsetution, what is T(n) of quick sort and what is the worst case for it
PYTHON Perform a series of benchmarking tests on merge-sort and quick-sort to determine which one is...
PYTHON Perform a series of benchmarking tests on merge-sort and quick-sort to determine which one is faster. Your tests should include sequences that are “random” as well as “almost” sorted.
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that...
USING JAVA Almost sort the array using Merge Sort. Perform Merge Sort as usual except that during the final merge, it is not necessary to merge all n elements, but only the elements in positions 1 to k.
20. The concept of best-, worst-, and average-case analyses extends beyond algorithms to other counting problems...
20. The concept of best-, worst-, and average-case analyses extends beyond algorithms to other counting problems in mathematics. Recall that the height of a binary tree is the number of edges in the longest path from the root to a leaf. (a) Find the best-case height of a binary tree with five nodes. (b) Find the worst-case height of a binary tree with five nodes. (c) Find the average-case height of a binary tree with five nodes. For this problem,...
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the...
The multi-merge sort algorithm (M&M sort) works like merge sort, except that instead of dividing the array into 2 partitions, it divides the array into p partitions. It recursively sorts each of the p partitions and then merges the results doing a p-way merge (except, of course, for the base case of a singleton or empty array). (a) Write the recurrence for T(n) for M&M sort for the case where p is a constant. Then, give a closed-form solution to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT