Nikzy7 / Algorithm-Snippets

Repo containing snippets of popular algorithms
17 stars 36 forks source link

Tim_Sort.py #86

Closed goyalritika14 closed 3 years ago

goyalritika14 commented 3 years ago

TimSort is a sorting technique based on Insertion Sort and Merge Sort. It is a stable sorting algorithm works in O(n Log n) time It is 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.