【发布时间】:2014-12-14 04:45:32
【问题描述】:
当我在一个小数组上运行我的函数时,它运行良好。然而,当我使用一个大数组时,我不断得到堆栈溢出。 是因为我的代码中的逻辑不正确吗?还是只是需要很长时间?
void RecursiveSort(T data[], int first, B last)
{
// Sorts the array elements a[first] through a[last] recursively.
// base case is if first and last are the same, which means we
// have no subarrays left to do
if (first < last)
{
int minIndex = first;
// replace first index of array with smallest of values in the array
for (int index = first+1; index < last; index++)
{
if (data[index] < data[minIndex])
// swap values
minIndex = index;
}
int temp = data[first];
data[first] = data[minIndex];
data[minIndex] = temp;
RecursiveSort(data, first + 1, last);
}
}
【问题讨论】:
-
我不认为 sorter 从递归函数调用中受益。
标签: c++ recursion stack-overflow