在排序类内部的函数中调用函数

发布于 2025-01-09 21:56:00 字数 2912 浏览 1 评论 0原文

在合并排序函数中,我很难在函数内部正确调用合并排序函数。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

堇色安年 2025-01-16 21:56:00

您的代码和逻辑内部存在一些错误,请在下面给您正确的示例

class Sort:

    def __init__(self):
        self.pool = []

    def mergeSort(self, pool=None):
        pool = pool if pool else self.pool
        if len(pool) > 1:
            mid = len(pool) // 2
            lefthalf = pool[:mid]
            righthalf = pool[mid:]
            self.mergeSort(lefthalf)
            self.mergeSort(righthalf)
            print(f"{'Splitting':10s}{pool}")
            i = j = k = 0
            while i < len(lefthalf) and j < len(righthalf):
                if lefthalf[i] < righthalf[j]:
                    pool[k] = lefthalf[i]
                    i += 1
                else:
                    pool[k] = righthalf[j]
                    j += 1
                k += 1
            while i < len(lefthalf):
                pool[k] = lefthalf[i]
                i += 1
                k += 1
            while j < len(righthalf):
                pool[k] = righthalf[j]
                j += 1
                k += 1
            print(f"{'Merging':10s}{pool}")

if __name__ == '__main__':
    sort = Sort()
    sort.pool = [39, 3, 50, 10, 58, 95, 13, 27, 14, 49]
    print(f"{'Pool':10s}{sort.pool}")
    sort.mergeSort()

结果

Pool      [39, 3, 50, 10, 58, 95, 13, 27, 14, 49]
Splitting [39, 3]
Merging   [3, 39]
Splitting [10, 58]
Merging   [10, 58]
Splitting [50, 10, 58]
Merging   [10, 50, 58]
Splitting [39, 3, 50, 10, 58]
Merging   [3, 10, 39, 50, 58]
Splitting [95, 13]
Merging   [13, 95]
Splitting [14, 49]
Merging   [14, 49]
Splitting [27, 14, 49]
Merging   [14, 27, 49]
Splitting [95, 13, 27, 14, 49]
Merging   [13, 14, 27, 49, 95]
Splitting [39, 3, 50, 10, 58, 95, 13, 27, 14, 49]
Merging   [3, 10, 13, 14, 27, 39, 49, 50, 58, 95]

Your code and logic had some error inside, give you the correct example below

class Sort:

    def __init__(self):
        self.pool = []

    def mergeSort(self, pool=None):
        pool = pool if pool else self.pool
        if len(pool) > 1:
            mid = len(pool) // 2
            lefthalf = pool[:mid]
            righthalf = pool[mid:]
            self.mergeSort(lefthalf)
            self.mergeSort(righthalf)
            print(f"{'Splitting':10s}{pool}")
            i = j = k = 0
            while i < len(lefthalf) and j < len(righthalf):
                if lefthalf[i] < righthalf[j]:
                    pool[k] = lefthalf[i]
                    i += 1
                else:
                    pool[k] = righthalf[j]
                    j += 1
                k += 1
            while i < len(lefthalf):
                pool[k] = lefthalf[i]
                i += 1
                k += 1
            while j < len(righthalf):
                pool[k] = righthalf[j]
                j += 1
                k += 1
            print(f"{'Merging':10s}{pool}")

if __name__ == '__main__':
    sort = Sort()
    sort.pool = [39, 3, 50, 10, 58, 95, 13, 27, 14, 49]
    print(f"{'Pool':10s}{sort.pool}")
    sort.mergeSort()

Result

Pool      [39, 3, 50, 10, 58, 95, 13, 27, 14, 49]
Splitting [39, 3]
Merging   [3, 39]
Splitting [10, 58]
Merging   [10, 58]
Splitting [50, 10, 58]
Merging   [10, 50, 58]
Splitting [39, 3, 50, 10, 58]
Merging   [3, 10, 39, 50, 58]
Splitting [95, 13]
Merging   [13, 95]
Splitting [14, 49]
Merging   [14, 49]
Splitting [27, 14, 49]
Merging   [14, 27, 49]
Splitting [95, 13, 27, 14, 49]
Merging   [13, 14, 27, 49, 95]
Splitting [39, 3, 50, 10, 58, 95, 13, 27, 14, 49]
Merging   [3, 10, 13, 14, 27, 39, 49, 50, 58, 95]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文