每日一题--20200510--再来一道冒泡排序的题,因为这道题的题目详细讲解了什么是冒泡排序

冒泡排序 【题目就是冒泡的方法讲解】

采用冒泡法进行升序排序法的基本原理是:对数组中的n个数执行n-1遍检查操作,在每一遍执行时,对数组中剩余的尚未排好序的元素进行如下操作:对相邻的两个元素进行比较,若排在后面的数小于排在前面的数,则交换其位置,这样每一遍操作中都将参与比较的数中的最大的数沉到数组的底部,经过n-1遍操作后就将全部n个数按从小到大的顺序排好序了。 #define N 10 程序的某次运行结果如下: Input n:10↙ Input 10 numbers:2 9 3 4 0 6 8 7 5 1↙ Sorting results: 0 1 2 3 4 5 6 7 8 9

输入格式:"%d"
输出格式:
输入数据个数提示:“Input n:”
输入数据提示:“Input %d numbers:”
输出提示:“Sorting results:”
输出格式:"%4d"

#include
#define N 10
void main()
{
	int n,a[N];
	printf("Input n:");
	scanf("%d",&n);
	printf("Input %d numbers:",n);
	int i;
	for(i=0;i<n;i++)
	{
		scanf("%d",a+i);
	}
	int j,temp;
	for(i=0;i<n;i++)
	{
		for(j=i;j<n;j++)
		{
			if(a[j]>a[i])
			{
				temp = a[j];
				a[j] = a[i];
				a[i] = temp;
			}
		}
	}
	printf("Sorting results:");
	for(i=0;i<n;i++)
	{
		printf("%4d",a[i]);
	}
}

结果

Sorting results: 0 1 2 3 4 6 7 9 12 23

你可能感兴趣的:(c语言题,c语言每日一道练习题)