修改选择排序模板实现

This commit is contained in:
huihut 2018-03-29 10:41:09 +08:00
parent a70cd27f93
commit f07406ea34
2 changed files with 10 additions and 4 deletions

View File

@ -12,6 +12,10 @@
"tuple": "cpp", "tuple": "cpp",
"system_error": "cpp", "system_error": "cpp",
"xtr1common": "cpp", "xtr1common": "cpp",
"limits": "cpp" "limits": "cpp",
"exception": "cpp",
"fstream": "cpp",
"map": "cpp",
"utility": "cpp"
} }
} }

View File

@ -25,11 +25,13 @@ void SelectionSort(vector<int>& v) {
// 模板实现 // 模板实现
template<typename T> template<typename T>
void Selection_Sort(std::vector<T>& arr) { void Selection_Sort(std::vector<T>& 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; 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]) if (arr[j] < arr[min])
min = j; min = j;
std::swap(arr[i], arr[min]); if(i != min)
std::swap(arr[i], arr[min]);
} }
} }