C++ 如何实现快速排序?

C++ 如何实现快速排序?,c++,C++,我正在使用Splash工具包库 #include "splashkit.h" #define NUM_VALS 100 //drawing 100 random values in chart void draw_values(const int values[], int size) { int x = 0; int y; int rect_height; int rect_width = screen_width() / size; for (i

我正在使用Splash工具包库

#include "splashkit.h"

#define NUM_VALS 100
//drawing 100 random values in chart
void draw_values(const int values[], int size) 
{
    int x = 0;
    int y;
    int rect_height;
    int rect_width = screen_width() / size;

    for (int i = 0; i < size; i++)
    {
        rect_height = values[i];
        y = screen_height() - rect_height;

        fill_rectangle(COLOR_RED, x, y, rect_width, rect_height);
        draw_rectangle(COLOR_WHITE, x, y, rect_width, rect_height);

        x += rect_width;
    }
}
//swap procedure
void swap (int &value1, int &value2)
{
    int temp = value1;
    value1 = value2;
    value2 = temp;
}
//draw array values window
void draw_sort(int values[], int size)
{
    clear_screen(COLOR_WHITE);
    draw_values(values, size);
    refresh_screen(60);
}
//bubble sort code using above procedures
void bubble_sort (int values[], int size)
{
    for (int j =0; j < size; j++)
    {
        for (int i =0; i < size - 1; i++ )
        {
            if (values[i] > values[i + 1])
            {
                swap(values[i], values[i + 1]);
                draw_sort(values, size);
            }
        }
    }
}
// i think something wrong in this function only
void quick_sort(int values[], int size) {
      int i = values[size + 1] , j = values[size - 1] ;
      int tmp;
      int pivot = values[(i + j) / 2];

      /* partition */
      while (i <= j) {
            while (values[i] < pivot)
                  i++;
            while (values[j] > pivot)
                  j--;
            if (i <= j) {
                  tmp = values[i];
                  values[i] = values[j];
                  values[j] = tmp;
                  i++;
                  j--;
            }
      };

      /* recursion */
      if (i < j)
            quick_sort(values, i);
      if (i < j)
            quick_sort(values, j);
}
// randomising array values to sort
void random_fill_array(int values[], int size)
{
    for (int i = 0; i < size; i++) 
    {
        values[i] = rnd(screen_height()) + 1;
    }
}
//to test each sort
void handle_input(int values[], int size)
{
    if (key_typed(R_KEY))
    {
        random_fill_array(values, size);
    }
    else if (key_typed(S_KEY))
    {
        bubble_sort(values, size);
    }
    else if (key_typed(D_KEY))
    {
        quick_sort(values, size);
    }
}

int main()
{
    int values[NUM_VALS];

    open_window("Sort Visualiser", 800, 600);

    random_fill_array(values, NUM_VALS);

    while ( not quit_requested() )
    {
        process_events();
        handle_input(values, NUM_VALS);

        draw_sort(values, NUM_VALS);
    }

    return 0;
}
我克服了以前的问题,但现在我遇到了一个新问题。这不是我的任务。我只是开始了解各种各样的东西;请告诉我哪里出错了


我试着在我现在运行时修复它,如果我按S我的气泡>排序是>获取排序,但是当我按D应用程序崩溃时,有人能发现我哪里出了问题吗?

这看起来很可疑:

/* recursion */
 if (i < j)
      quick_sort(values, i);
 if (i < j)
      quick_sort(values, j);
两种情况下的条件完全相同

Partition有两个参数Partitionint value[],int size,您用3来调用它:partitionvalue,low,high。这不是太多参数的意思吗?在分区函数中,使用未初始化的变量low和high,而从不使用参数大小。也许它应该把它们作为参数,而不是将它们声明为局部变量?交换&value[high]、&value[pvt];将交换指向值的指针,而不是值。为什么不使用std::swap呢?另外,我相信随机调用可以使pindex具有极值,使pindex-1或pindex+1的递归调用超出范围。swap使用引用,而不是指针。您成功地在冒泡排序中获得了正确的调用。请通过您的问题向我们展示您所做的调试。我希望您已经在Valgrind或类似的检查器中运行了自己的程序,并且已经使用诸如GDB之类的调试器进行了调查。确保还启用了一整套编译器警告。这些工具告诉了你什么,它们遗漏了什么信息?读埃里克·利珀特的。