我尝试使用以下代码在python中进行快速排序
def quicksort(a, left, right):
if left < right:
i = left;
j = right + 1;
pivot = a[left];
while True:
while True:
i=i+1
if a[i] > pivot or i >= right:
break
while True:
j=j-1
if a[j] < pivot:
break
if i < j:
a[i], a[j] = a[j],a[i];
if i>j:
break
a[left], a[j] = a[j],a[left]
quicksort(a, left, j - 1);
quicksort(a, j + 1, right);
我收到错误
[4, 5, 8, 2, 1, 10, 99, 3]
Traceback (most recent call last):
File "/home/suraj/workspace/DSPS/quicksort/quicksort_py.py", line 36, in <module>
quicksort(a, 0, 7);
File "/home/suraj/workspace/DSPS/quicksort/quicksort_py.py", line 28, in quicksort
quicksort(a, j + 1, right);
File "/home/suraj/workspace/DSPS/quicksort/quicksort_py.py", line 28, in quicksort
quicksort(a, j + 1, right);
File "/home/suraj/workspace/DSPS/quicksort/quicksort_py.py", line 12, in quicksort
if a[i] > pivot or i >= right:
IndexError: list index out of range
我知道实现快速排序的其他方法,但我想在此代码中进行更正。 相同的逻辑适用于c ++ 请帮帮我,我是python的新手