Sorting algorithm my implementation
Today I tried to implement sort in Python and faced a few problems and overcame them
The first problem was in the code below
inputlist = [54, 26, 93, 17, 17, 77, 31, 44, 55, 20]
def Sort(unsortedList):
if len(unsortedList) > 1:
left = [] # for smaller numbers (not deatl woth duplicat elements yet)
right = [] # for bigger numbers
pivot = len(unsortedList)//2 # write another using first element
for i in unsortedList:
if i < unsortedList[pivot]:
left.append(i)
elif i == unsortedList[pivot]:
continue
else:
right.append(i)
l = Sort(left)
part = l.append(unsortedList[pivot]) # this line is responsible
l.append(unsortedList[pivot])
r = Sort(right)
return part + r
else:
return unsortedList
print(Sort(inputlist))
When I ran this code, It showed an error as shown below
Traceback (most recent call last):
File "_____.py", line 24, in <module>
print(Sort(k))
^^^^^^^^^^^^
File "_____.py", line 16, in Sort
l = Sort(left)
^^^^^^^^^^^^^^^
File "_____.py", line 16, in Sort
l = Sort(left)
^^^^^^^^^^^^^^^
File "_____.py", line 19, in Sort
r = Sort(right)
^^^^^^^^^^^^^^^^
File "_____.py", line 20, in Sort
return part + r
~~~~~^~~
TypeError: unsupported operand type(s) for +: 'NoneType' and 'list'
By seeing this error I was sure that part
or r
one of those was not assigned properly, At first I thought r
should be the one which is responsible for this error and on the line r = Sort(right)
Sort(right)
wasn't returning anything was my conclusion
Then I tried to print out things and found that part
is the one responsible and the responsible line was part = l.append(unsortedList[pivot])
Because the append method is updating the list and won't return anything simply getting rid of the part variable was the solution and the corrected code is shown below
inputlist = [54, 26, 93, 17, 17, 77, 31, 44, 55, 20]
def Sort(unsortedList):
if len(unsortedList) > 1:
left = [] # for smaller numbers (not deatl woth duplicat elements yet)
right = [] # for bigger numbers
pivot = len(unsortedList)//2 # write another using first element
for i in unsortedList:
if i < unsortedList[pivot]:
left.append(i)
elif i == unsortedList[pivot]:
continue
else:
right.append(i)
l = Sort(left)
l.append(unsortedList[pivot])
r = Sort(right)
return l + r
else:
return unsortedList
print(Sort(inputlist))
The story did not end here, As You can see in the first version of the code I simply neglected duplicate elements in the list. even if the list has duplicate elements they will be gone only one will remain in the output, so I re-write the code as below
# this version does deal with duplicate elements
k = [54, 26, 93, 17, 17, 77, 31, 44, 55, 20]
def Sort(unsortedList):
if len(unsortedList) > 1:
left = [] # smaller numbers than pivot
right = [] # bigger numbers than pivot
equals = [] # equal numbers to pivot (to track how many duplicates are there)
pivot = len(unsortedList)//2
for i in unsortedList:
if i < unsortedList[pivot]:
left.append(i)
elif i == unsortedList[pivot]:
equals.append(i)
else:
right.append(i)
l = Sort(left)
for equal in equals:
l.append(equal)
r = Sort(right)
return l + r
else:
return unsortedList
print(Sort(k))
Still story is not finished yet, you can see the pivot element is near to middle (not exactly in the middle), but I can change it to the first, second or last element by changing the line below as follows
pivot = len(unsortedList)//2
to pivot = 0
, pivot = 1
or pivot = -1