Beispiel #1
0
 def testHeapSort(self):
     items = [random.randint(-1000,1000) for i in range(100000)]
     print('before sort ', items)
     HeapSort.sort(items)
     print('after sort ',items)
     for i in range(len(items)-1):
         if items[i] > items[i+1]:
             self.fail("Heap sort failed")
Beispiel #2
0
def ascend_sort():
    heap_sort = HeapSort()
    arr = [3, 8, 9, 10, 1, 2, 7, 22, 34, 10, 2]
    print("Before Sort Array: ", arr)
    start = datetime.now()
    heap_sort.big_heap_sort(arr)
    end = datetime.now() - start
    print("After Sort Array: ", arr)
    print("Heap Sort Lost Time: ", end)
Beispiel #3
0
 def test_sort(self):
     # arrange
     arr = [5, 55, 93, 83, 55, 122, 72]
     # act
     heap = HeapSort(list(arr))
     heap.heap_sort()
     # assert
     _arr = arr.copy()
     _arr.sort()
     self.assertEqual(_arr, heap.array)
     pass
Beispiel #4
0
 def kthMinimum(numbers, k):
     heap = numbers[:k]
     itera = numbers[k:]
     if len(itera) == 0:
         heap = HeapSort.sort(heap)
     else:
         for i in range(len(itera)):
             heap = HeapSort.sort(heap)
             if heap[len(heap)-1] > itera[i]:
                 heap[len(heap)-1] = itera[i]
     return heap[len(heap)-1]
 def test_heap_sort(self):
   lst = HeapSort()
   self.assertEqual([1, 2, 3], lst.heap_sort([2, 1, 3]))
Beispiel #6
0
from abstract_sort import AbstractSort
from bubble_sort import BubbleSort
from quick_sort import QuickSort
from heap_sort import HeapSort
from python_sort import PythonSort

from sort_time_complexity import SortTimeComplexity

if __name__ == '__main__':
    stc = SortTimeComplexity()
    stc.run_sorter_tests(BubbleSort())
    stc.run_sorter_tests(QuickSort())
    stc.run_sorter_tests(HeapSort())
    stc.run_sorter_tests(PythonSort())
    stc.display_plot()
Beispiel #7
0
filename = sys.argv[1]

if not os.path.exists(filename):
    sys.exit(f"Error: File '{sys.argv[1]}' not found")


class MyImp:
    def __init__(self, filename):
        self.paths = []
        self.tsp = TSP(filename)
        # self.start = tsp.vertices[0]


myImp = MyImp(filename)

heapSort = HeapSort()

for i in myImp.tsp.edges:
    heapSort.addToTree(i)

# for i in myImp.tsp.vertices:
#     print(i.toString());

kruskel = MSLKruskel(heapSort, myImp.tsp)

edges = kruskel.run()
for v in kruskel.verticesInTree:
    print(v.toString())
# for e in edges:
#     print(e.toString())
Beispiel #8
0
 def test_random_1(self):
     lst = random.sample(range(100), 50)
     lst_heap_sort = HeapSort(lst)
     lst.sort()
     self.assertEqual(lst, lst_heap_sort.sort())
Beispiel #9
0
 def test_random_4(self):
     lst = random.sample(range(50), 20)
     lst_heap_sort = HeapSort(lst)
     lst.sort(reverse=True)
     self.assertEqual(lst, lst_heap_sort.sort(descending=True))
Beispiel #10
0
from quick_sort import QuickSort
from bubble_sort import BubbleSort
from selection_sort import SelectionSort
from insertion_sort import InsertionSort
from time import time

import random

options = {
    1: ("Bubble Sort", BubbleSort()),
    2: ("Selection Sort", SelectionSort()),
    3: ("Insertion Sort", InsertionSort()),
    4: ("Quick Sort", QuickSort()),
    5: ("Merge Sort", MergeSort()),
    6: ("Radix Sort", RadixSort()),
    7: ("Counting Sort", CountingSort()),
    8: ("Heap Sort", HeapSort())
}
print("Sorting Options: ")
for i in options.keys():
    print("Option {} : {}".format(i, options[i][0]))
while True:
    option = int(input("Enter sorting option: "))
    sort_alg = options[option][1]
    start = time()
    arr = [random.randint(-100000, 100000) for _ in range(10)]
    print(arr)
    sort_alg.sort(arr)
    end = time()
    print(arr)
    print("Total Time (seconds): " + str(end - start))
Beispiel #11
0
# this script tests if heap_sort.py is callable externally
import sys, os
if os.getcwd() not in sys.path:
    sys.path.append(os.getcwd())
from heap_sort import HeapSort

ar = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
ar2 = [4, 13, 52, 7, 18, 3, 1, 6]

print (HeapSort.sort(ar))
print (HeapSort.sort(ar2))


Beispiel #12
0
def fn_heap_sort(input, output, start):
    heap_sort = HeapSort()
    heap_sort.heap_sort(input)
    assert input == output
    end = time.time()
Beispiel #13
0
 def test(self, args):
     HeapSort.sort(args)
     assert HeapSort.is_sorted(args)
     HeapSort.show(args)
 def test_heap_sort(self):
     algo = SortingTestWrapper(HeapSort(), self.n, self.seed)
     self.assertListEqual(algo.integer_sort(), self.verification.integer_sort())