16

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)
6
  • 1
    You seem to have the exact same code as en.wikipedia.org/wiki/Insertion_sort, is it not working for you or are you curious why it works? Commented Oct 6, 2012 at 0:23
  • 2
    It's working for me but I don't understand how it works, because when I follow the values on paper it doesn't get the numbers in order in less than len(s) tries. Commented Oct 6, 2012 at 0:39
  • 2
    Does this animation help en.wikipedia.org/wiki/File:Insertion-sort-example-300px.gif Commented Oct 6, 2012 at 0:43
  • It helps in that I didn't know how it worked until I watched that GIF, but I still can't follow that Python code, because if you're sorting 3 numbers it seems like the for loop would have to do it in 3 iterations, and from what I can tell it takes more than 3 iterations. Commented Oct 6, 2012 at 0:49
  • 3
    x = eval(input(...)) ARGH!!! Don't use eval! It is evil! Really: forget it! It shouldn't be taught to beginners. If you want to parse 1, 2, 3, 4 as a tuple and then convert it into a list use ast.literal_eval: import ast #at the beginning of the file [...] x = list(ast.literal_eval(input("Enter numbers to be sorted:"))). Commented Jul 9, 2013 at 10:08

10 Answers 10

25

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
Sign up to request clarification or add additional context in comments.

1 Comment

+1. Also we can actually decrement i directly in the loop (range() returns the correct values no matter what we do to i in the loop), so using j is clearer but not strictly necessary.
11

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]

Comments

3

If we consider an array from left to right [LeftMost, ..., RightMost], an insertion sort performs the following procedure for each item:

  1. Get the current value i.
  2. Get the value j (where j = i-1 in the first iteration, or basically the first element to the left of i). In the first iteration of the while array[i] and array[j] are to consecutive elements. For example, if array = [... 60, 100, 50, ...], and array[i] is 50, then array[j] is 100.
  3. If the previous value is greater than the current one, then it swaps the two values. Basically if you had something like [..., 60, 100, 50, ...] before this operation takes place, you will end up with [..., 60, 50, 100, ...]. The idea is that you move each item i left as long as there are elements to the left of it that are lower.

    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).

  4. Decrease the value of j by one. and go back to step 1 (In this example, this will lead you to compare 50 and 60 and swap them).
If you take a look at the code carefully, you never get a the point where i >= len(s) (range is a function that creates a list, and the value i is not changed inside the loop). You can think of the variable i as an imaginary arrow, pointing to a position in the array that has all sorted elements to its left. The variable j simply moves to the left with i fixed, to swap the original array[i] value until it finds another value that is equal or lower than it.

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.

Comments

3

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

Comments

2

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.

Comments

1

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)

Comments

0

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

Comments

-1
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.

1 Comment

This is in no way an answer to the question.
-1

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))

Comments

-2
__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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.