在排序类内部的函数中调用函数
在合并排序函数中,我很难在函数内部正确调用合并排序函数。 mergeSort(lefthalf) 和 mergeSort(righthalf) 是错误的,我不明白为什么。它作为一个独立的函数工作得很好,但当我将它实现到一个类中时就不行了。通话中是否缺少某些内容或者是否应该移动?任何帮助将不胜感激。
class sort:
def __init__(self):
self.Pool=[]
def generateList(self):
import random
self.Pool = []
amount = 10000
startRange = 0
endRange = 10000
for i in range(amount):
temp = random.randint(startRange, endRange + 1)
self.Pool.append(temp)
print("Pool =", self.Pool)
print()
def randomFromList(self):
import random
r = random.randint(0, len(self.Pool) - 1)
Target = self.Pool[r]
print("- Random number from Pool (Target): ")
print("Target = " + str(Target))
print()
def bubbleSort(self):
for passNumber in range(len(self.Pool) - 1, 0, -1):
for i in range(passNumber):
if self.Pool[i] > self.Pool[i + 1]:
temp = self.Pool[i]
self.Pool[i] = self.Pool[i + 1]
self.Pool[i + 1] = temp
def selectionSort(self):
for fillslot in range(len(self.Pool) - 1, 0, -1):
positionOfMax = 0
for location in range(1, fillslot + 1):
if self.Pool[location] > self.Pool[positionOfMax]:
positionOfMax = location
temp = self.Pool[fillslot]
self.Pool[fillslot] = self.Pool[positionOfMax]
self.Pool[positionOfMax] = temp
def insertionSort(self):
for index in range(1, len(self.Pool)):
currentvalue = self.Pool[index]
position = index
while position > 0 and self.Pool[position - 1] > currentvalue:
self.Pool[position] = self.Pool[position - 1]
position = position - 1
self.Pool[position] = currentvalue
def mergeSort(self):
print("Splitting ", self.Pool)
if len(self.Pool) > 1:
mid = len(self.Pool) // 2
lefthalf = self.Pool[:mid]
righthalf = self.Pool[mid:]
mergeSort(lefthalf) #where the error is
mergeSort(righthalf) #where the error is
i = j = k = 0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
self.Pool[k] = lefthalf[i]
i = i + 1
else:
self.Pool[k] = righthalf[j]
j = j + 1
k = k + 1
while i < len(lefthalf):
self.Pool[k] = lefthalf[i]
i = i + 1
k = k + 1
while j < len(righthalf):
self.Pool[k] = righthalf[j]
j = j + 1
k = k + 1
print("Merging ", self.Pool)
In the merge sort function, I am having a hard time correctly calling the merge sort function inside the function. mergeSort(lefthalf) and mergeSort(righthalf) are wrong and I do not understand why. It works fine as a standalone function, but not when I implement it into a class. Is there something missing from the call or should it be moved? Any help would be appreciatted.
class sort:
def __init__(self):
self.Pool=[]
def generateList(self):
import random
self.Pool = []
amount = 10000
startRange = 0
endRange = 10000
for i in range(amount):
temp = random.randint(startRange, endRange + 1)
self.Pool.append(temp)
print("Pool =", self.Pool)
print()
def randomFromList(self):
import random
r = random.randint(0, len(self.Pool) - 1)
Target = self.Pool[r]
print("- Random number from Pool (Target): ")
print("Target = " + str(Target))
print()
def bubbleSort(self):
for passNumber in range(len(self.Pool) - 1, 0, -1):
for i in range(passNumber):
if self.Pool[i] > self.Pool[i + 1]:
temp = self.Pool[i]
self.Pool[i] = self.Pool[i + 1]
self.Pool[i + 1] = temp
def selectionSort(self):
for fillslot in range(len(self.Pool) - 1, 0, -1):
positionOfMax = 0
for location in range(1, fillslot + 1):
if self.Pool[location] > self.Pool[positionOfMax]:
positionOfMax = location
temp = self.Pool[fillslot]
self.Pool[fillslot] = self.Pool[positionOfMax]
self.Pool[positionOfMax] = temp
def insertionSort(self):
for index in range(1, len(self.Pool)):
currentvalue = self.Pool[index]
position = index
while position > 0 and self.Pool[position - 1] > currentvalue:
self.Pool[position] = self.Pool[position - 1]
position = position - 1
self.Pool[position] = currentvalue
def mergeSort(self):
print("Splitting ", self.Pool)
if len(self.Pool) > 1:
mid = len(self.Pool) // 2
lefthalf = self.Pool[:mid]
righthalf = self.Pool[mid:]
mergeSort(lefthalf) #where the error is
mergeSort(righthalf) #where the error is
i = j = k = 0
while i < len(lefthalf) and j < len(righthalf):
if lefthalf[i] < righthalf[j]:
self.Pool[k] = lefthalf[i]
i = i + 1
else:
self.Pool[k] = righthalf[j]
j = j + 1
k = k + 1
while i < len(lefthalf):
self.Pool[k] = lefthalf[i]
i = i + 1
k = k + 1
while j < len(righthalf):
self.Pool[k] = righthalf[j]
j = j + 1
k = k + 1
print("Merging ", self.Pool)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码和逻辑内部存在一些错误,请在下面给您正确的示例
结果
Your code and logic had some error inside, give you the correct example below
Result