'Calling a function in a function inside a sorting class
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)
Solution 1:[1]
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]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Walker |
