While I was working on the PSet3, I've realized that I don't really understand the insertion sort algorithm the way it is presented in one of the short CS50 videos. So I commented out each line of the algorithm. Could you please check if my comments are correct? Thank you in advance!
for (int i = 1; i < k; i++) // Iterate over each integer in the given array
{
int temp = array[i]; // Select the (next) unsorted element and save in temp
int j = i; // int j holds the position where the unsorted element is inserted
while (j > 0 && temp < array[j-1]) // Check if previous element is larger than the element to be inserted in the sorted array
{
array[j] = array[j-1]; // If the previous element is larger, shift it by one position
j--; // Decrement the number of remaining integers in the sorted array
}
array[j] = temp; // Insert the element in the proper position in the sorted array
}