Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;随机快速排序故障 我在C++中开发了一个随机快速排序程序,但是由于某种原因,程序是SeaFaltTin,而我对它的原因有点迷惑。_C++_Sorting_Random_Quicksort - Fatal编程技术网

C++;随机快速排序故障 我在C++中开发了一个随机快速排序程序,但是由于某种原因,程序是SeaFaltTin,而我对它的原因有点迷惑。

C++;随机快速排序故障 我在C++中开发了一个随机快速排序程序,但是由于某种原因,程序是SeaFaltTin,而我对它的原因有点迷惑。,c++,sorting,random,quicksort,C++,Sorting,Random,Quicksort,我很确定这与我的hoarePartition函数有关,陷入了一个while循环,但我不确定问题出在哪里 任何关于解决这个问题的帮助都会非常有帮助 #import <iostream> #import <cstdlib> #import <random> #import <time.h> #include <ctime> #include <boost/timer.hpp> void swap(int& first,

我很确定这与我的hoarePartition函数有关,陷入了一个while循环,但我不确定问题出在哪里

任何关于解决这个问题的帮助都会非常有帮助

#import <iostream>
#import <cstdlib>
#import <random>
#import <time.h>
#include <ctime>
#include <boost/timer.hpp>

void swap(int& first, int& second)
{
    int temp = first; 
    first = second; 
    second = temp;
}

int hoarePartition(int* array, int leftIndex, int rightIndex)
{
    int partition = array[leftIndex];
    int i = leftIndex;
    int j = rightIndex + 1;

    while (i < j)
    {
        while (array[i] < partition && i <= j)
        {
            i = i + 1;
        }
        while (array[j] > partition && j > i)
        {
            j = j - 1;
            cout << j << endl;
        }
        swap(array[i], array[j]);
    }

    swap(array[i], array[j]);
    swap(array[leftIndex], array[j]);

    return j;
}

void randomQuickSort(int* array, int leftIndex, int rightIndex)
{
    if (leftIndex < rightIndex)
    {
        int q = rand() % (rightIndex - leftIndex) + leftIndex;
        swap(array[leftIndex], array[q]);
        int s = hoarePartition(array, leftIndex, rightIndex);
        randomQuickSort(array, leftIndex, s-1);
        randomQuickSort(array, s+1, rightIndex);
    }
}

int main(int argc, char** argv)
{
    srand(time(NULL));
    int size = atoi(argv[1]);
    int* array = new int[size];

    for (int i = 0; i < size; ++i) 
    {
        array[i] = (100.0 * rand()) / RAND_MAX;
    }

    boost::timer t;
    randomQuickSort(array, 0, size);
    std::cout << t.elapsed() << endl;

    delete[] array;

    return 0;
}
#导入
#进口
#进口
#进口
#包括
#包括
无效交换(整数和第一、整数和第二)
{
int temp=第一;
第一=第二;
秒=温度;
}
int-hoarePartition(int*数组、int-leftIndex、int-righindex)
{
int partition=array[leftIndex];
int i=左索引;
int j=右索引+1;
而(ii)
{
j=j-1;

您可以使用
rightIndex
=
size
调用
randomQuickSort
,它比数组中最后一个元素的索引大一个。然后,将其传递给
hoarePartition
,将
j
初始化为
rightIndex+1
,然后(在第二个内部
while
循环中)访问
数组[j] 

使用
rightIndex
=
size
调用
randomQuickSort
,它比数组中最后一个元素的索引大一个。然后,将其传递给
hoarePartition
,将
j
初始化为
rightIndex+1
,然后(在第二个内部
while
循环中)访问
数组[j]

您正在访问
hoarePartition
函数中的size+1。这是数组超出范围的2个元素,导致索引超出范围异常。

您正在访问
hoarePartition
函数中的size+1。这是数组超出范围的2个元素,导致索引超出范围异常on.

这是调试器的优点。这是调试器的优点。因此看起来它应该是
int j=rightIndex-1;
(如果它不执行制动逻辑,则不深入查看)哦,天哪,当然。我现在觉得很可笑;我已经有几个月没有编写代码了,所以我正在重新开始。非常感谢。所以看起来应该是
int j=rightIndex-1;
(如果它没有制动逻辑,就不要深入查看)哦,天哪,当然。我现在觉得很可笑;我已经有几个月没有编过代码了,所以我正在重新开始。非常感谢。