insertion sort implementation in python

I am working on my Python coding skills and this time reviewing various algorithms with which I have not been in touch for many days. So I start with sorting, here is my code for insertion sort:
def insertion_sort(ara):
    for j in range(1, len(ara)):
        key = ara[j]
        i = j - 1
        while i >= 0 and key < ara[i]:
            ara[i+1] = ara[i]
            i -= 1
        ara[i+1] = key

    return ara

if __name__ == "__main__":
    ara = [10, 5, 2, 9, 4, 8, 9, 2, 1]
    print insertion_sort(ara)
More python implementation of various algorithms are coming soon. Meanwhile if you feel that there is chance to improve my code, or make it more pythonic, please feel free to comment.

Oh, if you are not familiar with algorithms yet and wording what the ... is insertion sort, please visit the wikipedia article on insertion sort.

Comments

Popular posts from this blog

Strip HTML tags using Python

lambda magic to find prime numbers

Convert text to ASCII and ASCII to text - Python code