Skip to content

Commit

Permalink
Create insertionSort.py
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeHiestIndia authored Oct 10, 2021
1 parent cf235d4 commit 40fcede
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Python/insertionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
def insertionSort(arr):

# Traverse through 1 to len(arr)
for i in range(1, len(arr)):

key = arr[i]

# Move elements of arr[0..i-1], that are
# greater than key, to one position ahead
# of their current position
j = i-1
while j >=0 and key < arr[j] :
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key


# Driver code to test above
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 40fcede

Please sign in to comment.