用选择排序法对10个整数进行排序的c语言代码
时间: 2023-05-31 11:07:59 浏览: 166
以下是用选择排序法对10个整数进行排序的C语言代码:
```
#include <stdio.h>
void selection_sort(int arr[], int n) {
int i, j, min_idx;
// One by one move boundary of unsorted subarray
for (i = 0; i < n-1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[10] = {64, 25, 12, 22, 11, 90, 13, 87, 45, 50};
int n = sizeof(arr)/sizeof(arr[0]);
selection_sort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
阅读全文