Skip to content

Instantly share code, notes, and snippets.

@richardbwest
Created November 7, 2020 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardbwest/10781a339c51006658b50638f7e09eb1 to your computer and use it in GitHub Desktop.
Save richardbwest/10781a339c51006658b50638f7e09eb1 to your computer and use it in GitHub Desktop.
import random,time,os
l = [i for i in range(10)]
random.shuffle(l)
def redraw(arrow = "↑",shift=0,moved_right=False):
os.system('cls')
print()
print("Outer Index:",index, "\tCurrentValue: ",currentValue, "\tInner Index:",currentPosition)
print()
print("--------"*index+">")
if not moved_right:
for item in l: print(str(item) + "\t",end = "")
else:
for item in l[0:currentPosition-1]: print(str(item) + "\t",end = "")
print(l[currentPosition],end="")
print("------>",end="")
for item in l[currentPosition:]: print(str(item) + "\t",end = "")
print()
print("\t"*(currentPosition+shift)+arrow)
print("\t"*(currentPosition)+str(currentValue))
time.sleep(2)
def insertion_sort(array):
global index,currentPosition,currentValue,l
for index in range(1, len(array)):
print("Current Index:",index)
currentValue = array[index]
currentPosition = index
redraw(arrow ="↓")
redraw(arrow ="?",shift =-1)
while currentPosition > 0 and array[currentPosition - 1] > currentValue:
array[currentPosition] = array[currentPosition -1]
redraw(arrow ="?",shift =-1,moved_right=True)
currentPosition = currentPosition - 1
redraw(arrow ="?",shift =-1)
array[currentPosition] = currentValue
redraw()
insertion_sort(l)
print("Array Sorted!")
for item in l: print(str(item) + "\t",end = "")
input()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment