Selection Sort

Selection Sort Visualization
template <typename eType>
void selectionSort(int vector<eType> arr, int size) {
  int i, j, first, temp;
  for (i = size - 1; i > 0; i--) {
    first = 0;
    for (j = 1; j <= i; j++)
      if (arr[j] > arr[first]) first = j;
    temp = arr[first];
    arr[first] = arr[i];
    arr[i] = temp;
  }
}
template <typename eType>
void selectionSort(vector<eType> arr, int size) {
  int i, j, first, temp;
  for (i = 0; i < size; i++) {
    first = i;
    for (j = i + 1; j < size; j++)
      if (arr[j] < arr[first]) first = j;
    temp = arr[first];

    arr[first] = arr[i];
    arr[i] = temp;
  }
}

Last updated

Was this helpful?