用c++写一段选择排序
时间: 2023-03-15 12:31:33 浏览: 103
我可以为您编写一段C语言选择排序的程序:#include <stdio.h>
int main()
{
int array[100], n, c, d, position, swap;
printf("输入元素个数\n");
scanf("%d", &n);
printf("输入 %d 个元素\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("排序后的结果:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
阅读全文