C ++中的Timsort实现不适用于10,000个数字

时间:2019-04-01 07:48:02

标签: c++ sorting

//
//  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;
}

这是我编写的程序,该程序使用timsort算法对数组中包含的数字进行排序。该程序使用100、1000 ...数字进行编译并成功运行。但是,当我在数组中放入10,000个或更多数字时,程序崩溃,出现如下编译错误: Xcode Compile Error

我仍然是C ++编程的初学者,并且上面的代码已使用各种在线资源进行合并。请协助我确定问题!

该程序是timsort的标准实现,可以有效地组合使用插入排序排序的数组。

1 个答案:

答案 0 :(得分:2)

您遇到了无法解决的问题,尽管它并不能立即显示出来。首先,您不确定在merge()内使用称为VLA的非标准扩展名,因此不确定这些数组在堆栈中的行为。其次,合并中的索引数学结果为

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,因此访问arr[l + i]导致读取超出数组末尾。几次迭代后,它会达到会导致分段错误的状态。尝试打印变量以对其进行调试。