それをよりよく理解するために、独自のhoareパーティション関数を作成しようとしています。私はその定義と擬似コードにうまく従ったと思っていましたが、多くの場合期待どおりに動作しているように見えますが、pivot に等しい複数の値を持つリストが渡されるとバラバラになり、無限ループに陥ります。私の間違いはどこですか?バグを修正するには、これをどのように変更すればよいですか?
def partition(lst, left_index, right_index):
pivot = lst[right_index]
while True:
#Increment left index until value at that index is greater or equal to pivot
while True:
if lst[left_index] >= pivot: break
left_index += 1
#Increment right index until value at that index is less or equal to pivot
while True:
if lst[right_index] <= pivot: break
right_index -= 1
if left_index < right_index:
lst[left_index], lst[right_index] = lst[right_index], lst[left_index]
else:
return right_index
return partition(0, end)