Tim-sort
Tim-sort is a sorting algorithm derived from insertion sort and merge sort. It was designed to perform in an optimal way on different kind of real world data. It is an adaptive sorting algorithm which needs O(n log n) comparisons to sort an array of n elements. It was designed and implemented by Tim Peters in 2002 in python programming language. It has been python's standard sorting algorithm since version 2.3.
Technique
Consider an array of n elements which needs to be sorted. In Tim sort, the array is divided into several parts where each of them is called a Run. These Runs are sorted by using insertion sort one by one and then the sorted runs get combined using a combine function. The idea of Tim sort is originated from the fact that, insertion sort works more optimally on the short lists rather than working on the larger lists.
Complexity
Complexity | Best Case | Average Case | Worst Case |
---|---|---|---|
Time Complexity | O(n) | O(n log n) | O(n log n) |
Space Complexity | n |
Steps :
- Divide the array into the number of blocks known as run.
- Consider size of run either 32 or 64(in the below implementation, size of run is 32.)
- Sort the individual elements of every run one by one using insertion sort.
- Merge the sorted runs one by one using merge function of merge sort.
- Double the size of merged sub-arrays after every iteration.
- #include<stdio.h>
- const int run = 32;
- int minimum(int a, int b)
- {
- if(a<b)
- return a;
- else
- return b;
- }
- void insertionSort(int a[], int beg, int end)
- {
- int temp, i, j;
- for (i = beg + 1; i <= end; i++)
- {
- temp = a[i];
- j = i - 1;
- while (a[j] > temp && j >= beg)
- {
- a[j+1] = a[j];
- j--;
- }
- a[j+1] = temp;
- }
- }
- void merge(int a[], int left, int mid, int right)
- {
- int len1 = mid - left + 1, len2 = right - mid;
- int beg[len1], end[len2];
- int i,j,k;
- for (i = 0; i < len1; i++)
- beg[i] = a[left + i];
- for (i = 0; i < len2; i++)
- end[i] = a[mid + 1 + i];
- i = 0;
- j = 0;
- k = left;
- while (i < len1 && j < len2)
- {
- if (beg[i] <= end[j])
- {
- a[k] = beg[i];
- i++;
- }
- else
- {
- a[k] = end[j];
- j++;
- }
- k++;
- }
- while (i < len1)
- {
- a[k] = beg[i];
- k++;
- i++;
- }
- while (j < len2)
- {
- a[k] = end[j];
- k++;
- j++;
- }
- }
- void timSort(int a[], int n)
- {
- int i,size,beg,mid,end;
- for (i = 0; i < n; i+=run)
- insertionSort(a, i, minimum((i+31), (n-1)));
- for (size = run; size < n; size = 2*size)
- {
- for (beg = 0; beg < n; beg += 2*size)
- {
- mid = beg + size - 1;
- end = minimum((beg + 2*size - 1), (n-1));
- merge(a, beg, mid, end);
- }
- }
- }
- int main()
- {
- int a[] = {12,1,20,2,3,123,54,332},i;
- int n = sizeof(a)/sizeof(a[0]);
- printf("Printing Array elements \n");
- for (i = 0; i < n; i++)
- printf("%d ", a[i]);
- printf("\n");
- timSort(a, n);
- printf("Printing sorted array elements \n");
- for (i = 0; i < n; i++)
- printf("%d ", a[i]);
- printf("\n");
- return 0;
- }
C program
Printing Array elements 12 1 20 2 3 123 54 332 Printing sorted array elements 1 2 3 12 20 54 123 332