Insertion Sort

Insertion Sort Visualization
template <typename eType>
void insertionSort(vector<eType> arr, int size) {
  int j, temp;
  for (int i = 1; i < size; i++) {
    j = i;
    while (j > 0 && arr[j] < arr[j - 1]) {
      temp = arr[j];
      arr[j] = arr[j - 1];
      arr[j - 1] = temp;
      j--;
    }
  }
}

Last updated

Was this helpful?