호 전세 분할 함수를 잘 이해하기 위해 함수를 작성하려고합니다. 나는 그것의 정의와 의사 코드를 잘 따라 갔다고 생각했지만 많은 경우에 예상대로 작동하는 것처럼 보이지만 피벗과 동일한 값을 가진 목록이 전달되면 무너져 내리고 무한 루프가됩니다. 실수는 어디에서 발생합니까? 버그를 수정하려면 어떻게 수정해야합니까?둘 이상의 값이 피벗과 같을 때 호아 분할이 작동하지 않습니다.
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)
흠 ... D Uchhh, 당신이 내 코드를 더 자세히 보았습니까? algo 구현의 기본에서 실수를했을 수도 있습니다 ... 감사합니다 ^^ – MadRabbit