Data Structures: Question Set – 14
What does Merge Sort mean?
Merge Sort is a divide-and-conquer algorithm that involves recursively dividing the input array into smaller sub-arrays until they become trivially small (i.e., one element or none), and then merging them back together in a sorted manner.
What is the Merge Sort’s time complexity, and why?
Merge Sort has an O(nlog(n)) time complexity, where n is the array’s total number of items. This is due to the algorithm’s recursive division of the input array into half, which results in each sub-array having just one element. Afterwards, two sorted arrays of size n/2 are combined in the merging stage, which requires O(n) time. Merge Sort’s overall time complexity is O(nlog(n)) since the division step needs O(log(n)) time to complete in order to reach the base case.
What distinguishes Merge Sort from other sorting algorithms?
Merge Sort is superior to alternative sorting algorithms in a number of ways, including:
It is the best choice for comparison-based sorting algorithms because of its worst-case time complexity of O(n*log(n)).
Since it maintains the relative order of like elements in the input array, it is a stable sorting algorithm.
It can be easily implemented to take advantage of multi-core processors and remote computing because it is a parallelizable method. The merging phase needs O(n) extra space, however this is typically a minor price to pay for the advantages it provides.
What steps comprise the Merge Sort process?
Merge Sort involves the following steps:
The input array should be split in half.
Apply Merge Sort iteratively to each of the halves until trivially small (i.e., one element or none).
Reconstruct the two sorted sub-arrays as a single sorted array.
How does Merge Sort ensure that the final merged array is sorted?
Merge Sort ensures that the final merged array is sorted by comparing the first elements of the two sub-arrays being merged and selecting the smaller one to be the next element in the merged array. This process is repeated until all elements from both sub-arrays have been merged into the final array. Since each sub-array is already sorted, this process guarantees that the merged array is also sorted.
Merge Sort is an algorithm for in-place sorting, right?
No. Because the intermediate arrays used in the merging step demand O(n) more storage space, Merge Sort is not an in-place sorting method.
What is Quick Sort?
Quick Sort is a popular sorting algorithm used to sort arrays or lists in ascending or descending order. It works by dividing an array into two sub-arrays, where one contains all the elements less than a pivot value and the other contains all the elements greater than the pivot value. The process is then repeated recursively on the sub-arrays until the entire array is sorted.
What is the time complexity of Quick Sort?
The time complexity of Quick Sort is O(nlogn) in the average case and O(n^2) in the worst case. The worst case occurs when the pivot element chosen is the smallest or largest element in the array, resulting in unbalanced partitions.
How does Quick Sort choose a pivot element?
Quick Sort chooses a pivot element by selecting an element from the array, usually the first or last element, and partitioning the array around that element.
What is the partitioning process in Quick Sort?
The partitioning process in Quick Sort involves selecting a pivot element, and then rearranging the elements of the array such that all the elements less than the pivot are on the left side of the pivot, and all the elements greater than the pivot are on the right side of the pivot.