├── 1) Bubble.cpp ├── 10) Bucket Sort.cpp ├── 11) Shell Sort.cpp ├── 12) Bogo Sort.cpp ├── 2) Sorting Strings.cpp ├── 3)InsertionSort.cpp ├── 4)Quick_Sort.cpp ├── 5) SelectionSort.cpp ├── 6) MergeSort.cpp ├── 7. heapSort.cpp ├── 8) Counting Sort.cpp ├── 9) Radix Sort.cpp ├── HeapSort.java ├── MergeSort.java ├── QuickSortBest.java ├── QuickSortWorst.java └── Tim Sort.cpp /1) Bubble.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | void swap(int *x, int *y){ 5 | int temp=*x; 6 | *x=*y; 7 | *y=temp; 8 | } 9 | 10 | void BubbleSort(int A[], int n){ 11 | int i,j,flag=0; 12 | 13 | for(i=0;iA[j+1]){ 17 | swap(&A[j] , &A[j+1]); 18 | flag=1; 19 | } 20 | } 21 | if(flag==0){ 22 | break; 23 | } 24 | } 25 | } 26 | 27 | int main(){ 28 | int A[] = {11,13,6,23,443,44,54,656,65,98}, n=10,i; 29 | BubbleSort(A,n); 30 | 31 | for(i=0;i 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // Function to sort arr[] of size n using bucket sort 8 | void bucketSort(float arr[], int n) 9 | { 10 | // 1) Create n empty buckets 11 | vector b[n]; 12 | 13 | // 2) Put array elements in different buckets 14 | for (int i = 0; i < n; i++) { 15 | int bi = n * arr[i]; // Index in bucket 16 | b[bi].push_back(arr[i]); 17 | } 18 | 19 | // 3) Sort individual buckets 20 | for (int i = 0; i < n; i++) 21 | sort(b[i].begin(), b[i].end()); 22 | 23 | // 4) Concatenate all buckets into arr[] 24 | int index = 0; 25 | for (int i = 0; i < n; i++) 26 | for (int j = 0; j < b[i].size(); j++) 27 | arr[index++] = b[i][j]; 28 | } 29 | 30 | /* Driver program to test above function */ 31 | int main() 32 | { 33 | float arr[] = { 0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434 }; 34 | int n = sizeof(arr) / sizeof(arr[0]); 35 | bucketSort(arr, n); 36 | 37 | cout << "Sorted array is \n"; 38 | for (int i = 0; i < n; i++) 39 | cout << arr[i] << " "; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /11) Shell Sort.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of Shell Sort 2 | #include 3 | using namespace std; 4 | 5 | /* function to sort arr using shellSort */ 6 | int shellSort(int arr[], int n) 7 | { 8 | // Start with a big gap, then reduce the gap 9 | for (int gap = n/2; gap > 0; gap /= 2) 10 | { 11 | // Do a gapped insertion sort for this gap size. 12 | // The first gap elements a[0..gap-1] are already in gapped order 13 | // keep adding one more element until the entire array is 14 | // gap sorted 15 | for (int i = gap; i < n; i += 1) 16 | { 17 | // add a[i] to the elements that have been gap sorted 18 | // save a[i] in temp and make a hole at position i 19 | int temp = arr[i]; 20 | 21 | // shift earlier gap-sorted elements up until the correct 22 | // location for a[i] is found 23 | int j; 24 | for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) 25 | arr[j] = arr[j - gap]; 26 | 27 | // put temp (the original a[i]) in its correct location 28 | arr[j] = temp; 29 | } 30 | } 31 | return 0; 32 | } 33 | 34 | void printArray(int arr[], int n) 35 | { 36 | for (int i=0; i 3 | using namespace std; 4 | 5 | // To check if array is sorted or not 6 | bool isSorted(int a[], int n) 7 | { 8 | while (--n > 1) 9 | if (a[n] < a[n - 1]) 10 | return false; 11 | return true; 12 | } 13 | 14 | // To generate permuatation of the array 15 | void shuffle(int a[], int n) 16 | { 17 | for (int i = 0; i < n; i++) 18 | swap(a[i], a[rand() % n]); 19 | } 20 | 21 | // Sorts array a[0..n-1] using Bogo sort 22 | void bogosort(int a[], int n) 23 | { 24 | // if array is not sorted then shuffle 25 | // the array again 26 | while (!isSorted(a, n)) 27 | shuffle(a, n); 28 | } 29 | 30 | // prints the array 31 | void printArray(int a[], int n) 32 | { 33 | for (int i = 0; i < n; i++) 34 | printf("%d ", a[i]); 35 | printf("\n"); 36 | } 37 | 38 | // Driver code 39 | int main() 40 | { 41 | int a[] = { 3, 2, 5, 1, 0, 4 }; 42 | int n = sizeof a / sizeof a[0]; 43 | bogosort(a, n); 44 | printf("Sorted array :\n"); 45 | printArray(a, n); 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /2) Sorting Strings.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | char str[5][10],temp[10]; 6 | int i,j,k; 7 | cout << "Enter 5 names to sort them in dictionary order " << endl; 8 | for(i=0;i<5;i++){ 9 | cin >> str[i] ; 10 | } 11 | for(i=0;i<5;i++){ 12 | for(j=0;j<5;j++){ 13 | k = strcmp(str[j],str[j+1]); 14 | if(k>0){ 15 | strcpy(temp,str[j]);a 16 | strcpy(str[j],str[j+1]); 17 | strcpy(str[j+1],temp); 18 | } 19 | } 20 | } 21 | cout < 2 | using namespace std; 3 | 4 | void InsertionSort(int A[],int n){ 5 | int x,i,j; //Time Complexity = O(n^2) 6 | for(i=1;i-1 && A[j]>x){ 10 | A[j+1] = A[j]; 11 | j=j-1; 12 | } 13 | A[j+1] = x; 14 | } 15 | } 16 | 17 | int main(){ 18 | int n=5; 19 | int A[5]={43,23,125,87,32}; 20 | InsertionSort(A,n); 21 | for(int i=0;i 2 | using namespace std; 3 | 4 | void swap(int *x,int *y){ //fun to swap two values 5 | int temp; 6 | temp = *x; 7 | *x=*y; 8 | *y=temp; 9 | } 10 | 11 | int partition(int A[],int l, int h){ 12 | int pivot = A[l]; 13 | int i=l,j=h; 14 | do{ // Main Algo; 15 | do{ 16 | i++; 17 | }while(A[i]<=pivot); 18 | do{ 19 | j--; 20 | }while(A[j]>pivot); 21 | if(i 4 | using namespace std; 5 | 6 | void swap(int *p,int *q){ 7 | int temp = *p; 8 | *p = *q; 9 | *q = temp; 10 | } 11 | 12 | void SelectionSort(int A[],int n){ 13 | int i,j,min; 14 | for(i=0;i> n; 34 | int A[n]; 35 | for(int i=0;i>A[i]; 37 | } 38 | SelectionSort(A,n); 39 | Display(A,n); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /6) MergeSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | void Merge(int A[],int p,int q,int r){ 5 | 6 | /* 7 | Assumes A to be a list and p,q and r an int 8 | merges the sorted sub-lists into the single list A 9 | */ 10 | 11 | int i,j,k; 12 | int n1 = q-p+1; 13 | int n2 = r-q; 14 | int L[n1+1],R[n2+1]; 15 | for(i=0;i> n; 85 | int A[n]; 86 | cout << "Enter the elements :" << endl; 87 | for(int i=0;i> A[i]; 89 | } 90 | int p,r; 91 | p=0,r=n-1; 92 | Sort(A,p,r); 93 | Display(A,n); 94 | return 0; 95 | } 96 | -------------------------------------------------------------------------------- /7. heapSort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | class Node{ 5 | private: 6 | int value; 7 | int index; 8 | 9 | public: 10 | Node(){ 11 | 12 | } 13 | Node(int a,int b){ 14 | value = a; 15 | index = b; 16 | } 17 | 18 | void setValue(int a){ 19 | value = a; 20 | } 21 | void setIndex(int a){ 22 | index = a; 23 | } 24 | 25 | int getValue(){ 26 | return this ->value; 27 | } 28 | int getIndex(){ 29 | return this ->index; 30 | } 31 | 32 | int right(){ 33 | int i = this -> index; 34 | return 2*i + 2; 35 | } 36 | int left(){ 37 | int i = this -> index; 38 | return 2*i + 1; 39 | } 40 | int parent(){ 41 | int i = this -> index; 42 | return i/2; 43 | } 44 | }; 45 | void swap(Node &a,Node &b){ 46 | int i = a.getValue(); 47 | a.setValue(b.getValue()); 48 | b.setValue(i); 49 | } 50 | 51 | 52 | void maxHeapify(Node *a,int index,int n) 53 | //Creates a max heap at a particular index of an array 54 | //Running time of O(lgn) as the maximum height of the node can be lgn(base 2 log) 55 | { 56 | int l = a[index].left(); //index of the left child 57 | int r = a[index].right();//index of the right child 58 | int largest = 0; 59 | if((l < n) && (a[l].getValue() > a[index].getValue())){ 60 | largest = l; 61 | } 62 | else{ 63 | largest = index; 64 | } 65 | if( (r < n) && (a[r].getValue() > a[largest].getValue())){ 66 | largest = r; 67 | } 68 | //largest contains the index of the largest of parent,left child and right child 69 | if(largest != index){ 70 | swap(a[largest],a[index]); 71 | //The parent needs to be swapped as it does not contain the largest value 72 | maxHeapify(a,largest,n); 73 | //As the swapped element could violate maxHeapify property, a recursive call 74 | } 75 | } 76 | 77 | void buildMaxHeap(Node *a,int n) 78 | //Build max heaps at each node in an array 79 | //Running time is O(n) i.e. linear time 80 | { 81 | int i = n/2; 82 | // As half of the node in an array are at the leaf so they are already a max-heap 83 | while (i >= 0){ 84 | maxHeapify(a,i,n); 85 | // Run maxHeapify on the remaining nodes 86 | i -= 1; 87 | } 88 | } 89 | 90 | 91 | void heapsort(Node *a,int n) 92 | //Sorts the array using buildMaxHeap and MaxHeapify 93 | //Running time is O(nlgn) 94 | { 95 | buildMaxHeap(a,n); 96 | //Converts an array into max heap. Thus, the largest element is root 97 | int i = n - 1; 98 | while (i >= 1) 99 | { 100 | swap(a[0],a[i]); 101 | // The largest element is at position 0 that is root of the maxHeap, so it is swapped to its right position 102 | //Now the array a[1...i] contains smallest i element and a[i+1.....n] are already in sorted form 103 | maxHeapify(a,0,i); 104 | i -= 1; 105 | } 106 | 107 | } 108 | int main(){ 109 | int size; 110 | scanf("%d",&size); 111 | Node heap[size]; 112 | int n; 113 | for(int i = 0; i < size;i++){ 114 | scanf("%d",&n); 115 | heap[i].setIndex(i); 116 | heap[i].setValue(n); 117 | } 118 | heapsort(heap,size); 119 | printf("\n"); 120 | for(int j = 0; j < size;j++){ 121 | printf("%d ",heap[j].getValue()); 122 | } 123 | printf("\n"); 124 | return 0; 125 | } 126 | -------------------------------------------------------------------------------- /8) Counting Sort.cpp: -------------------------------------------------------------------------------- 1 | // C++ Program for counting sort 2 | #include 3 | #include 4 | using namespace std; 5 | #define RANGE 255 6 | 7 | // The main function that sort 8 | // the given string arr[] in 9 | // alphabatical order 10 | void countSort(char arr[]) 11 | { 12 | // The output character array 13 | // that will have sorted arr 14 | char output[strlen(arr)]; 15 | 16 | // Create a count array to store count of inidividul 17 | // characters and initialize count array as 0 18 | int count[RANGE + 1], i; 19 | memset(count, 0, sizeof(count)); 20 | 21 | // Store count of each character 22 | for (i = 0; arr[i]; ++i) 23 | ++count[arr[i]]; 24 | 25 | // Change count[i] so that count[i] now contains actual 26 | // position of this character in output array 27 | for (i = 1; i <= RANGE; ++i) 28 | count[i] += count[i - 1]; 29 | 30 | // Build the output character array 31 | for (i = 0; arr[i]; ++i) { 32 | output[count[arr[i]] - 1] = arr[i]; 33 | --count[arr[i]]; 34 | } 35 | 36 | /* 37 | For Stable algorithm 38 | for (i = sizeof(arr)-1; i>=0; --i) 39 | { 40 | output[count[arr[i]]-1] = arr[i]; 41 | --count[arr[i]]; 42 | } 43 | 44 | For Logic : See implementation 45 | */ 46 | 47 | // Copy the output array to arr, so that arr now 48 | // contains sorted characters 49 | for (i = 0; arr[i]; ++i) 50 | arr[i] = output[i]; 51 | } 52 | 53 | // Driver code 54 | int main() 55 | { 56 | char arr[] = "geeksforgeeks"; 57 | 58 | countSort(arr); 59 | 60 | cout << "Sorted character array is " << arr; 61 | return 0; 62 | } 63 | 64 | // This code is contributed by rathbhupendra 65 | -------------------------------------------------------------------------------- /9) Radix Sort.cpp: -------------------------------------------------------------------------------- 1 | // C++ implementation of Radix Sort 2 | #include 3 | using namespace std; 4 | 5 | // A utility function to get maximum value in arr[] 6 | int getMax(int arr[], int n) 7 | { 8 | int mx = arr[0]; 9 | for (int i = 1; i < n; i++) 10 | if (arr[i] > mx) 11 | mx = arr[i]; 12 | return mx; 13 | } 14 | 15 | // A function to do counting sort of arr[] according to 16 | // the digit represented by exp. 17 | void countSort(int arr[], int n, int exp) 18 | { 19 | int output[n]; // output array 20 | int i, count[10] = { 0 }; 21 | 22 | // Store count of occurrences in count[] 23 | for (i = 0; i < n; i++) 24 | count[(arr[i] / exp) % 10]++; 25 | 26 | // Change count[i] so that count[i] now contains actual 27 | // position of this digit in output[] 28 | for (i = 1; i < 10; i++) 29 | count[i] += count[i - 1]; 30 | 31 | // Build the output array 32 | for (i = n - 1; i >= 0; i--) { 33 | output[count[(arr[i] / exp) % 10] - 1] = arr[i]; 34 | count[(arr[i] / exp) % 10]--; 35 | } 36 | 37 | // Copy the output array to arr[], so that arr[] now 38 | // contains sorted numbers according to current digit 39 | for (i = 0; i < n; i++) 40 | arr[i] = output[i]; 41 | } 42 | 43 | // The main function to that sorts arr[] of size n using 44 | // Radix Sort 45 | void radixsort(int arr[], int n) 46 | { 47 | // Find the maximum number to know number of digits 48 | int m = getMax(arr, n); 49 | 50 | // Do counting sort for every digit. Note that instead 51 | // of passing digit number, exp is passed. exp is 10^i 52 | // where i is current digit number 53 | for (int exp = 1; m / exp > 0; exp *= 10) 54 | countSort(arr, n, exp); 55 | } 56 | 57 | // A utility function to print an array 58 | void print(int arr[], int n) 59 | { 60 | for (int i = 0; i < n; i++) 61 | cout << arr[i] << " "; 62 | } 63 | 64 | // Driver Code 65 | int main() 66 | { 67 | int arr[] = { 170, 45, 75, 90, 802, 24, 2, 66 }; 68 | int n = sizeof(arr) / sizeof(arr[0]); 69 | 70 | // Function Call 71 | radixsort(arr, n); 72 | print(arr, n); 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /HeapSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class HeapSort{ 3 | public void sort(int arr[]){ 4 | int n = arr.length; 5 | for (int i = n / 2 - 1; i >= 0; i--) 6 | heapify(arr, n, i); 7 | for (int i=n-1; i>=0; i--){ 8 | int temp = arr[0]; 9 | arr[0] = arr[i]; 10 | arr[i] = temp; 11 | heapify(arr, i, 0); 12 | } 13 | } 14 | void heapify(int arr[], int n, int i){ 15 | int largest = i; 16 | int l = 2*i + 1; 17 | int r = 2*i + 2; 18 | if (l < n && arr[l] > arr[largest]) 19 | largest = l; 20 | if (r < n && arr[r] > arr[largest]) 21 | largest = r; 22 | if (largest != i){ 23 | int swap = arr[i]; 24 | arr[i] = arr[largest]; 25 | arr[largest] = swap; 26 | 27 | heapify(arr, n, largest); 28 | } 29 | } 30 | static void printArray(int arr[]){ 31 | int n = arr.length; 32 | for (int i=0; i"); 56 | ob1.printArray(arr); 57 | System.out.println("\n Time taken for worst case = " + (end-start)/100000 + "ms"); 58 | HeapSort ob2 = new HeapSort(); 59 | System.out.println("\nTaking previously Sorted array for best Case.\n"); 60 | start = System.nanoTime(); 61 | ob2.sort(arr); 62 | end = System.nanoTime(); 63 | System.out.print("\nSorted Array ===>"); 64 | ob2.printArray(arr); 65 | System.out.println("\n Time taken for best case = " + (end-start)/100000 + "ms"); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /MergeSort.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class MergeSort{ 3 | double number_of_merge_calls = 0; 4 | void merge(double arr[], int l, int m, int r){ 5 | number_of_merge_calls++; 6 | int n1 = m - l + 1; 7 | int n2 = r - m; 8 | double L[] = new double [n1]; 9 | double R[] = new double [n2]; 10 | for (int i=0; i= R[j]){ 18 | arr[k] = L[i]; 19 | i++; 20 | } 21 | else{ 22 | arr[k] = R[j]; 23 | j++; 24 | } 25 | k++; 26 | } 27 | while (i < n1){ 28 | arr[k] = L[i]; 29 | i++; 30 | k++; 31 | } 32 | while (j < n2){ 33 | arr[k] = R[j]; 34 | j++; 35 | k++; 36 | } 37 | } 38 | 39 | void sort(double arr[], int l, int r){ 40 | if (l < r){ 41 | int m = (l+r)/2; 42 | sort(arr, l, m); 43 | sort(arr , m+1, r); 44 | merge(arr, l, m, r); 45 | } 46 | } 47 | static void printArray(double arr[]){ 48 | int n = arr.length; 49 | for (int i=0; i"); 67 | ob.printArray(arr); 68 | MergeSort ob1 = new MergeSort(); 69 | double start = System.nanoTime(); 70 | ob1.sort(arr,0,n-1); 71 | double end = System.nanoTime(); 72 | System.out.print("\nSorted Array ===>"); 73 | ob1.printArray(arr); 74 | System.out.println("\nNumber of Merge Calls = " + ob1.number_of_merge_calls); 75 | System.out.println("\n Time taken = " + (end-start)/100000 + "ms"); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /QuickSortBest.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class QuickSortBest 3 | { 4 | static int partition = 0; 5 | 6 | static void qsort(int a[], int left_index, int right_index) 7 | { 8 | int left, right, pivot; 9 | if(left_index >= right_index) return; 10 | left = left_index; 11 | right = right_index; 12 | pivot = a[(left_index + right_index) /2]; 13 | partition++; 14 | while(left <= right) { 15 | while(a[left] < pivot) left++; 16 | while(a[right] > pivot) right--; 17 | if(left <= right) { 18 | swap(a,left,right); 19 | left++; right--; 20 | } 21 | qsort(a,left_index,right); 22 | qsort(a,left,right_index); 23 | } 24 | } 25 | 26 | static void swap(int a[], int i, int j) 27 | { 28 | int temp; 29 | temp = a[i]; 30 | a[i] = a[j]; 31 | a[j] = temp; 32 | } 33 | 34 | static void printArray(int arr[]) 35 | { 36 | int n = arr.length; 37 | for (int i=0; i"); 61 | ob1.printArray(arr); 62 | System.out.println("\n Time taken for worst case = " + (end-start)/100000 + "Partion calls = " + ob1.partition ); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /QuickSortWorst.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class QuickSortWorst{ 3 | int partition = 0; 4 | int partition(int arr[], int low, int high){ 5 | partition++; 6 | int pivot = arr[low]; 7 | int i = low; 8 | { 9 | for (int j=low+1; j<=high; j++) 10 | if (arr[j] <= pivot) 11 | { 12 | i++; 13 | int temp = arr[i]; 14 | arr[i] = arr[j]; 15 | arr[j] = temp; 16 | } 17 | } 18 | int temp = arr[i]; 19 | arr[i] = arr[low]; 20 | arr[low] = temp; 21 | return i; 22 | } 23 | 24 | void sort(int arr[], int low, int high) 25 | { 26 | if (low < high) 27 | { 28 | int pi = partition(arr, low, high); 29 | sort(arr, low, pi-1); 30 | sort(arr, pi+1, high); 31 | } 32 | } 33 | 34 | static void printArray(int arr[]) 35 | { 36 | int n = arr.length; 37 | for (int i=0; i"); 61 | ob1.printArray(arr); 62 | System.out.println("\n Time taken for worst case = " + (end-start)/100000 + "Partiton calls = " + ob1.partition); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Tim Sort.cpp: -------------------------------------------------------------------------------- 1 | // C++ program to perform TimSort. 2 | #include 3 | using namespace std; 4 | const int RUN = 32; 5 | 6 | // this function sorts array from left index to 7 | // to right index which is of size atmost RUN 8 | void insertionSort(int arr[], int left, int right) 9 | { 10 | for (int i = left + 1; i <= right; i++) 11 | { 12 | int temp = arr[i]; 13 | int j = i - 1; 14 | while (j >= left && arr[j] > temp) 15 | { 16 | arr[j+1] = arr[j]; 17 | j--; 18 | } 19 | arr[j+1] = temp; 20 | } 21 | } 22 | 23 | // merge function merges the sorted runs 24 | void merge(int arr[], int l, int m, int r) 25 | { 26 | // original array is broken in two parts 27 | // left and right array 28 | int len1 = m - l + 1, len2 = r - m; 29 | int left[len1], right[len2]; 30 | for (int i = 0; i < len1; i++) 31 | left[i] = arr[l + i]; 32 | for (int i = 0; i < len2; i++) 33 | right[i] = arr[m + 1 + i]; 34 | 35 | int i = 0; 36 | int j = 0; 37 | int k = l; 38 | 39 | // after comparing, we merge those two array 40 | // in larger sub array 41 | while (i < len1 && j < len2) 42 | { 43 | if (left[i] <= right[j]) 44 | { 45 | arr[k] = left[i]; 46 | i++; 47 | } 48 | else 49 | { 50 | arr[k] = right[j]; 51 | j++; 52 | } 53 | k++; 54 | } 55 | 56 | // copy remaining elements of left, if any 57 | while (i < len1) 58 | { 59 | arr[k] = left[i]; 60 | k++; 61 | i++; 62 | } 63 | 64 | // copy remaining element of right, if any 65 | while (j < len2) 66 | { 67 | arr[k] = right[j]; 68 | k++; 69 | j++; 70 | } 71 | } 72 | 73 | // iterative Timsort function to sort the 74 | // array[0...n-1] (similar to merge sort) 75 | void timSort(int arr[], int n) 76 | { 77 | // Sort individual subarrays of size RUN 78 | for (int i = 0; i < n; i+=RUN) 79 | insertionSort(arr, i, min((i+31), (n-1))); 80 | 81 | // start merging from size RUN (or 32). It will merge 82 | // to form size 64, then 128, 256 and so on .... 83 | for (int size = RUN; size < n; size = 2*size) 84 | { 85 | // pick starting point of left sub array. We 86 | // are going to merge arr[left..left+size-1] 87 | // and arr[left+size, left+2*size-1] 88 | // After every merge, we increase left by 2*size 89 | for (int left = 0; left < n; left += 2*size) 90 | { 91 | // find ending point of left sub array 92 | // mid+1 is starting point of right sub array 93 | int mid = left + size - 1; 94 | int right = min((left + 2*size - 1), (n-1)); 95 | 96 | // merge sub array arr[left.....mid] & 97 | // arr[mid+1....right] 98 | merge(arr, left, mid, right); 99 | } 100 | } 101 | } 102 | 103 | // utility function to print the Array 104 | void printArray(int arr[], int n) 105 | { 106 | for (int i = 0; i < n; i++) 107 | printf("%d ", arr[i]); 108 | printf("\n"); 109 | } 110 | 111 | // Driver program to test above function 112 | int main() 113 | { 114 | int arr[] = {5, 21, 7, 23, 19}; 115 | int n = sizeof(arr)/sizeof(arr[0]); 116 | printf("Given Array is\n"); 117 | printArray(arr, n); 118 | 119 | timSort(arr, n); 120 | 121 | printf("After Sorting Array is\n"); 122 | printArray(arr, n); 123 | return 0; 124 | } 125 | --------------------------------------------------------------------------------