Skip to content

Commit

Permalink
Merge pull request #6972 from nagaharika-nagella/patch-1
Browse files Browse the repository at this point in the history
Create Insertion.py
  • Loading branch information
ossamamehmood authored Oct 31, 2023
2 parents e8e5025 + 95fb38a commit 2200b0c
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions Insertion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
def insertionSort(arr):
n = len(arr) # Get the length of the array

if n <= 1:
return # If the array has 0 or 1 element, it is already sorted, so return

for i in range(1, n): # Iterate over the array starting from the second element
key = arr[i] # Store the current element as the key to be inserted in the right position
j = i-1
while j >= 0 and key < arr[j]: # Move elements greater than key one position ahead
arr[j+1] = arr[j] # Shift elements to the right
j -= 1
arr[j+1] = key # Insert the key in the correct position

# Sorting the array [12, 11, 13, 5, 6] using insertionSort
arr = [12, 11, 13, 5, 6]
insertionSort(arr)
print(arr)

0 comments on commit 2200b0c

Please sign in to comment.