Reputation: 205
Here's a Python implementation of insertion sort, I tried to follow the values on paper but once the counting variable i gets bigger than len(s) I don't know what to do, how/why does it still run?
def sort_numbers(s):
for i in range(1, len(s)):
val = s[i]
j = i - 1
while (j >= 0) and (s[j] > val):
s[j+1] = s[j]
j = j - 1
s[j+1] = val
def main():
x = eval(input("Enter numbers to be sorted: "))
x = list(x)
sort_numbers(x)
print(x)
Upvotes: 16
Views: 58136
Reputation: 20311
To see how that implementation works, check it out in visual form.
However, here is a less confusing implementation of insertion sort:
def insertion_sort(seq):
for i in range(1, len(seq)):
j = i
while j > 0 and seq[j - 1] > seq[j]:
seq[j - 1], seq[j] = seq[j], seq[j - 1]
j -= 1
Upvotes: 3
Reputation: 2087
Or, this one:
def ins_sort(k):
for i in range(1,len(k)): #since we want to swap an item with previous one, we start from 1
j = i #create i's copy (or not)
temp = k[j] #temp will be used for comparison with previous items, and sent to the place it belongs
while j > 0 and temp < k[j-1]: #j>0 bcoz no point going till k[0] since there is no seat available on its left, for temp
k[j] = k[j-1] #Move the bigger item 1 step right to make room for temp
j=j-1 #take k[j] all the way left to the place where it has a smaller/no value to its left.
k[j] = temp
return k
Upvotes: 25
Reputation: 1061
Try this one, works for both increasing and decreasing order:
import operator
def insertion_sort(arr, reverse=False):
# check if array is empty or not
if arr is None:
raise TypeError("Data cannot be none")
n = len(arr)
# if length of array is less than two it is already sorted
if n < 2:
return arr
lgt = operator.gt if reverse else operator.lt
# Traverse through 1 to len(arr)
for i in range(1, n):
key = arr[i]
j = i-1
while j >= 0 and lgt(key, arr[j]):
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
return arr
li = [1, 4, 8, 9, 42, 0, 36]
print(insertion_sort(li))
Upvotes: -1
Reputation: 3091
There are a few pieces of information that help to understand insertion sort.
First of all, i
never gets bigger than len(s)
. In fact, it never is equal to it, either. What range(1, len(s))
does is produces an immutable sequence over which you can iterate. What you will have underneath the following statement (let's assume len(s) = 3
):
for i in range(1, len(s)):
...
is something like this:
for i in (1, 2):
...
You can verify that yourself:
>>> list(range(1, 3))
>>> [1, 2]
So you will iterate two times, first time with i = 1
, and the second time with i = 2
.
Second, it helps to remind yourself that a list you are sorting consists, in essence, of two parts: the part that is sorted (list[0:i-1]
), and the part that is yet to be sorted ([list[i:]
). What you are trying to achieve with insertion sort on every iteration is finding a place to put a current value inside a sorted list. We'll get to that shortly.
Third, the algorithm can be thought of as doing two quite distinct tasks. First one is to iterate over all members in a list and ensure that the list is sorted. That's the part that outer loop concerns itself with (of course, inner loop is involved in actual checks, but it helps to make that simplification). The other one is to find appropriate positions for elements in a list so that a list would be sorted. I.e., if you have a list letters = ['a', 'b', 'd', 'c']
, 'c'
should obviously go to letters[2]
and 'd'
needs to take 'c'
's former place if the list is to be sorted in ascending order. That is the part that inner while
loop does.
How the while loop (and s[j+1] = val
statement) ensures that list is sorted is quite clever, actually. First, it makes sure that we're not overreaching (j >= 0
condition). That is, that we're not selecting s[-1]
element, which in Python would be the last element in the list, and other languages, like Java, ArrayIndexOutOfBounds
exception. Then, it asks: is the number that I'm holding lower than the one before it? If so, it means the list is not sorted and we need to move the bigger number one place towards the end of a list (to the right if you will). Note, the element with which we are comparing other elements remains the same. We are holding it in val
variable. So we need not concern ourselves with accidentally losing it by overwriting it when moving values.
Now, once we move the bigger value to the right, we decrement j
and ask the same question again: is the value at s[j]
bigger than the one that we have in val
? We continue to do so until we find the value that's lower than what we have in val
, or we reach s[0]
without finding lower value. That means that what we hold is the lowest number in a list. Either way, we break out of the while
loop and overwrite s[j+1]
with a value that we have, so that we do not lose value in val
.
To see how it looks in practice, consider a list: [2, 3, 4, 5, 1]
. Let's say we iterate until we reach number 1
, at which point we enter while loop, since 5 > 1
. The steps taken would be:
2, 3, 4, 5, 1 # val = 1, s[j] = 5, j = 3 5 > 1
2, 3, 4, 5, 5 # val = 1, s[j] = 4, j = 2 4 > 1
2, 3, 4, 4, 5 # val = 1, s[j] = 5, j = 1 3 > 1
2, 3, 3, 4, 5 # val = 1, s[j] = 5, j = 0 2 > 1
2, 2, 3, 4, 5 # val = 1, s[j] = 5, j = -1 break out of while
1, 2, 3, 4, 5 # val = 1, s[j] = 5, j = -1 put val into s[0]
And that's basically it. Iterate over the loop, check that values before the current one are lower than the one we're holding, and if not, move those values to the right to make space for our value. And there you have it - insertion sort.
Edit: there is a very nice, visual explanation on code review if you're still finding it hard to see how it works.
Upvotes: 2
Reputation: 1116
a recursive implementation
def insert(x, L):
if [] == L: return [x]
elif x <= L[0]: return [x] + L
else: return [L[0]] + insert(x,L[1:])
def insertion_sort(L):
if [] == L: return []
else: return insert(L[0], insertion_sort(L[1:]))
# test
import random
L = [random.randint(1,50) for _ in range(10)]
print L
print insertion_sort(L)
Upvotes: 1
Reputation: 1
__author__ = 'Dharmjit'
def InsertionSort(list):
for index in range(1,len(list)):
curr = list[index]
position = index
while position > 0 and list[position-1] > curr:
list[position] = list[position-1]
position = position - 1
list[position] = curr
return list
l = [2,1,5,3,9,6,7]
print(InsertionSort(l))
[1,2,3,5,6,7,9]
You can see the whole concept here- http://pythonplanet.blogspot.in/2015/07/sorting-algorithm-1-insertion-sort.html
Upvotes: -2
Reputation: 15
def insertionsort(list):
for i in range(1,len(list)):
temp=list[i]
j=i-1
while temp<+list[j] and j>=0:
list[j+1]=list[j]
j=j-1
list[j+1]=temp
return list
list=eval(raw_input('Enter a list:'))
print insertionsort(list)
This will help you.
Upvotes: -1
Reputation: 69
The python range(start, end)
function starts counting from start
to end - 1
. That is, end
will never be part of the range()
values. So if you have, for example, range(len(A))
, and A
is an array (in Python, a list) of 10 integers, len(A)
will be 10, and range(len(A))
will return (0,1,2,3,4,5,6,7,8,9)
so you can index every element in A.
In your case, i never gets bigger than len(s) - 1
.
Following your code on paper can be useful, but you have to make sure that the programming language does exactly what you think it does, and sometimes the implementation isn't intuitive. A fast and simple way of tracing your program values is to use print
statements. For example:
def sort_numbers(s):
for i in range(1, len(s)):
# let's see what values i takes on
print "i = ", i
val = s[i]
j = i - 1
while (j >= 0) and (s[j] > val):
s[j+1] = s[j]
j = j - 1
s[j+1] = val
Upvotes: 0
Reputation: 3535
If we consider an array from left to right [LeftMost, ..., RightMost], an insertion sort performs the following procedure for each item:
This is the key of the sort algorithm. Once you are done processing at item i, you have a sorted array from where it originally was all the way to the beggining (left most).
Sidenote (not important to understand the algorithm, but could be useful): With that in mind, you can deduce that this algorithm's complexity (measured in worst case comparisons) is O(N^2) where N = len(s). It is similar to having two nested for statements.
This video does a great job explaining the above, and you know what they say, an image is worth 1000 words.
Upvotes: 3
Reputation: 17629
Consider [3, 2, 1]
The loop starts with 3. Since it is the first item in the list there is nothing else to do.
[3, 2, 1]
The next item is 2. It compares 2 to 3 and since 2 is less than 3 it swaps them, first putting 3 in the second position and then placing 2 in the first position.
[2, 3, 1]
The last item is 1. Since 1 is less than 3 it moves 3 over.
[2, 3, 3]
Since 1 is less than 2 it swaps moves 2 over.
[2, 2, 3]
Then it inserts 1 at the beginning.
[1, 2, 3]
Upvotes: 11