Skip to content

Instantly share code, notes, and snippets.

@Tester2009
Created October 23, 2018 17:34
Show Gist options
  • Save Tester2009/e4b90a7b26c2f1e925f268276f6989aa to your computer and use it in GitHub Desktop.
Save Tester2009/e4b90a7b26c2f1e925f268276f6989aa to your computer and use it in GitHub Desktop.
Insertion sorting algorithm. Written in Python
# 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