Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist consists of a single element and merging those sublists in a manner that results into a sorted list.
The main advantage of the selection sort is that it performs well on a small list. The primary disadvantage of the selection sort is its poor efficiency when dealing with a huge list of items. Similar to the bubble sort, the selection sort requires n-squared number of steps for sorting n elements.
Difference between Selection, Bubble and Insertion Sort
| Selection | Bubble |
|---|
| Best case time complexity is O(n2) | Best case time complexity is O(n) |
| Works better than bubble as no of swaps are significantly low | Worst efficiency as too many swaps are required in comparison to selection and insertion |
| It is in-place | It is in-place |
Several common sorting algorithms are stable by nature, such as Merge Sort, Timsort, Counting Sort, Insertion Sort, and Bubble Sort. Others such as Quicksort, Heapsort and Selection Sort are unstable. External sorting is a class of sorting algorithms that can handle massive amounts of data. External sorting is required when the data being sorted do not fit into the main memory of a computing device (usually RAM) and instead they must reside in the slower external memory, usually a hard disk drive.
Space Complexity comparison of Sorting Algorithms
| Algorithm | Data Structure | Worst Case Auxiliary Space Complexity |
|---|
| Quicksort | Array | O(n) |
| Mergesort | Array | O(n) |
| Heapsort | Array | O(1) |
| Bubble Sort | Array | O(1) |
The only significant advantage that bubble sort has over most other algorithms, even quicksort, but not insertion sort, is that the ability to detect that the list is sorted efficiently is built into the algorithm. When the list is already sorted (best-case), the complexity of bubble sort is only O(n).
Merge Sort is a stable sort which means that the same element in an array maintain their original positions with respect to each other. Overall time complexity of Merge sort is O(nLogn). It is more efficient as it is in worst case also the runtime is O(nlogn) The space complexity of Merge sort is O(n).
Discussion Forum
| Que. | What is the best case efficiency of bubble sort in the improvised version? |
|---|
| b. | O(logn) |
| c. | O(n) |
| d. | O(n^2) |
| Answer:O(n) |
O(N) O(N) describes an algorithm whose performance will grow linearly and in direct proportion to the size of the input data set.
8. Which of the following sorting algorithms is the fastest for sorting small arrays? Explanation: Insertion sort is faster than simple sort for sorting small arrays.
An internal sort is any data sorting process that takes place entirely within the main memory of a computer. This is possible whenever the data to be sorted is small enough to all be held in the main memory. This issue has implications for different sort algorithms.
Discussion Forum
| Que. | Out of the following, the slowest sorting procedure is |
|---|
| b. | Heap Sort |
| c. | Shell Sort |
| d. | Bubble Sort |
| Answer:Bubble Sort |
Time Complexities of Sorting Algorithms:
| Algorithm | Best | Average |
|---|
| Bubble Sort | Ω(n) | Θ(n^2) |
| Merge Sort | Ω(n log(n)) | Θ(n log(n)) |
| Insertion Sort | Ω(n) | Θ(n^2) |
| Selection Sort | Ω(n^2) | Θ(n^2) |
I found mergesort to be the most complex sorting algorithm to implement. The next most complex was quicksort. There are two common types of mergesort: Top-Down & Bottom-Up.
Sorting algorithms
| Algorithm | Data structure | Time complexity:Worst |
|---|
| Heap sort | Array | O(n log(n)) |
| Smooth sort | Array | O(n log(n)) |
| Bubble sort | Array | O(n2) |
| Insertion sort | Array | O(n2) |
Radix sort: 0.220s. Quicksort: 0.247s. Shell sort: 0.250s. Merge sort: 0.435s.
Yes, Radix Sort and Counting Sort are O(N) . They are NOT comparison-based sorts, which have been proven to have Ω(N log N) lower bound. To be precise, Radix Sort is O(kN) , where k is the number of digits in the values to be sorted.
Unlike other sorting algorithms, LSD Radix Sort is non-comparative and only utilizes hashing ( O(1) amortized) and simple traversals ( O(n) ); put these two ingredients together, and you have a worst-case O(n) sorting algorithm!
Efficient sorting is important for optimizing the efficiency of other algorithms (such as search and merge algorithms) that require input data to be in sorted lists. Sorting is also often useful for canonicalizing data and for producing human-readable output.