Algorithm No. 1 INSERTION SORT

Hello everyone !
I had found a nice book in my TI classroom which is called “Introduction to Algorithms” Thomas. H Cormen. I had some time during lesson so I decided to rewrite some algorithms in python. Here you got one of these, it is called “insertion sort”. Insertion sort is a simple sorting algorithm, a comparison sort in which the sorted array (or list) is built one entry at a time.

A = [6,2,3,1,5,8,10,9,22,7]

for j in range(1,len(A)):
    key = A[j]
    i = j -1
    while i >= 0 and A[i] > key:
        A[i+1] = A[i]
        i = i-1
    A[i+1] = key

One thought on “Algorithm No. 1 INSERTION SORT

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s