コード例 #1
0
    def test_merge_sort(self):
        input = [3, 1, 9, 4, 2, 7, 6, 10, 8, 5]
        output = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

        mergeSort(input, 0, len(input) - 1)

        self.assertListEqual(input, output)
コード例 #2
0
ファイル: main.py プロジェクト: namitadhikari/Algo_Lab
def runMerge(n):
    data = sample(range(n + 1), n)

    start_time = time() * 1000
    mergeSort(data, 0, len(data) - 1)
    end_time = time() * 1000

    time_taken_merge = end_time - start_time
    print(f"Time taken for merge sort of {n} data = {time_taken_merge} ms")

    return time_taken_merge
コード例 #3
0
def measureTime(algoList):
    '''Hilfsfunktion um Laufzeit von MergeSort zu messen'''
    lstTime = []  # Liste mit Laufzeiten abhaengig von der Laenge der Liste

    for _list in algoList:
        s = time.perf_counter()
        merge.mergeSort(_list)
        e = time.perf_counter()
        diff = e - s
        lstTime.append(diff)

    return lstTime
コード例 #4
0
def decoratedMergeSort(data, key=None):
    """
	Demonstrate the decorate-sort-undecorate pattern when using a key parameter
	Applies private _Item class to represent key-value pairs
	"""
    class _Item:
        """ Private representation of key-value pair as items """
        __slots__ = '_key', '_value'  #steamline memory

        def __init__(self, key, value):
            """ create a new item """
            self._key = key
            self._value = value

        def __lt__(self, other):
            """ Comparison test on items """
            return self._key < other._key

    if key is not None:
        for j in range(len(data)):
            data[j] = _Item(key(data[j]), data[j])

    data = mergeSort(data)

    if key is not None:
        for j in range(len(data)):
            data[j] = data[j]._value

    return data
コード例 #5
0
def time(a, sortname):
    import time
    from selection import selection
    from insertion import insertion
    from merge import mergeSort
    from quick import quickSort
    from heap import heapSort
    time_start = time.time()
    if sortname == 'Selection':
        selection(a)
    if sortname == 'Insertion':
        insertion(a)
    if sortname == 'Merge':
        mergeSort(a)
    if sortname == 'Quick':
        quickSort(a)
    if sortname == 'Heap':
        heapSort(a)
    time_end = time.time()
    return (time_end - time_start)
コード例 #6
0
ファイル: pysort.py プロジェクト: DawidZiec/Python-Sorter
        print(" ")
        print("Po sortowaniu kubelkowym malejaco:")
        t.reverse()
        print(t)

#########################################################################
############################  MERGE SORT  ###############################
#########################################################################

if metoda == '4':
    # wypisanie #1
    print(" ")
    print("Przed sortowaniem:")
    print(x)

    merge.mergeSort(x)

    # wypisanie #2
    if kierunek == 'r':
        print(" ")
        print("Po sortowaniu przez scalanie rosnaco:")
        print(x)
    if kierunek == 'm':
        print(" ")
        print("Po sortowaniu przez scalanie malejaco:")
        x.reverse()
        print(x)

#########################################################################
############################  HEAP SORT  ################################
#########################################################################
コード例 #7
0
r.recvline()

print('starting hanoi')

i = r.recvline()
while (not i.startswith(b"level 2")):
    l = len(json.loads(i))
    r.sendline(str(2**l - 1).encode())
    r.recvuntil(' ')
    i = r.recvline()

print('done hanoi')

# l = len(json.loads(r.recvline()))
# r.sendline(str(2**l-1).encode())
# r.recvuntil(' ')
# r.recvline()

from merge import mergeSort
print('starting merge sort')
i = r.recvline()
while (not i.startswith(b"you won")):
    l = json.loads(i)
    r.sendline(str(mergeSort(l, len(l))).encode())
    r.recvuntil(' ')
    i = r.recvline()

print('done merge sort')

print(i.decode()[:-1])
r.close()
コード例 #8
0
from heap import heapSort
from merge import mergeSort
from tim import timSort

arr = [1, 3, 4, 2]
heapSort(arr)
assert arr == [1, 2, 3, 4]

arr = [1, 3, 4, 2]
mergeSort(arr)
assert arr == [1, 2, 3, 4]

arr = [1, 3, 4, 2]
timSort(arr, len(arr))
assert arr == [1, 2, 3, 4]
コード例 #9
0
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 3:
        print("Você escolheu o algoritmo InsertionSort\n")
        array_inser = array.copy()
        inicio = timeit.default_timer()
        result_inser = insertionSort(array_inser)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 4:
        print("Você escolheu o algoritmo MergeSort\n")
        array_merg = array.copy()
        inicio = timeit.default_timer()
        result_merg = mergeSort(array_merg)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 5:
        print("Você escolheu o algoritmo QuickSort\n")
        array_quic = array.copy()
        n = len(array_quic)
        inicio = timeit.default_timer()
        result_quic = quickSort(array_quic, 0, n - 1)
        fim = timeit.default_timer()
        result = fim - inicio
        print("Tempo de execução: {:.8f} \n".format(result))

    elif op == 6:
コード例 #10
0
def test_merge():
    arr = [50, 3, 11, 7]
    merge.mergeSort(arr)
    assert arr == [3, 7, 11, 50]