c++ - 使用快速排序在 C++ 中排序可视化器错误
问题描述
因此,当我使用名为 splashkit 的游戏制作库创建此排序可视化器时(我知道它并不理想,但这是我的课程教给我的)我试图显示整个数组并显示每个单独的交换。但相反,它正在这样做:我的错误视频
我没有理解我的代码逻辑,因为我按照教程来实现这一点。排序本身很好,效果很好,但是矩形的绘制很奇怪,而不是我试图实现的目标。 我想实现这样的目标。(没有颜色/声音效果)。代码更新:
#include "splashkit.h"
#define NUM_VALS 200
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;
}
}
void draw_sort(int values[], int size)
{
clear_screen(COLOR_WHITE);
draw_values(values, size);
refresh_screen(60);
}
void swap (int &value1, int &value2)
{
int temp = value1;
value1 = value2;
value2 = temp;
}
/* inspiration/educated from https://www.geeksforgeeks.org/quick-sort/ */
int partition (int values[], int low, int size)
{
int pivot = values[size]; // the pivot value
int i = (low - 1); // currently selected element
// work out if all values have become the pivot value, loop until all have.
for (int j = low; j <= size-1; j++)
{
if (values[j] <= pivot)
{
i++;
swap(values[i], values[j]);
draw_sort(values, size);
}
}
swap(values[i + 1], values[size]);
draw_sort(values, size);
return (i+1);
}
void quick_sort (int values[], int low, int size)
{
if (low < size)
{
// This is the partitioning index for quick sorting
int pi = partition(values, low, size);
// This sorts small partitions at a time then sorts them together.
quick_sort(values, low, (pi - 1));
quick_sort(values, (pi + 1), size);
}
}
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);
}
}
}
}
void random_fill_array(int values[], int size)
{
for (int i = 0; i < size; i++)
{
values[i] = rnd(screen_height()) + 1;
}
}
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, 0, 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;
}
解决方案
在quick_sort
函数内部,size
列表的大小不是当前分区的大小,因此您在调用时只绘制当前分区而不是整个列表draw_sort
。您需要添加具有原始列表大小的额外参数:
int partition (int values[], int low, int partitionSize, int size)
{
int pivot = values[partitionSize]; // the pivot value
int i = (low - 1); // currently selected element
// work out if all values have become the pivot value, loop until all have.
for (int j = low; j <= partitionSize-1; j++)
{
if (values[j] <= pivot)
{
i++;
swap(values[i], values[j]);
draw_sort(values, size);
}
}
swap(values[i + 1], values[partitionSize]);
draw_sort(values, size);
return (i+1);
}
void quick_sort (int values[], int low, int partitionSize, int size)
{
if (low < partitionSize)
{
// This is the partitioning index for quick sorting
int pi = partition(values, low, partitionSize, size);
// This sorts small partitions at a time then sorts them together.
quick_sort(values, low, (pi - 1), size);
quick_sort(values, (pi + 1), partitionSize, size);
}
}
推荐阅读
- java - TreeCellRenderer 中的标签宽度不正确
- javascript - 在浏览器(vuex + axios)加载时,带有多个 JSON 文件的模拟数据似乎被覆盖
- ruby-on-rails - Rails 6.0.0 TypeError - 没有将 nil 隐式转换为字符串:
- redirect - 如何在 HAProxy 中进行内部重定向?
- python - 如何将文件从网站下载到 S3 存储桶而无需下载到本地机器
- java - 如何设置 okhttp 3 线程池大小
- c# - Oracle.ManagedDataAccess.Core 和 ORA-01017:用户名/密码无效;登录被拒绝
- forms - Symfony:处理请求时出现错误?
- javascript - 反应原生
onDismiss 未调用 - php - 在 laravel 中编辑类别时如何显示特定类别的先前值?