Fixed sorting_algorithms

This commit is contained in:
madmaurice 2015-11-13 02:42:25 +01:00
parent c54dfca75d
commit a5ee847442

View file

@ -97,7 +97,7 @@ class HeapSort(SortAlgorithm):
def build_heap(self, data): def build_heap(self, data):
w = len(data) w = len(data)
i = w/2-1 i = int(w/2-1)
while i>=0: while i>=0:
self.heapify(data, i, w) self.heapify(data, i, w)
i-=1 i-=1
@ -118,7 +118,7 @@ class MergeSort(SortAlgorithm):
if len(a) <= 1: if len(a) <= 1:
return a return a
else: else:
n = len(a)/2 n = int(len(a)/2);
return self.merge(self.mergesort(a[:n] or []), self.mergesort(a[n:] or [])) return self.merge(self.mergesort(a[:n] or []), self.mergesort(a[n:] or []))
def merge(self,a,b): def merge(self,a,b):
@ -205,8 +205,8 @@ def main():
except Exception: except Exception:
size= 1000 size= 1000
result = SortTester(list( SortAlgorithm.__subclasses__() )).test(size=size) result = SortTester(list( SortAlgorithm.__subclasses__() )).test(size=size)
for cls, r in result.items(): for cls, r in sorted(result.items(),key=lambda e: e[1][0]+e[1][1]):
print("%-12s: %10d, %d compares, %d swaps" % (cls.__name__, r[0]+r[1], r[0],r[1])) print("%-12s: %10d, %10d compares, %10d swaps" % (cls.__name__, r[0]+r[1], r[0],r[1]))
def test(): def test():
print(HeapSort().sort([randint(0,500) for i in range(0,100)])) print(HeapSort().sort([randint(0,500) for i in range(0,100)]))