MergeSort

MergeSort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merg() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes … More on Merge Sort
 

1
Question 1
Which of the following is not a stable sorting algorithm in its typical implementation.
A
Insertion Sort
B
Merge Sort
C
Quick Sort
D
Bubble Sort
Sorting    QuickSort    InsertionSort    MergeSort    
Discuss it


Question 1 Explanation: 
Question 2
Consider a situation where swap operation is very costly. Which of the following sorting algorithms should be preferred so that the number of swap operations are minimized in general?
A
Heap Sort
B
Selection Sort
C
Insertion Sort
D
Merge Sort
Sorting    SelectionSort    InsertionSort    MergeSort    
Discuss it


Question 2 Explanation: 

Selection sort makes O(n) swaps which is minimum among all sorting algorithms mentioned above.

Question 3
You have to sort 1 GB of data with only 100 MB of available main memory. Which sorting technique will be most appropriate?
A
Heap sort
B
Merge sort
C
Quick sort
D
Insertion sort
Sorting    QuickSort    MergeSort    HeapSort    
Discuss it


Question 3 Explanation: 
The data can be sorted using external sorting which uses merging technique. This can be done as follows: 1. Divide the data into 10 groups each of size 100. 2. Sort each group and write them to disk. 3. Load 10 items from each group into main memory. 4. Output the smallest item from the main memory to disk. Load the next item from the group whose item was chosen. 5. Loop step #4 until all items are not outputted. The step 3-5 is called as merging technique.
Question 4
In a modified merge sort, the input array is splitted at a position one-third of the length(N) of the array. What is the worst case time complexity of this merge sort?
A
N(logN base 3)
B
N(logN base 2/3)
C
N(logN base 1/3)
D
N(logN base 3/2)
Analysis of Algorithms    Sorting    MergeSort    
Discuss it


Question 4 Explanation: 
The time complexity is given by: T(N) = T(N/3) + T(2N/3) + N Solving the above recurrence relation gives, T(N) = N(logN base 3/2)
Question 5
Which sorting algorithm will take least time when all elements of input array are identical? Consider typical implementations of sorting algorithms.
A
Insertion Sort
B
Heap Sort
C
Merge Sort
D
Selection Sort
Sorting    SelectionSort    InsertionSort    MergeSort    
Discuss it


Question 5 Explanation: 
The insertion sort will take [Tex]\theta[/Tex](n) time when input array is already sorted.
Question 6
A list of n string, each of length n, is sorted into lexicographic order using the merge-sort algorithm. The worst case running time of this computation is (A) O (n log n (B) O (n^2 log n) (C) O (n^2 + log n) (D) O (n^2)
A
A
B
B
C
C
D
D
Analysis of Algorithms    Sorting    MergeSort    
Discuss it


Question 6 Explanation: 
The recurrence tree for merge sort will have height Log(n). And O(n^2) work will be done at each level of the recurrence tree (Each level involves n comparisons and a comparison takes O(n) time in worst case). So time complexity of this Merge Sort will be [Tex]O (n^2 log n) [/Tex].
Question 7
Which of the following sorting algorithms has the lowest worst-case complexity?
A
Merge Sort
B
Bubble Sort
C
Quick Sort
D
Selection Sort
Analysis of Algorithms    Sorting    SelectionSort    MergeSort    
Discuss it


Question 7 Explanation: 
Worst case complexities for the above sorting algorithms are as follows: Merge Sort — nLogn Bubble Sort — n^2 Quick Sort — n^2 Selection Sort — n^2
Question 8
Which of the following is true about merge sort?
A
Merge Sort works better than quick sort if data is accessed from slow sequential memory.
B
Merge Sort is stable sort by nature
C
Merge sort outperforms heap sort in most of the practical situations.
D
All of the above.
Sorting    QuickSort    MergeSort    HeapSort    
Discuss it


Question 8 Explanation: 
See Merge Sort and this.
Question 9
Assume that a mergesort algorithm in the worst case takes 30 seconds for an input of size 64. Which of the following most closely approximates the maximum input size of a problem that can be solved in 6 minutes?
A
256
B
512
C
1024
D
2048
Sorting    GATE-CS-2015 (Set 3)    MergeSort    
Discuss it


Question 9 Explanation: 
Time complexity of merge sort is Θ(nLogn)

c*64Log64 is 30
c*64*6 is 30
c is 5/64

For time 6 minutes

5/64*nLogn = 6*60

nLogn = 72*64 = 512 * 9

n = 512. 
Question 10
What is the best sorting algorithm to use for the elements in array are more than 1 million in general?
A
Merge sort.
B
Bubble sort.
C
Quick sort.
D
Insertion sort.
Sorting    QuickSort    InsertionSort    MergeSort    
Discuss it


Question 10 Explanation: 
Most practical implementations of Quick Sort use randomized version. The randomized version has expected time complexity of O(nLogn). The worst case is possible in randomized version also, but worst case doesn’t occur for a particular pattern (like sorted array) and randomized Quick Sort works well in practice. Quick Sort is also a cache friendly sorting algorithm as it has good locality of reference when used for arrays. Quick Sort is also tail recursive, therefore tail call optimizations is done.
There are 10 questions to complete.
1

 

Coding practice for sorting.

 

Company Wise Coding Practice    Topic Wise Coding Practice