使用快速排序对 C++ 中的可视化工具错误进行排序
sort visualizer bug in c++ using quicksort
所以当我使用一个名为 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);
}
}
相关文章:
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- Visual Studio 2013,错误 MSB8020:找不到 Visual Studio 2010 的生成工具
- 如何使用CLion和MSVC编译器工具链编译wxWidgets Hello World应用程序而没有错误?
- 错误:必须设置CMake工具才能生成.在工具包选项中配置CMake工具
- VS 2017 构建工具失败,出现错误 MSB4019:找不到导入的项目"D:Microsoft.Cpp.Default.props"
- 使用自定义工具集获取动态退出析构函数链接错误 - eh 矢量析构函数
- Jetson 工具链文件夹结构和交叉编译时的 libgomp 链接器错误
- 为什么在链接时搜索使用错误工具集的提升库?
- 为什么使用适用于C++的 AWS 开发工具包创建 EC2Client 段错误
- C2064 从 MSVC v100 更新到 v140 平台工具集时出现编译错误
- 尝试将Winelib与标准macOS构建工具一起使用:得到奇怪的编译器错误
- 使用Linaro工具链编译CPP文件时出现错误
- 提升链接器错误工具集
- NDK r16b 独立工具链错误:"/usr/bin/ld: this linker was not configured to use sysroots"
- Buildroot 包需要一个带有C++大文件错误的工具链
- 编程原理和实践第27章具有工具性清单错误
- 致命错误C1083在IDL文件上使用OPENDDS_IDL工具时
- Visual Studio 14.0 和 Windows 工具包中的错误包括文件夹
- Qt 5测试版2,Qt Creator 2.6,MSVC错误:Qt Creaator需要一个编译器来构建.在工具包选项中
- std::unique_ptr 是为数组分配内存的错误工具吗?