Sort ten integers using selection

Use an array to hold a set of numbers.

The so-called selection method is to first exchange the smallest number among the 10 numbers with a[0], then exchange the smallest number among a[1]~a[9] with a[1], and so on.

For each round of comparison, find the smallest one among the unsorted numbers, a total of 9 rounds of comparison.

#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 Results:

Supongo que te gusta

Origin blog.csdn.net/jiang1126/article/details/125340565
Recomendado
Clasificación