简介
冒泡排序相信大家都听说过(没听说过点这里), 这个程序可以让冒泡排序可视化。
图片
功能
将冒泡排序的过程 生动形象 的表现出来,同时能够统计 时间(Used Time),交换次数(Swaped)、比较次数(Compared)三种重要的信息。
使用方法
当屏幕上出现 Please Input Nums(<50) 时, 输入数字数量,程序会自动生成数据并开始排序。
代码
#include <cstdio>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <windows.h>
using namespace std;
enum concol {
black = 0,
dark_blue = 1,
dark_green = 2,
dark_aqua = 3, dark_cyan = 3,
dark_red = 4,
dark_purple = 5, dark_pink = 5, dark_magenta = 5,
dark_yellow = 6,
dark_white = 7,
gray = 8,
blue = 9,
green = 10,
cyan = 11,
red = 12,
purple = 13, pink = 13,
yellow = 14,
white = 15
};
struct pixel {
concol color;
};
CONSOLE_CURSOR_INFO CursorInfo;
COORD _GoToPos;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
int backcol, textcol;
pixel _LastScreen[1000][1000];
pixel _NewScreen[1000][1000];
int _ScreenSideLength;
inline void gt(short x, short y) {
--x;
--y;
_GoToPos = {x * 2, y};
SetConsoleCursorPosition(hOut, _GoToPos);
}
inline void settextcolor(concol textcolor) {
textcol = textcolor;
unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
SetConsoleTextAttribute(hOut, wAttributes);
}
inline void setbackcolor(concol backcolor) {
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
backcol = backcolor;
unsigned short wAttributes = ((unsigned int)backcol << 4) | (unsigned int)textcol;
SetConsoleTextAttribute(hOut, wAttributes);
}
inline void init(int x, int fz) {
GetConsoleCursorInfo(hOut, &CursorInfo);
CursorInfo.bVisible = false;
SetConsoleCursorInfo(hOut, &CursorInfo);
_ScreenSideLength = x;
}
inline void update() {
for (int i = 1; i <= _ScreenSideLength ; ++i) {
for (int j = 1; j <= _ScreenSideLength ; ++j) {
if (_LastScreen[j][i].color != _NewScreen[j][i].color) {
gt(j, i);
setbackcolor(_NewScreen[j][i].color);
putchar(' ');
putchar(' ');
_LastScreen[j][i] = _NewScreen[j][i];
}
}
}
}
void fill(concol color) {
for (int i = 1; i <= _ScreenSideLength; ++i) {
for (int j = 1; j <= _ScreenSideLength; ++j) {
if (_NewScreen[j][i].color != color) {
_NewScreen[j][i].color = color;
}
}
}
}
void rect(int sx, int sy, int ex, int ey, concol color) {
if (ey < sy) swap(ey, sy);
if (ex < sx) swap(ex, sx);
for (int i = sy; i <= ey; ++i) {
for (int j = sx; j <= ex; ++j) {
_NewScreen[j][i].color = color;
}
}
}
inline void line(int sx, int sy, int ex, int ey, concol color)
{
int xlen = ex - sx ;
int ylen = ey - sy ;
int len = sqrt(pow(xlen, 2) + pow(ylen, 2)) ;
for(double i=0; i<=len; i += 1)
{
int x = xlen * i / len ;
int y = ylen * i / len ;
_NewScreen[x + sx][y + sy].color = color ;
}
}
int n, a[10050], _swap, _compare;
int st = clock();
void valline(int i, concol color)
{
line(i, a[i], i, 50, color);
}
void Out(int i)
{
for(int K=1;K<=n;++K)
{
if(K + i > n+2)
{
valline(K, K&1 ? dark_green : green);
}
else
{
if(K&1)
{
line(K, a[K], K, 50, red);
}
else
{
line(K, a[K], K, 50, dark_red);
}
}
}
}
void Outdata()
{
setbackcolor(black);
settextcolor(white);
int et = clock();
gt(1, 54);
printf("Used Time : %.3lf", (et-st) / 1000.00);
gt(20, 54);
printf("Swaped : %d", _swap);
gt(40, 54);
printf("Compared : %d", _compare);
}
bool v[10050];
int main()
{
srand(time(NULL));
init(100, 16);
system("mode con cols=202 lines=106");
printf("Please Input Nums(<50)\n");
scanf("%d", &n);
++n;
system("cls");
for(int i=1; i<=n; ++i)
{
a[i] = rand() % n + 1;
while(v[a[i]])
{
a[i] = rand() % n + 1;
}
v[a[i]]=1;
}
for(int i=1;i<=n;++i)
{
for(int j=1;j+i<=n+1;++j)
{
fill(black);
Out(i);
line(j, a[j], j, 50, yellow);
line(j+1, a[j+1], j+1, 50, yellow);
update();
Outdata();
++_compare;
if(a[j] > a[j+1])
{
++_swap;
rect(j, 0, j+1, 50, black);
swap(a[j+1], a[j]);
line(j, a[j], j, 50, yellow);
line(j+1, a[j+1], j+1, 50, yellow);
update();
Outdata();
}
}
}
Out(n+2);
update();
setbackcolor(black);
getchar();
getchar();
}