Friday 2 May 2014

Insertion Sort , In Python

Insertion Sort is one of easiest & efficient way to sort small group of numbers . Using Python this algorithm is very very simple to implement .
But in other languages like C it is not so easy ..

Read actual algorithm & its implementation in C .
Insertion sort Algorithm & implementation in C

Python 2.7.6 Implementation of Insertion sort Algorithm

Algorithm is well explain in program comment


  1. #   Python 2.7
  2. #   Insertion Sort
  3. #   http://beginer2cs.blogspot.com
  4. def Insertion_Sort():
  5.     global ls #declare ls as global var
  6.     l=len(ls)  
  7.     for i in range(1,l):   #for loop start from 1 to end of list
  8.         for j in range(i):    # for loop b/w 0 to I'th element
  9.             if(ls[j]>=ls[i]):   # and check for appropriate postion
  10.                                    # If appropriate position found
  11.                 temp = ls.pop(i) # pop number & save it to var temp
  12.                 ls.insert(j,temp) #insert temp at appropriate postion
  13.        
  14. ######### MAIN ###########
  15. print "insert number seperated by space to sort using insertion sort algorithm"
  16. ls = [int(x) for x in raw_input().split(' ')] # taking input
  17.                      # input is Dynamic so list should be of any length
  18.                      
  19. print "List U inserted to sort Using Insertion Sort"
  20. print ls
  21. Insertion_Sort() #sort list
  22. print 'Sorted list'
  23. print ls



Insertion sort algorithm in Python 2.7


Another way to sort using insertion sort.
this algorithm is implemented in python 2.7 . you can see it does't use special power of  Python list operation as in previous example So you can directly implement
this algorithm in C,C++

  1. #            Python 2.7
  2. #       Insertion Sort  Algorithm     (way 2)
  3. #       http://beginer2cs.blogspot.com
  4.  
  5. def insertion_sort2():
  6.     global ls
  7.     l=len(ls)
  8.     j=0
  9.     for i in range(1,l):
  10.         key = ls[i]
  11.         j=i-1
  12.           # Check for appropriate position to insert
  13.           # from i-1'th pos to -'th pos (checking for postion is right to left)
  14.         while j>=0 and key < ls[j] :
  15.             ls[j+1]=ls[j] #shift J'th element to j+1'th position
  16.             j=j-1         #decrease counter by 1
  17.  
  18. # after this loop completion value of j is appropriate position for key
  19.             ls[j+1]=key
  20.  
  21.  
  22.  
  23. # ******** Main ***********
  24. print 'insert number to be sort,numbers must be seperated by space'
  25. ls = [int(x) for x in raw_input().split(' ') ] # array of number as input
  26.  
  27. print 'list before sorting'
  28. print ls
  29.  
  30. insertion_sort2() #calling function to sort
  31.  
  32. print 'sorted list'
  33. print ls

3 comments:

  1. I'm not sure where you're getting your information, but good
    topic. I must spend some time learning more or figuring out more.

    Thanks for wonderful info I used to be in search of this information for my mission.

    my webpage: dui penalties arizona

    ReplyDelete
  2. That is really attention-grabbing, You are an excessively professional blogger.
    I've joined your feed and look ahead to seeking more of
    your great post. Also, I've shared your site in my social networks

    Here is my page grow taller 4 idiots

    ReplyDelete
  3. Outstanding story there. What happened after? Thanks!


    Here is my blog :: grow taller 4 idiots

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets