- A stable sorting algorithm works in O(n Log n) time
- Used in Java’s Arrays.sort() as well as Python’s sorted() and sort().
- First sort small pieces using Insertion Sort, then merges the pieces using merge of merge sort.
We divide the Array into blocks known as Run. We sort those runs using insertion sort one by one and then merge those runs using combine function used in merge sort. If the size of Array is less than run, then Array get sorted just by using Insertion Sort. The size of run may vary from 32 to 64 depending upon the size of the array. Note that merge function performs well when sizes subarrays are powers of 2. The idea is based on the fact that insertion sort performs well for small arrays.
Details of below implementation :
- We consider size of run as 32.
- We one by one sort pieces of size equal to run
- After sorting individual pieces, we merge them one by one. We double the size of merged subarrays after every iteration.
Given Array is 5 21 7 23 19 After Sorting Array is 5 7 19 21 23
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Minimize sum of smallest elements from K subsequences of length L
- Lexicographically smallest string after M operations
- Maximum size subset with given sum using Backtracking
- Maximize jobs that can be completed under given constraint
- Minimize number of boxes by putting small box inside bigger one
- Minimum increment/decrement operations required to make Median as X
- Minimum sum obtained by choosing N number from given N pairs
- Largest square which can be formed using given rectangular blocks
- Count of elements which is product of a pair or an element square
- 0/1 Knapsack using Least Count Branch and Bound
- Count of distinct numbers in an Array in a range for Online Queries using Merge Sort Tree
- Sorting algorithm visualization : Insertion Sort
- Sort a string lexicographically using triple cyclic shifts
- Minimize difference after changing all odd elements to even