diff --git a/.vscode/settings.json b/.vscode/settings.json index e627288..2562181 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -12,6 +12,10 @@ "tuple": "cpp", "system_error": "cpp", "xtr1common": "cpp", - "limits": "cpp" + "limits": "cpp", + "exception": "cpp", + "fstream": "cpp", + "map": "cpp", + "utility": "cpp" } } \ No newline at end of file diff --git a/Algorithm/SelectionSort.h b/Algorithm/SelectionSort.h index 47721f9..f6cd6b5 100644 --- a/Algorithm/SelectionSort.h +++ b/Algorithm/SelectionSort.h @@ -25,11 +25,13 @@ void SelectionSort(vector& v) { // 模板实现 template void Selection_Sort(std::vector& arr) { - for (int i = 0; i < arr.size() - 1; i++) { + int len = arr.size(); + for (int i = 0; i < len - 1; i++) { int min = i; - for (int j = i + 1; j < arr.size(); j++) + for (int j = i + 1; j < len; j++) if (arr[j] < arr[min]) min = j; - std::swap(arr[i], arr[min]); + if(i != min) + std::swap(arr[i], arr[min]); } } \ No newline at end of file