Skip to content

Instantly share code, notes, and snippets.

@krishnaglodha
Created July 10, 2023 04:32
Show Gist options
  • Save krishnaglodha/618a3d96182b0ce2b6dac4367ba56698 to your computer and use it in GitHub Desktop.
Save krishnaglodha/618a3d96182b0ce2b6dac4367ba56698 to your computer and use it in GitHub Desktop.
Insertion Sort Algorithm in python
# input value
array = [5,2,4,6,1,3,7]
# start loop from 1st index ( considering the 0th index value is already sorted
for index in range(1,len(array)):
j = index
# loop to compare current value with it's left neighbor value
while array[j-1] > array[j] and j > 0 :
# if left value is bigger, swap both values
array[j-1],array[j] = array[j], array[j-1]
# reduce j by 1 to now deal with lefter value
j -= 1
# Print the result
print(array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment