c++用选择法对 10个整数排序
时间: 2024-05-25 11:06:29 浏览: 62
选择法对十个数排序
5星 · 资源好评率100%
以下是使用C++实现选择排序对10个整数排序的示例代码:
```cpp
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n-1; i++) {
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(arr[min_idx], arr[i]);
}
}
int main() {
int arr[] = {64, 25, 12, 22, 11, 1, 2, 3, 4, 5};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
cout << "Sorted array: \n";
for (int i=0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
```
输出结果为:
```
Sorted array:
1 2 3 4 5 11 12 22 25 64
```
阅读全文