Skip to content

Commit

Permalink
Merge pull request #392 from mthakur7/patch-2
Browse files Browse the repository at this point in the history
Create insertionSort.py
  • Loading branch information
fineanmol committed Oct 2, 2021
2 parents bb723ce + 11c2450 commit 1b61570
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
def insertionSort(arr):

# traversing through the length of whole array
for i in range(1, len(arr)):
key = arr[i]
j = i-1
while j >=0 and key < arr[j] :
#comparing current element with its previous one, and if previous one is greater
#move it one place ahead of current
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key


# Driver test case
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print ("Sorted array is:")
for i in range(len(arr)):
print ("%d" %arr[i])

0 comments on commit 1b61570

Please sign in to comment.