I’m reading some books on Python, data structures, and analysis and design of algorithms. I want to really understand the in’s and out’s of coding, and become an efficient programmer. It’s difficult to ask the book to clarify, hence my question on stackoverflow. I’m really finding Algorithms and recursion challenging … I posted some code (insertion sort) below that I’m trying to understand exactly what’s happening. I understand, generally, what is supposed to happen, but I’m not really getting the how and why.
From trying to analyze pieces of the code on Python Idle, I know that:
key (holds variables) = 8, 2, 4, 9, 3, 6
and that:
i (holds the length) = 7 ( 1, 2, 3, 4, 5, 6, 7)
I don’t know why 1 is used in the first line: range(1, len(mylist)). Any help is appreciated.
mylist = [8, 2, 4, 9, 3, 6]
for j in range(1,len(mylist)):
key = mylist[j]
i = j
while i > 0 and mylist[i-1] > key:
mylist[i] = mylist[i - 1]
i -= 1
mylist[i] = key
Let me try to break this down.
Start by considering a list. It is “almost” sorted. That is, the first few elements are sorted, but the last element is not sorted. So it looks something like this:
Obviously, the 15 is in the wrong place. So how do we move it?
That’ll switch around the 15 and the 50 so now the list looks like:
But we’d like to do this several times in a loop. To do that we can do:
That loop will go back one position at a time swapping the two elements. That’ll move the out of order position one place back each time. But how do we know when to stop?
Let’s look again at our list and the moves we want to make:
But what is different that last time around? Every time we move the number one place back, it is because the 15 is less then the element on the left, meaning its not sorted. When that is no longer true we should stop moving. But we can easily deal with that:
Ok, but happens if we now try to sort this list:
[10, 20, 1]
[10, 1, 20]
[1, 10, 20]
# ERROR!!
At this point something bad happens. We try to check whether key < mylist[i-1] but when we’ve reached the beginning, i = 0, and this checks the end of the list. But we should stop moving to left at this point…
If we reach the beginning of the list, we can’t move our pivot/key further so we should stop. We update our while loop to handle that:
So now we have a technique for sorting an almost sorted list. But how can we use that to sort a whole list? We sort parts of the list at a time.
First we sort the first 1 elements:
Then we sort the first 2 elements:
Then we sort the first 3 elements
So on and so forth
But how do we do we do that? With a for loop
But we can skip the first time through, because a list of one element is obviously already sorted.
A few minor changes which make no difference brings us back to your original code