Kürzlich habe ich versucht, den QuickSort-Algorithmus in Python zu implementieren, und ich konnte es nicht richtig funktionieren. Obwohl das Programm die Sub-Arrays sortiert, wird es nicht in der Hauptliste angezeigt. Ich bin neu im Programmieren, also kann mir jemand helfen zu verstehen, welchen Teil oder welches Konzept ich nicht richtig gemacht habe?Fehler in meinem schnellen Sortieralgorithmus Implementierung
def swap(arr, right, left):
temp = arr[right]
arr[right] = arr[left]
arr[left] = temp
def split_array(arr, right):
left_half = arr[:right]
right_half = arr[right:]
a = (left_half, right_half)
return a
def quick_sort(arr):
if len(arr) >= 2:
pivot = 0
left_mark = pivot + 1
right_mark = len(arr) - 1
stop = True
while stop:
while arr[pivot] > arr[left_mark] and left_mark < right_mark:
left_mark += 1
while arr[pivot] < arr[right_mark] and left_mark < right_mark:
right_mark -= 1
if left_mark < right_mark:
swap(arr, right_mark, left_mark)
right_mark -= 1
left_mark += 1
else:
if arr[pivot] > arr[right_mark]:
swap(arr, right_mark, pivot)
stop = False
left, right = split_array(arr, right_mark)
quick_sort(left)
quick_sort(right)
return arr
array = [8, 6, 1, 7, 0, 5, 4, 3, 2, 1]
print(quick_sort(array))
Das ist die richtige Idee, aber sehen Sie meine Antwort, warum das immer noch nicht funktioniert. ("Links" und "Rechts" werden nie wirklich geändert.) – smarx