当前位置:网站首页>Sort ten integers using selection

Sort ten integers using selection

2022-06-22 05:07:00 Jiangxueqian

Use an array to hold a set of numbers .

The so-called selective method , It's about putting 10 The smallest of the numbers is the same as a[0] In exchange for , then a[1]~a[9] The smallest number and number in a[1] In exchange for , And so on .

Every round of comparison , Find the smallest unordered number , A total comparison 9 round .

#include <stdio.h>

void sort(int a[])
{
	int temp;
	for (int i = 0; i < 10; i++)
	{
		for (int j = i+1; j < 10; j++)
		{
			if (a[j] < a[i])
			{
				temp = a[i];
				a[i] = a[j];
				a[j] = temp;
			}
		}
	}
}

int main()
{
	int a[10];

	for (int i = 0; i < 10; i++)
	{
		scanf("%d", &a[i]);
	}

	sort(a);

	for (int j = 0; j < 10; j++)
	{
		printf("%-3d", a[j]);
	}
    return 0;
}

test result :

原网站

版权声明
本文为[Jiangxueqian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206220503534170.html