如何实现快速排序?

时间:2017-09-22 09:18:51

标签: 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 (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”我的气泡&gt;我现在跑的时候试过修理它sort是&gt;得到排序,但当我按“D”应用程序崩溃时,任何人都可以看到我在哪里出错?

1 个答案:

答案 0 :(得分:1)

这看起来很可疑:

  class HelloWidget extends React.Component{
    constructor(props) {
      super(props);

      this.state = {
        activeTabIndex: 0
      };
    }

    changeTab(newActiveTabIndex) {
      this.setState({
        activeTabIndex: newActiveTabIndex
      });
    }

    render() {
            const { activeTabIndex } = this.state;

        return (
            <div>
              <ul>
                  <li
                    className={activeTabIndex === 0 ? 'active' : ''}
                    onClick={this.changeTab.bind(this, 0)}
                  >
                    Tab 1
                  </li>
                  <li
                    className={activeTabIndex === 1 ? 'active' : ''}
                    onClick={this.changeTab.bind(this, 1)}
                  >
                    Tab 2
                  </li>
                  <li
                    className={activeTabIndex === 2 ? 'active' : ''}
                    onClick={this.changeTab.bind(this, 2)}
                  >
                    Tab 3
                  </li>
              </ul>
          </div>
        )
    }
  }

  React.render(<HelloWidget />, document.getElementById('container'));

两种情况下的条件完全相同