I am new to this forum and this is my first topic. I have a question in regards to the pseudocode of insertion sort. I am new to programming and currently enrolled in a Computer science degree. Anyway, I find my lecturer's pseudocode very hard to understand. Can anyone explain the following pseudocode to me clearly? I really do not get what is the wallIndex in the pseudocode.
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
Algorithm:insertionSort
BEGIN insertionSort (array, N)
INITIALISE wallIndex to 1
WHILE (wallIndex <= N-1)
SET temp to array[wallIndex]
SET index to wallIndex
WHILE (index>0 AND array[index-1]>temp)
SET array[index] to array[index-1];
DECREMENT index
ENDWHILE
SET array[index] to temp;
INCREMENT wallIndex
ENDWHILE
RETURN array
END insertionSort
Does this help?
Functionally the wallIndex is the same as the index, they are both pointers which index an item in the array list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
BEGIN insertionSort (array, N) array is a list of N items
wallIndex = 1
WHILE (wallIndex <= N-1)
temp = array[wallIndex]
index = wallIndex
WHILE (index>0 AND array[index-1]>temp)
array[index] = array[index-1]
index = index - 1
ENDWHILE
array[index] = temp
wallIndex = wallIndex + 1
ENDWHILE
RETURN array
END insertionSort
Or maybe it will help if can compare it with a C++ version?