In this insertion sort implementation, is the line arr[pre + 1] = curr; optional?
Since we are already swapping elements inside the while-loop, won't the current element automatically reach its correct position without explicitly assigning it at the end?”
void insertionSort(int* arr, int n) {
for (int i = 1; i < n; i++) {
int curr = arr[i];
int pre = i - 1;
while (pre >= 0 && arr[pre] > curr) {
swap(arr[pre], arr[pre + 1]);
pre--;
}
// arr[pre+1] = curr; is this line optional ?
}
}
int* arr, int nC++ has std::span for this. Or use std::vector<int>&. Do not represent arrays by passing around pointers (that's so pre C++98)