-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #957 from smriti2411/insertionsort
Recursive Insertion sort
- Loading branch information
Showing
1 changed file
with
23 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Program's_Contributed_By_Contributors/Python_Programs/Insertion_Sort.py
This file contains 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
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) |