[英]why am i getting this List index out of range error in my Quick sort program?
有人可以帮我解决这个代码吗,我知道错误的含义,但无论我尝试多少,我都找不到解决方案。 如果有人能告诉我我在哪里弄乱了代码,那将非常有帮助。
这就是我创建的 Class。
class AlgoTest:
def __init__(self):
self.li = []
len_list = int(input("Enter the number of elements you want in the list -->> "))
for i in range(len_list):
inp = int(input(f"Enter your {i} element -->> "))
self.li.append(inp)
Function 用于快速排序问题:
def quick_sort(self, array, low, high):
if low < high:
loc = self.partiotion(arrp=array, lower=low, higher=high)
self.quick_sort(array, low, loc - 1)
self.quick_sort(array, loc + 1, high)
print("Sorted Array Using QUICK Sort -->> ", array)
这是快速排序算法中使用的分区 function。 这就是错误的来源。
def partiotion(self, arrp, lower, higher):
pivot = arrp[lower]
start = lower
end = higher
while start < end:
while arrp[start] <= pivot:
start = start + 1
while arrp[end] > pivot:
end = end - 1
if start < end:
# arrp[start], arrp[end] = arrp[end], arrp[start]
temp = arrp[start]
arrp[start] = arrp[end]
arrp[end] = temp
# arrp[lower], arrp[end] = arrp[end], arrp[lower]
temp = arrp[lower]
arrp[lower] = arrp[end]
arrp[end] = temp
return end
这些只是我在使用类调用函数时调用的 object。
a = AlgoTest()
lb = 0
ub = len(a.li) - 1
arr = a.li
a.quick_sort(arr, lb, ub)
这些是我得到的错误:
C:\Python\python.exe "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py"
Enter the number of elements you want in the list -->> 5
Enter your 0 element -->> 23
Enter your 1 element -->> 12
Enter your 2 element -->> 0
Enter your 3 element -->> 14
Enter your 4 element -->> 8
Traceback (most recent call last):
File "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py", line 132, in <module>
a.quick_sort(arr, lb, ub)
File "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py", line 88, in quick_sort
loc = self.partiotion(arrp=array, lower=low, higher=high)
File "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py", line 101, in partiotion
while arrp[start] <= pivot:
IndexError: list index out of range
Process finished with exit code 1
有人可以帮我解决这个代码吗,我知道错误的含义,但无论我尝试多少,我都找不到解决方案。 如果有人能告诉我我在哪里弄乱了代码,那将非常有帮助。
这就是我创建的 Class。
class AlgoTest:
def __init__(self):
self.li = []
len_list = int(input("Enter the number of elements you want in the list -->> "))
for i in range(len_list):
inp = int(input(f"Enter your {i} element -->> "))
self.li.append(inp)
Function 用于快速排序问题:
def quick_sort(self, array, low, high):
if low < high:
loc = self.partiotion(arrp=array, lower=low, higher=high)
self.quick_sort(array, low, loc - 1)
self.quick_sort(array, loc + 1, high)
print("Sorted Array Using QUICK Sort -->> ", array)
这是快速排序算法中使用的分区 function。 这就是错误的来源。
def partiotion(self, arrp, lower, higher):
pivot = arrp[lower]
start = lower
end = higher
while start < end:
while arrp[start] <= pivot:
start = start + 1
while arrp[end] > pivot:
end = end - 1
if start < end:
# arrp[start], arrp[end] = arrp[end], arrp[start]
temp = arrp[start]
arrp[start] = arrp[end]
arrp[end] = temp
# arrp[lower], arrp[end] = arrp[end], arrp[lower]
temp = arrp[lower]
arrp[lower] = arrp[end]
arrp[end] = temp
return end
这些只是我在使用类调用函数时调用的 object。
a = AlgoTest()
lb = 0
ub = len(a.li) - 1
arr = a.li
a.quick_sort(arr, lb, ub)
这些是我得到的错误:
C:\Python\python.exe "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py"
Enter the number of elements you want in the list -->> 5
Enter your 0 element -->> 23
Enter your 1 element -->> 12
Enter your 2 element -->> 0
Enter your 3 element -->> 14
Enter your 4 element -->> 8
Traceback (most recent call last):
File "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py", line 132, in <module>
a.quick_sort(arr, lb, ub)
File "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py", line 88, in quick_sort
loc = self.partiotion(arrp=array, lower=low, higher=high)
File "C:/Imp softwares/Pycharm/Pycharm projects/python_GUI_tkinter/Algo_tester_part_2.py", line 101, in partiotion
while arrp[start] <= pivot:
IndexError: list index out of range
Process finished with exit code 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.