mirror of
https://github.com/huihut/interview.git
synced 2025-12-18 13:04:38 +03:00
更新选择排序
This commit is contained in:
17
Algorithm/SelectionSort.h
Normal file
17
Algorithm/SelectionSort.h
Normal file
@@ -0,0 +1,17 @@
|
||||
// 选择排序
|
||||
void SelectionSort(vector<int>& v) {
|
||||
int min, temp;
|
||||
for (int i = 0; i < v.size() - 1; ++i) {
|
||||
min = i;
|
||||
for (int j = i + 1; j < v.size(); ++j) {
|
||||
if (v[j] < v[min]) { // 标记最小的
|
||||
min = j;
|
||||
}
|
||||
}
|
||||
if (i != min) { // 交换到前面
|
||||
temp = v[i];
|
||||
v[i] = v[min];
|
||||
v[min] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user