sort.c
/* 
this algorithms is O(n log n), proof is in the comments below
*/
this algorithms is O(n log n), proof is in the comments below
this algorithms is O ( n log n ) proof is in the comments below
void timSort(int array[], int n) {
	int auxiliary[n];
	int i, size;
	for (i = 0; i < n; i += SIZE_RUN) {
		if (i + SIZE_RUN <= n) {
			insertionSort(array + i, SIZE_RUN);
		} else {
			insertionSort(array + i, n - i);
		}
	}
	for (size = SIZE_RUN; size < n; size *= 2) {
		for (i = 0; i < n; i += 2 * size) {
			int rightSize = size;
			if (i + 2 * size > n) {
				rightSize = n - i - size;
			}
			merge(array + i, array + i + size, size, rightSize, auxiliary);
		}
	}
}
void timSort ( int array [ ] , int n ) { int auxiliary [ n ] ; int i , size ; for ( i = 0 ; i < n ; i += SIZE_RUN ) { if ( i + SIZE_RUN <= n ) { insertionSort ( array + i , SIZE_RUN ) ; } else { insertionSort ( array + i , n - i ) ; } } for ( size = SIZE_RUN ; size < n ; size *= 2 ) { for ( i = 0 ; i < n ; i += 2 * size ) { int rightSize = size ; if ( i + 2 * size > n ) { rightSize = n - i - size ; } merge ( array + i , array + i + size , size , rightSize , auxiliary ) ; } } }
this algorithms is O(n log n), proof is in the comments below
this algorithms is O ( n log n ) proof is in the comments below
// first sort all runs using insertion sort // this step is O(n) // proof: //  loop (n/SIZE_RUN) times //  each loop is O(SIZE_RUN^2) using insertion sort //  overall is O(SIZE_RUN^2 * n/SIZE_RUN) = O(n * SIZE_RUN) //  since SIZE_RUN is a constant, it is O(n) // last part might not be exactly of size run // then merge those runs  // this step is O(n log n) // proof: //  outer loop log(n/SIZE_RUN) times //  inner loop is O(n) using merge //  overall is O(n log(n/SIZE_RUN)) //  simce SIZE_RUN is a constant, it is O(n log n) // exceeds limit // combining two parts, it is O(n + n*log n) which is O(n log n)