TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
tim_sort.cpp
1// C++ program to perform TimSort.
2#include <algorithm>
3#include <cassert>
4#include <iostream>
5#include <numeric>
6
7const int RUN = 32;
8
9// this function sorts array from left index to to right index which is of size
10// atmost RUN
11void insertionSort(int arr[], int left, int right) {
12 for (int i = left + 1; i <= right; i++) {
13 const int temp = arr[i];
14 int j = i - 1;
15 while (j >= left && arr[j] > temp) {
16 arr[j + 1] = arr[j];
17 j--;
18 }
19 arr[j + 1] = temp;
20 }
21}
22
23// merge function merges the sorted runs
24void merge(int arr[], int l, int m, int r) {
25 // original array is broken in two parts, left and right array
26 const int len1 = m - l + 1, len2 = r - m;
27 int *left = new int[len1], *right = new int[len2];
28 for (int i = 0; i < len1; i++) left[i] = arr[l + i];
29 for (int i = 0; i < len2; i++) right[i] = arr[m + 1 + i];
30
31 int i = 0;
32 int j = 0;
33 int k = l;
34
35 // after comparing, we merge those two array in larger sub array
36 while (i < len1 && j < len2) {
37 if (left[i] <= right[j]) {
38 arr[k] = left[i];
39 i++;
40 } else {
41 arr[k] = right[j];
42 j++;
43 }
44 k++;
45 }
46
47 // copy remaining elements of left, if any
48 while (i < len1) {
49 arr[k] = left[i];
50 k++;
51 i++;
52 }
53
54 // copy remaining element of right, if any
55 while (j < len2) {
56 arr[k] = right[j];
57 k++;
58 j++;
59 }
60 delete[] left;
61 delete[] right;
62}
63
64// iterative Timsort function to sort the array[0...n-1] (similar to merge sort)
65void timSort(int arr[], int n) {
66 // Sort individual subarrays of size RUN
67 for (int i = 0; i < n; i += RUN)
68 insertionSort(arr, i, std::min((i + 31), (n - 1)));
69
70 // start merging from size RUN (or 32). It will merge to form size 64, then
71 // 128, 256 and so on ....
72 for (int size = RUN; size < n; size = 2 * size) {
73 // pick starting point of left sub array. We are going to merge
74 // arr[left..left+size-1] and arr[left+size, left+2*size-1] After every
75 // merge, we increase left by 2*size
76 for (int left = 0; left < n; left += 2 * size) {
77 // find ending point of left sub array
78 // mid+1 is starting point of right sub array
79 const int mid = std::min((left + size - 1), (n - 1));
80 const int right = std::min((left + 2 * size - 1), (n - 1));
81
82 // merge sub array arr[left.....mid] & arr[mid+1....right]
83 merge(arr, left, mid, right);
84 }
85 }
86}
87
88// utility function to print the Array
89void printArray(int arr[], int n) {
90 for (int i = 0; i < n; i++) printf("%d ", arr[i]);
91 std::cout << std::endl;
92}
93
98void tests() {
99 // Case: array of length 65
100 constexpr int N = 65;
101 int arr[N];
102
103 std::iota(arr, arr + N, 0);
104 std::reverse(arr, arr + N);
105 assert(!std::is_sorted(arr, arr + N));
106
107 timSort(arr, N);
108 assert(std::is_sorted(arr, arr + N));
109}
110
111// Driver program to test above function
112int main() {
113 tests(); // run self test implementations
114
115 int arr[] = {5, 21, 7, 23, 19};
116 const int n = sizeof(arr) / sizeof(arr[0]);
117 printf("Given Array is\n");
118 printArray(arr, n);
119
120 timSort(arr, n);
121
122 printf("After Sorting Array is\n");
123 printArray(arr, n);
124 return 0;
125}
double k(double x)
Another test function.
double l(double x)
Another test function.
uint32_t merge(T *arr, T *temp, uint32_t left, uint32_t mid, uint32_t right)
Function to merge two sub-arrays.
int main()
Main function.
void printArray(T *arr, int sz)
Definition heap_sort.cpp:37
void insertionSort(T *arr, int n)
Insertion Sort Function.
Testcases to check Union of Two Arrays.
constexpr uint32_t N
A struct to represent sparse table for min() as their invariant function, for the given array A....