Skip to content

Commit db182d5

Browse files
MaximSmolskiyrealstealthninja
andauthoredSep 1, 2024
fix: fix bug in timSort (TheAlgorithms#2692)
* fix: fix bug in timSort * Apply suggestions from code review Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com> * Fix * Update sorting/tim_sort.cpp * Add const --------- Co-authored-by: realstealthninja <68815218+realstealthninja@users.noreply.github.com>
1 parent 52db277 commit db182d5

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed
 

‎sorting/tim_sort.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// C++ program to perform TimSort.
22
#include <algorithm>
3+
#include <cassert>
34
#include <iostream>
5+
#include <numeric>
46

57
const int RUN = 32;
68

79
// this function sorts array from left index to to right index which is of size
810
// atmost RUN
911
void insertionSort(int arr[], int left, int right) {
1012
for (int i = left + 1; i <= right; i++) {
11-
int temp = arr[i];
13+
const int temp = arr[i];
1214
int j = i - 1;
1315
while (arr[j] > temp && j >= left) {
1416
arr[j + 1] = arr[j];
@@ -21,7 +23,7 @@ void insertionSort(int arr[], int left, int right) {
2123
// merge function merges the sorted runs
2224
void merge(int arr[], int l, int m, int r) {
2325
// original array is broken in two parts, left and right array
24-
int len1 = m - l + 1, len2 = r - m;
26+
const int len1 = m - l + 1, len2 = r - m;
2527
int *left = new int[len1], *right = new int[len2];
2628
for (int i = 0; i < len1; i++) left[i] = arr[l + i];
2729
for (int i = 0; i < len2; i++) right[i] = arr[m + 1 + i];
@@ -74,8 +76,8 @@ void timSort(int arr[], int n) {
7476
for (int left = 0; left < n; left += 2 * size) {
7577
// find ending point of left sub array
7678
// mid+1 is starting point of right sub array
77-
int mid = left + size - 1;
78-
int right = std::min((left + 2 * size - 1), (n - 1));
79+
const int mid = std::min((left + size - 1), (n - 1));
80+
const int right = std::min((left + 2 * size - 1), (n - 1));
7981

8082
// merge sub array arr[left.....mid] & arr[mid+1....right]
8183
merge(arr, left, mid, right);
@@ -89,10 +91,29 @@ void printArray(int arr[], int n) {
8991
std::cout << std::endl;
9092
}
9193

94+
/**
95+
* @brief self-test implementation
96+
* @returns void
97+
*/
98+
void 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+
92111
// Driver program to test above function
93112
int main() {
113+
tests(); // run self test implementations
114+
94115
int arr[] = {5, 21, 7, 23, 19};
95-
int n = sizeof(arr) / sizeof(arr[0]);
116+
const int n = sizeof(arr) / sizeof(arr[0]);
96117
printf("Given Array is\n");
97118
printArray(arr, n);
98119

0 commit comments

Comments
 (0)
Please sign in to comment.