Last active
December 21, 2016 12:21
-
-
Save vividvilla/5108171 to your computer and use it in GitHub Desktop.
Insertion Sort in Python - Hacker rank Insertionsort2 solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | |
m = input() | |
ar = [int(i) for i in raw_input().strip().split()] | |
insertionSort(ar) |
It works 👍
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
it fails in hacker news