Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created recursiveinsertionsort.py #188

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Searching & sorting/Sorting/recursiveinsertionsort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Recursive Python program for insertion sort
# Recursive function to sort an array using insertion sort

def insertionSortRecursive(arr,n):
# base case
if n<=1:
return

# Sort first n-1 elements
insertionSortRecursive(arr,n-1)
'''Insert last element at its correct position
in sorted array.'''
last = arr[n-1]
j = n-2

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

arr[j+1]=last

# A utility function to print an array of size n
def printArray(arr,n):
for i in range(n):
print(arr[i],end=" ")

# Driver program to test insertion sort
arr = [12,11,13,5,6]
n = len(arr)
insertionSortRecursive(arr, n)
printArray(arr, n)