用C语言实现:将十个数选择排序输出
时间: 2023-06-16 07:08:13 浏览: 51
以下是用C语言实现选择排序并输出的代码:
```c
#include <stdio.h>
void selectionSort(int arr[], int n);
int main() {
int arr[10] = { 10, 3, 5, 1, 7, 2, 8, 9, 4, 6 };
int n = 10;
printf("Before sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
selectionSort(arr, n);
printf("\nAfter sorting: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
void selectionSort(int arr[], int n) {
int i, j, minIndex, temp;
for (i = 0; i < n - 1; i++) {
minIndex = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
```
输出结果:
```
Before sorting: 10 3 5 1 7 2 8 9 4 6
After sorting: 1 2 3 4 5 6 7 8 9 10
```