[英]Timsort implementation in C++ not working on 10,000 numbers
//
// main.cpp
// timsort
//
// Created by Atharva Koli on 2019/1/27.
// Copyright © 2019 Atharva Koli. All rights reserved.
//
#include<bits/stdc++.h>
using namespace std;
const int RUN = 32;
// this function sorts array from left index to
// to right index which is of size atmost RUN
void insertionSort(int arr[], int left, int right)
{
for (int i = left + 1; i <= right; i++)
{
int temp = arr[i];
int j = i - 1;
while (arr[j] > temp && j >= left)
{
arr[j+1] = arr[j];
j--;
}
arr[j+1] = temp;
}
}
// merge function merges the sorted runs
void merge(int arr[], int l, int m, int r)
{
// original array is broken in two parts
// left and right array
int len1 = m - l + 1, len2 = r - m;
int left[len1], right[len2];
for (int i = 0; i < len1; i++)
left[i] = arr[l + i];
for (int i = 0; i < len2; i++)
right[i] = arr[m + 1 + i];
int i = 0;
int j = 0;
int k = l;
// after comparing, we merge those two array
// in larger sub array
while (i < len1 && j < len2)
{
if (left[i] <= right[j])
{
arr[k] = left[i];
i++;
}
else
{
arr[k] = right[j];
j++;
}
k++;
}
// copy remaining elements of left, if any
while (i < len1)
{
arr[k] = left[i];
k++;
i++;
}
// copy remaining element of right, if any
while (j < len2)
{
arr[k] = right[j];
k++;
j++;
}
}
// iterative Timsort function to sort the
// array[0...n-1] (similar to merge sort)
void timSort(int arr[], int n)
{
// Sort individual subarrays of size RUN
for (int i = 0; i < n; i+=RUN)
insertionSort(arr, i, min((i+31), (n-1)));
// start merging from size RUN (or 32). It will merge
// to form size 64, then 128, 256 and so on ....
for (int size = RUN; size < n; size = 2*size)
{
// pick starting point of left sub array. We
// are going to merge arr[left..left+size-1]
// and arr[left+size, left+2*size-1]
// After every merge, we increase left by 2*size
for (int left = 0; left < n; left += 2*size)
{
// find ending point of left sub array
// mid+1 is starting point of right sub array
int mid = left + size - 1;
int right = min((left + 2*size - 1), (n-1));
// merge sub array arr[left.....mid] &
// arr[mid+1....right]
merge(arr, left, mid, right);
}
}
}
// utility function to print the Array
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
// Driver program to test above function
int main()
{
int arr[] = {"10 thousand numbers separated by commas"};
int n = sizeof(arr)/sizeof(arr[0]);
timSort(arr, n);
printArray(arr, n);
return 0;
}
This is a program I wrote which sorts the numbers contained in an array using the timsort algorithm. 这是我编写的程序,该程序使用timsort算法对数组中包含的数字进行排序。 The program compiled and ran successfully using 100, 1000...numbers. 该程序使用100、1000 ...数字进行编译并成功运行。 However when I put 10,000 numbers or more in the array the program crashes giving me a compile error like so: Xcode Compile Error 但是,当我在数组中放置10,000个或更多数字时,程序崩溃,出现如下编译错误 : Xcode编译错误
I am still a beginner in C++ programming and the code above is combined using various sources online. 我仍然是C ++编程的初学者,并且上面的代码已使用各种在线资源进行组合。 Please assist me in identifying the issue! 请协助我确定问题!
The program is a standard implementation of timsort, efficiently combining arrays sorted using insertion sort. 该程序是timsort的标准实现,可以有效地组合使用插入排序排序的数组。
You have out of bound problem, though it doesn't reveal itself instantly. 您遇到了无法解决的问题,尽管它不会立即显示出来。 Firstly you use non-standard extension called VLA inside of merge()
, so how those arrays behaves in stack, I'm not sure. 首先,您不确定在merge()
内部使用称为VLA的非标准扩展名,因此我不确定这些数组在堆栈中的行为。 Secondly, the index math in merge results in 其次,合并中的索引数学结果为
l,len1 =0,32
l,len1 =64,32
l,len1 =128,32
l,len1 =192,32
...
l,len1 =9472,32
l,len1 =9536,32
l,len1 =9600,32
l,len1 =9664,32
l,len1 =9728,32
l,len1 =9792,32
l,len1 =9856,32
l,len1 =9920,32
l,len1 =9984,32
9984 + 32 > 10000 , so accessing arr[l + i]
results in reading past the end of array. 9984 + 32> 10000,因此访问arr[l + i]
导致读取结果超出数组末尾。 After few iteration it reaches state where that would cause segmentation fault. 几次迭代后,它会达到会导致分段错误的状态。 Try print your variables to debug it. 尝试打印变量以对其进行调试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.