cpp-library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub 9tc/cpp-library

:x: 選択ソート
(sort/selection-sort.hpp)

Verified with

Code

#pragma once
template <class T>
int selectionSort(vector<T> &target){
  int n = target.size();
  int res = 0;

  for(int i = 0; i < n-1; ++i){
    int it = min_element(target.begin() + i, target.end()) - target.begin();
    if(i != it){
      ++res;
      swap(target[i], target[it]);
    }
  }
  return res;
}
#line 2 "sort/selection-sort.hpp"
template <class T>
int selectionSort(vector<T> &target){
  int n = target.size();
  int res = 0;

  for(int i = 0; i < n-1; ++i){
    int it = min_element(target.begin() + i, target.end()) - target.begin();
    if(i != it){
      ++res;
      swap(target[i], target[it]);
    }
  }
  return res;
}
Back to top page