You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
902 B
37 lines
902 B
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
//选择排序
|
|
void SelectSort(float a[], int length) {
|
|
//对数组a排序,length是数组元素数量
|
|
for (int i = 0; i < length; i++) {
|
|
// 找到从i开始到最后一个元素中最小的元素,k存储最小元素的下标.
|
|
int k = i;
|
|
for (int j = i + 1; j < length; j++) {
|
|
if (a[j] < a[k]) { k = j; }
|
|
}
|
|
// 将最小的元素a[k] 和 开始的元素a[i] 交换数据.
|
|
if (k != i) {
|
|
float temp;
|
|
temp = a[k];
|
|
a[k] = a[i];
|
|
a[i] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
float a[5] = {0};
|
|
for (int i = 0; i < 5; ++i) {
|
|
cin >> a[i];
|
|
}
|
|
//执行选择排序
|
|
SelectSort(a, 5);
|
|
//输出结果
|
|
for (int i = 0; i < 5; ++i) {
|
|
cout << a[i] << " ";
|
|
}
|
|
return 0;
|
|
}
|