35 lines
821 B
C++
35 lines
821 B
C++
|
/**
|
||
|
* File: selection_sort.cpp
|
||
|
* Created Time: 2023-05-23
|
||
|
* Author: krahets (krahets@163.com)
|
||
|
*/
|
||
|
|
||
|
#include "../utils/common.hpp"
|
||
|
|
||
|
/* 选择排序 */
|
||
|
void selectionSort(vector<int> &nums) {
|
||
|
int n = nums.size();
|
||
|
// 外循环:未排序区间为 [i, n-1]
|
||
|
for (int i = 0; i < n - 1; i++) {
|
||
|
// 内循环:找到未排序区间内的最小元素
|
||
|
int k = i;
|
||
|
for (int j = i + 1; j < n; j++) {
|
||
|
if (nums[j] < nums[k])
|
||
|
k = j; // 记录最小元素的索引
|
||
|
}
|
||
|
// 将该最小元素与未排序区间的首个元素交换
|
||
|
swap(nums[i], nums[k]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* Driver Code */
|
||
|
int main() {
|
||
|
vector<int> nums = {4, 1, 3, 1, 5, 2};
|
||
|
selectionSort(nums);
|
||
|
|
||
|
cout << "选择排序完成后 nums = ";
|
||
|
printVector(nums);
|
||
|
|
||
|
return 0;
|
||
|
}
|