Code:
import math
import time
import random
class SortClass(object):
def sort1(self, l):
if len(l)==1:
return l
elif len(l)==2:
if l[0]<l[1]:
return l
else:
return l[::-1]
else:
pivot=math.floor(len(l)/2)
a=l[pivot:]
b=l[:pivot]
a2=self.sort1(a)
b2=self.sort1(b)
if a2==None or b2==None:
a2=[]
b2=[]
return (a2+b2).sort()
return []
Sort=SortClass()
x=[20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1]
print(Sort.sort1(x))
The code outputs None even though it should return an empty list in two cases:
return []
and
a2=self.mergeSort(a)
b2=self.mergeSort(b)
if a2==None or b2==None:
a2=[]
b2=[]
return (a2+b2).sort()
Details:
The code is for a list sorting module I am making for python practice (I am relatively new at python). sort1
is a modified mergesort.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…