选择法对10个数进行升序排列.c

Select sort of thinking is as follows :
(1)Each element of the input array A;
(2) Enter the number of control, and keep in min ( for loops ) ;
(3)a[j ] For a[i ] The latter, more a[min ] and a[j ], ensuring a[min with exchange ] The value of the minimum ( if selected structure ) ;
(4) output.

//  date:2020/3/3
//  author:xiezhg5
#include <stdio.h>
int main(void)
{
	int i,j,min,temp,a[11];     //temp交换的中间变量,a[11]存放数据 
	printf("请输入数据:\n");
	for(i=1;i<=10;i++)        //剔除a[0],符合习惯
	//for循环控制输入 
	{
		printf("a[%d]=\n",i);
		scanf("%d",&a[i]);
	}
	printf("您输入的原始数据为:\n");
	//for循环控制输出 
	for(i=1;i<=10;i++)
	printf("%5d\n",a[i]);
	for(i=1;i<=9;i++)
	{
		min=i;                      //min储存输入的i的值 
		for(j=i+1;j<=10;j++)       //a[i]的后一个数a[j] 
		if(a[min]>a[j])  min=j;   //保证a[min]储存最小值 
		//交换a[i]和a[min]的值 
		temp=a[i];
		a[i]=a[min];
		a[min]=temp;   //a[i]储存最小值 
	}
	printf("\n排序后的数字为:\n");
	//for循环控制输出 
	for(i=1;i<=10;i++)
	printf("%5d\n",a[i]);     //宽度为5列 
	return 0;
}
发布了30 篇原创文章 · 获赞 10 · 访问量 303

猜你喜欢

转载自blog.csdn.net/qq_45645641/article/details/104632658