insertion sort

def insertion_sort(lst): result=[lst[0]] i=1 while i<len(lst): j=0 while j<len(result): if (j==0 and lst[i]<result[j]): result =[lst[i]]+result break elif (j==len(result)-1 and lst[i]>result[j]): result=result+[lst[i]] break elif (result[j]<=lst[i]<=result[j+1]): result = result[:j+1]+[lst[i]]+result[j+1:] break j+=1 i+=1 return result while True : n=input("enter the list :") print insertion_sort(n)

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.