Skip to content

Commit

Permalink
Merge pull request #957 from smriti2411/insertionsort
Browse files Browse the repository at this point in the history
Recursive Insertion sort
  • Loading branch information
fineanmol authored Oct 16, 2021
2 parents 2c8854f + ba95bf7 commit 68a4ded
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Recursive Insertion sort
def insertionSort(arr,n):
# base case
if n<=1:
return
# Recursive Call
insertionSort(arr,n-1)
key = arr[n-1]
j = n-2
# Compare key with each element on the left of it until an element smaller than it is found
while (j>=0 and arr[j]>key):
arr[j+1] = arr[j]
j = j-1
# Place key at after the element just smaller than it.
arr[j+1]=key
a=[]
n=int(input("Enter number of elements: "))
for i in range(0,n):
b=int(input("Enter element: "))
a.append(b)
insertionSort(a,n)
print('Sorted Array:')
print(a)

0 comments on commit 68a4ded

Please sign in to comment.