I have started to learn python about two months ago and I am trying to practice new skills. I am trying to practice by building existing algorithms and this was the start of my take on Quicksort:
def quicksort(list):
left=[]
right=[]
#marker positions
pivot_position=-1
left_marker_position=0
right_marker_position=-2
#markers
pivot=list[pivot_position]
left_marker=list[left_marker_position]
right_marker=list[right_marker_position]
while True:
if left_marker>=pivot:
break
elif left_marker<pivot:
left_marker_position+=1
print(left_marker)
while True:
if right_marker>pivot:
right_marker_position-=1
elif right_marker<=pivot:
break
list[left_marker_position],list[right_marker_position]=list[right_marker_position],list[left_marker_position]
print(list)
a=[1,4,5,7,9,6,3,2,5] quicksort(a)
I know I should make it recursive and this isn't the best approach, however my problem is that I don't understand why the while loop infinitely prints 1. Why does left_marker not change to index two?
Aucun commentaire:
Enregistrer un commentaire