提高篇第26-27课第三题

#include<stdio.h>
#include<stdlib.h>
//第三体,整型,字符型冒泡排序,选择排序
void bubble_sort(int array[],int len);
void output_array(int array[], int len);
void char_bubble_sort(char c[], int len);//字符数组排序
void char_output(char c[], int len);//字符数组输出
void select_sort(int array[], int len);//选择排序
int main()
{
	int a[20] = { 86, 76, 62, 58, 77, 85, 92, 80, 96, 88, 77, 67, 80, 68, 88, 87, 64, 59, 61, 76 };
	int b[15] = { 27, 61, 49, 88, 4, 20, 28, 31, 42, 62, 64, 14, 88, 27, 73 };
	char c[20] = { 's', 'o', 'r', 't', 'b', 'u', 'b', 'b', 'l', 'e', 's', 'e', 'l', 'e', 'c', 't', 'o', 'k', 'o', 'k' };
	char d[15] = { 'a', 'b', 'a', 's', 'o', 'r', 't', 'b', 'u', 'b', 'b', 'l', 'e', 's', 'e' };  //自己补足
	bubble_sort(a, 20);   //用冒泡法按降序排序a中元素
	output_array(a, 20);   //输出排序后的数组
	bubble_sort(b, 15);   //用冒泡法按降序排序b中元素
	output_array(b, 15);   //输出排序后的数组
	char_bubble_sort(c, 20);
	char_output(c,20);
	char_bubble_sort(d, 15);
	char_output(d, 15);
	select_sort(a, 20);
	output_array(a, 20);
	return 0;
}
void bubble_sort(int array[], int len)
{
	int i,j,temp;
	for (i = 0; i < len - 1; i++)
		for (j = 0; j<len-i-1; j++)
		{
			if (array[j] < array[j+1])
			{
				temp = array[j];
				array[j] = array[j+1];
				array[j+1] = temp;
			}
		}
}
void output_array(int array[], int len)
{
	int i;
	for (i = 0; i < len; i++)
		printf("%d ", array[i]);
	printf("\n");
}
void char_bubble_sort(char c[], int len)
{
	int  i;
	int *array;
	array = (int*)malloc(len*sizeof(int));//动态定义len大小的整型数组
	for (i = 0; i < len; i++)
		array[i] = c[i];
	bubble_sort(array, len);
	for (i = 0; i < len; i++)
		c[i] = array[i];
	free(array);
}
void char_output(char c[], int len)
{
	int i;
	for (i = 0; i < len; i++)
		printf("%c ", c[i]);
	printf("\n");
}
void select_sort(int array[],int len)
{
	int i,j,temp;
	for (i = 0; i < len; i++)
		for (j = i; j < len;j++)
			if (array[j] < array[i])
			{
				temp = array[j];
				array[j] = array[i];
				array[i] = temp;
			}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值