Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Last active December 21, 2016 12:21
Show Gist options
  • Save vividvilla/5108171 to your computer and use it in GitHub Desktop.
Save vividvilla/5108171 to your computer and use it in GitHub Desktop.
Insertion Sort in Python - Hacker rank Insertionsort2 solution
def insertionSort(ar):
for q in range(1,len(ar)):
for i in range(q):
if(ar[q] < ar[i]):
temp=ar[q]
ar[q]=ar[i]
ar[i]=temp
for x in ar:
print x,
print
m = input()
ar = [int(i) for i in raw_input().strip().split()]
insertionSort(ar)
@mesonoxian
Copy link

it fails in hacker news

@brandonlee503
Copy link

It works 👍

@shivdotpy
Copy link

For Python 3
def insertionSort(ar):
for q in range(1,len(ar)):
for i in range(q):
if(ar[q] < ar[i]):
temp=ar[q]
ar[q]=ar[i]
ar[i]=temp
print(' '.join(str(x) for x in ar))

m = input()
ar = [int(i) for i in input().strip().split()]
insertionSort(ar)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment