Created
July 10, 2023 04:32
-
-
Save krishnaglodha/618a3d96182b0ce2b6dac4367ba56698 to your computer and use it in GitHub Desktop.
Insertion Sort Algorithm in python
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
# 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