c++实现选择排序算法
时间: 2023-11-06 19:00:56 浏览: 92
C++实现选择排序(选择排序)
选择排序算法的C语言实现如下:
```c
#include <iostream>
using namespace std;
void sort(int *arr, int length) {
int index;
int temp;
for (int i = 0; i < length - 1; i++) {
index = i;
for (int j = i + 1; j < length; j++) {
if (arr[j] < arr[index])
index = j;
}
if (arr[index] < arr[i]) {
temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
int main() {
int arr[] = {12, 12, 54, 1745, 45, 45, 469, 789, 789};
int length = sizeof(arr) / sizeof(arr[0]);
sort(arr, length);
for (size_t i = 0; i < length; i++)
cout << arr[i] << " ";
return 0;
}
```
阅读全文