Created
October 23, 2018 17:34
-
-
Save Tester2009/e4b90a7b26c2f1e925f268276f6989aa to your computer and use it in GitHub Desktop.
Insertion sorting algorithm. Written 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
# explanation: https://www.youtube.com/watch?v=i-SKeOcBwko | |
# written by github.com/Tester2009 | |
array_1 = [5, 7, 9, 3, 2, 1] | |
for iterate in range( 1, len(array_1) ): # iteration start with 1, because to compare with previous element in array | |
value = array_1[iterate] # assign array_1[value_of_iteration] to value variable | |
element = iterate # assign value_of_iteration to element variable | |
while ( element>0 ) and ( array_1[element-1]>value ): # check if element is more than 0. and check value of previous element in array to compare with current element value | |
array_1[element] = array_1[element-1] # assign value of previous element in array to current array element | |
element = element-1 # assign element to previous element | |
array_1[element] = value # assign current value to previous element | |
print(array_1) # print all sorted integer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment