Which of the following is stable sort
Merge sort
Sorting algorithms arrange elements in a list based on a specific order. A key property some sorting algorithms have is called stability.
A sorting algorithm is considered stable if it preserves the relative order of equal elements. This means if two elements have the same value, their original order in the unsorted list is maintained in the sorted list.
Let's consider an example to illustrate stability. Suppose we have a list of pairs where the first element is the value and the second is an original index to show their initial positions: [(3, 1), (1, 2), (2, 3), (3, 4)]. If we sort this list based on the value, a stable sort would produce [(1, 2), (2, 3), (3, 1), (3, 4)], where the two elements with value 3 remain in their original order (the one with original index 1 comes before the one with original index 4). An unstable sort might produce [(1, 2), (2, 3), (3, 4), (3, 1)], changing the relative order of the equal elements.
Let's examine the stability property of each sorting algorithm provided in the options:
Based on the analysis, Merge sort is the stable sorting algorithm among the given options.
| Sorting Algorithm | Stable? |
|---|---|
| Quick Sort | No |
| Heap Sort | No |
| Merge Sort | Yes |
| Selection Sort | No |
| Algorithm | Average Time Complexity | Worst-Case Time Complexity | Space Complexity | Stable? |
|---|---|---|---|---|
| Bubble Sort | O(n2) | O(n2) | O(1) | Yes |
| Insertion Sort | O(n2) | O(n2) | O(1) | Yes |
| Selection Sort | O(n2) | O(n2) | O(1) | No |
| Merge Sort | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n2) | O(log n) to O(n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(1) | No |
| Counting Sort | O(n + k) | O(n + k) | O(k) | Yes |
| Radix Sort | O(nk) or O(n logb k) | O(nk) or O(n logb k) | O(n + k) or O(k) | Yes (depending on implementation) |
Beyond stability, sorting algorithms can be characterized by other properties:
Understanding these properties helps in choosing the most appropriate sorting algorithm for a given task.
You are given a sequence of n elements to sort. The input sequence consists of n/k subsequences, each containing k elements. The elements in a given subsequence are all smaller than the elements in the succeeding subsequence and larger than the elements in the preceding subsequence. Thus, all that is needed to sort the whole sequence of length n is to sort the k elements in each of the n/k subsequences.
The lower bound on the number of comparisons needed to solve this variant of the sorting problem is :
Merge sort uses:
For standard LSD radix sort of n keys, each represented by k digits in a fixed base, using stable counting sort for every digit, what is the tight best-case time complexity?
Which of the following algorithm design approach is used in Quick sort algorithm?
Arrange the following items in ascending order using Bubble Sort. What is the intermediate sequence of 37, 54, 21, 85, 68, 12, 9, and 57 after the second pass?