排序的四种算法

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>

// 生成 num 个范围在 0~max 之间的整数
void CreateInt(int *p, int num, int max);

// 演示排序函数
int ShowSort(int *p, int num, HANDLE hOut, WORD corBase);

void CompareSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed);
void SelectSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed);
void InsertSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed);
void PopSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed);

int main(int argc ,char **argv)
{
	srand((unsigned int)time(NULL));
	HANDLE hOut;   //  typedef void *HANDLE;
	WORD corBase = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE |  
		FOREGROUND_INTENSITY; //  typedef unsigned short      WORD;
	hOut = GetStdHandle(STD_OUTPUT_HANDLE);//返回标准的输入、输出或错误的设备的句柄,也就是获得输入、输出/错误的屏幕缓冲区的句柄
	SetConsoleTextAttribute(hOut, corBase);

	int num[10];
	CreateInt(num, 10, 100);

	while (ShowSort(num, 10, hOut, corBase));

	getchar();
	getchar();
	return 0;
}

// 生成 num 个范围在 0~max 之间的整数
void CreateInt(int *p, int num, int max)
{
	puts("生成10个随机数,如下:");
	for (int i=0; i<10; i++)
	{
		p[i] = rand()%max;
		printf("%d ", p[i]);
	}
	puts("\n");
}

// 四种排序函数
int ShowSort(int *p, int num, HANDLE hOut, WORD corBase)
{
	SetConsoleTextAttribute(hOut, corBase);

	// 选择排序方法
	int index;
	puts("请选择需要演示的排序方法:");
	puts("1. 比较法 2. 选择法 3. 插入法 4. 冒泡法 0. 退出");
	scanf("%d", &index);
	puts("");
	if (index == 0)
		return index;

	// 设置 演示 属性
	int n;
	WORD color;
	puts("选择字体颜色:1. 红色 2. 绿色 3. 蓝色");
	scanf("%d", &n);
	if (n == 1)
		color = FOREGROUND_RED | FOREGROUND_INTENSITY;
	else if (n == 2)
		color = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
	else
		color = FOREGROUND_BLUE | FOREGROUND_INTENSITY;

	puts("选择演示速度:1. 快 2. 中 3. 慢");
	scanf("%d", &n);
	puts("");

	// 判断排序方式,执行演示和排序
	switch (index)
	{
	case 1:				// 比较法排序
		CompareSort(p, num, hOut, corBase, color, n);
		break;
	case 2:				// 选择法排序
		SelectSort(p, num, hOut, corBase, color, n);
		break;
	case 3:				// 插入法排序
		InsertSort(p, num, hOut, corBase, color, n);
		break;
	case 4:				// 冒泡法排序
		PopSort(p, num, hOut, corBase, color, n);
		break;
	default:
		break;
	}

	return index;
}

// 比较法排序
void CompareSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed)
{
	int res[10];
	for (int i=0; i<num; i++)
		res[i] = p[i];

	puts("比较法排序过程,如下:");
	printf("            ");
	for (int i=0; i<num; i++)
		printf("%d ", p[i]);
	puts("");

	for (int i=0; i<num-1; i++)
	{
		printf("第%d轮 结果:", i+1);

		int tag = 0;
		for (int j=i+1; j<num; j++)
		{
			if (res[i] > res[j])
			{
				int tmp = res[i];
				res[i] = res[j];
				res[j] = tmp;

				// 输出
				if (tag == 0)
					tag = 1;
				else
				{
					printf("            ");
				}

				for(int k=0; k<num; k++)
				{
					if (k <= i)
						SetConsoleTextAttribute(hOut, color);
					else
						SetConsoleTextAttribute(hOut, corBase);
					printf("%d ", res[k]);
				}
				puts("");

				Sleep(speed*700);
			}
		}		
	}
	puts("演示完毕\n");
}

// 选择法排序
void SelectSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed)
{
	int res[10];
	for (int i=0; i<num; i++)
		res[i] = p[i];

	puts("选择法排序过程,如下:");
	printf("            ");
	for (int i=0; i<num; i++)
		printf("%d ", p[i]);
	puts("");

	for (int i=0; i<num-1; i++)
	{
		printf("第%d轮 结果:", i+1);
		int min=i;
		for (int j=i; j<num; j++)
		{
			if (res[min] > res[j])
				min = j;
		}

		// 交换
		int tmp = res[i];
		res[i] = res[min];
		res[min] = tmp;

		// 输出
		for(int k=0; k<num; k++)
		{
			if (k <= i)
				SetConsoleTextAttribute(hOut, color);
			else
				SetConsoleTextAttribute(hOut, corBase);
			printf("%d ", res[k]);
		}
		puts("");

		Sleep(speed*700);
	}
	puts("演示完毕\n");
}

// 插入法排序
void InsertSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed)
{
	int res[11];
	for (int i=0; i<num+1; i++)
		res[i] = -1;

	puts("插入法排序过程,如下:\n");
	printf("            ");
	for (int i=0; i<num; i++)
		printf("%d ", p[i]);
	puts("");

	for (int i=0; i<num; i++)
	{
		printf("第%d轮 结果:", i+1);
		for(int j=i; j>=0; j--)
		{
			if (res[j] < p[i])
			{
				res[j+1] = p[i];
				break;
			}
			else
				res[j+1] = res[j];
		}

		// 输出
		for(int k=1; k<num+1; k++)
		{
			if (k <= i+1)
				SetConsoleTextAttribute(hOut, color);
			else
				SetConsoleTextAttribute(hOut, corBase);
			if (res[k] != -1)
				printf("%d ", res[k]);
		}
		puts("");

		Sleep(speed*700);
	}
	puts("演示完毕\n");
}

// 冒泡法排序
void PopSort(int *p, int num, HANDLE hOut, WORD corBase, WORD color, int speed)
{
	int res[10];
	for (int i=0; i<num; i++)
		res[i] = p[i];

	puts("冒泡法排序过程,如下:\n");
	printf("            ");
	for (int i=0; i<num; i++)
		printf("%d ", p[i]);
	puts("");

	int tag = 0, tag1 = 0;
	for(int i=0; i<num-1; i++)
	{
		SetConsoleTextAttribute(hOut, corBase);
		printf("第%d轮 结果:", i+1);
		tag = tag1 = 0;
		for (int j=0; j<num-i-1; j++)
		{
			if (res[j] > res[j+1])
			{
				int tmp = res[j+1];
				res[j+1] = res[j];
				res[j] = tmp;
				tag = 1;

				// 输出
				if (tag1 == 0)
					tag1 = 1;
				else
				{
					printf("            ");
				}

				for(int k=0; k<num; k++)
				{
					if (k==j+1 || k>(num-i-1))
						SetConsoleTextAttribute(hOut, color);
					else
						SetConsoleTextAttribute(hOut, corBase);
					printf("%d ", res[k]);
				}
				puts("");

				Sleep(speed*700);
			}
		}
		if (tag == 0)
		{
			puts("");
			break;
		}	
	}
	puts("演示完毕\n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值