Exemple #1
0
def StartAlgorithm():
    global data
    speed = int(speedScale.get())
    if speed == 2:
        speed = 0.5
    elif speed == 3:
        speed = 0.1
    elif speed == 4:
        speed = 0.05
    elif speed == 5:
        speed = 0.01
    elif speed == 6:
        speed = 0.005
    elif speed == 7:
        speed = 0.001

    if selected_algo.get() == "Bubble Sort":
        bubble_sort(data, drawData, speed)
    elif selected_algo.get() == "Selection Sort":
        selection_sort(data, drawData, speed)
    elif selected_algo.get() == "Insertion Sort":
        insertion_sort(data, drawData, speed)
    elif selected_algo.get() == "Merge Sort":
        merge_sort(data, 0, len(data) - 1, drawData, speed)
        drawData(data, ['light green' for x in range(len(data))])
        time.sleep(speed)
    elif selected_algo.get() == "Quick Sort":
        quick_sort(data, drawData, speed)
Exemple #2
0
def StartAlgorithm():
    global data
    speed = int(speedScale.get())
    if speed == 2:
        speed = 1.5
    elif speed == 3:
        speed = 0.1
    elif speed == 4:
        speed = 0.05
    elif speed == 5:
        speed = 0.01
    elif speed == 6:
        speed = 0.005
    elif speed == 7:
        speed = 0.001

    search = int(searchEntry.get())
    if selected_algo.get() == "Bubble Sort":
        bubble_sort(data, drawData, speed)
    elif selected_algo.get() == "Selection Sort":
        selection_sort(data, drawData, speed)
    elif selected_algo.get() == "Insertion Sort":
        insertion_sort(data, drawData, speed)
    elif selected_algo.get() == "Merge Sort":
        merge_sort(data, 0, len(data) - 1, drawData, speed)
        drawData(data, ['light green' for x in range(len(data))])
        time.sleep(speed)
    elif selected_algo.get() == "Quick Sort":
        quick_sort(data, drawData, speed)
    elif algMenu.get() == 'Linear Search':
        linear_search(data, search, drawData, speedScale.get())
    elif algMenu.get() == 'Binary Search':
        merge_sort(data, 0, len(data) - 1, drawData, speed)
        drawData(data, ['red' for x in range(len(data))])
        binary_search(data, search, drawData, speedScale.get())
Exemple #3
0
def quick_vs_selec_vs_insr():
    lista = []

    for i in range(40000):
        aleatorio = random.randint(0, 40000)
        lista.append(aleatorio)
    
    tempo = time.time()
    quick_sort(lista, 0, len(lista) - 1)
    fim = time.time()
    print("[QUICKSORT] 40000 (40 mil) inteiros Aleatorios\n[TEMPO] "+ str(fim - tempo))
    
    lista = []

    for i in range(40000):
        aleatorio = random.randint(0, 40000)
        lista.append(aleatorio)
    
    tempo = time.time()
    selection_sort(lista, len(lista))
    fim = time.time()
    print("[SELECTIONSORT] 40000 (40 mil) inteiros Aleatorios\n[TEMPO] "+ str(fim - tempo))
    
    lista = []

    for i in range(40000):
        aleatorio = random.randint(0, 40000)
        lista.append(aleatorio)

    tempo = time.time()
    insertion_sort(lista, len(lista))
    fim = time.time()
    print("[INSERTIONSORT] 40000 (40 mil) inteiros Aleatorios\n[TEMPO] "+ str(fim - tempo))
    print("\n")
Exemple #4
0
def Dselect(input_array, l, r, k):
    n = r - l + 1
    stat_val = -1
    if n > 1:
        n1 = int(n / 5)
        means = []
        for i in range(0, n1):
            sorted_subarr = ins_sort.insertion_sort(input_array[l + 5 * i:l +
                                                                5 * i + 5])
            means.append(sorted_subarr[2])
        if n % 5 > 0:
            sorted_subarr = ins_sort.insertion_sort(input_array[l + 5 * n1:r +
                                                                1])
            means.append(sorted_subarr[int(len(sorted_subarr) / 2) - 1])
        piv_val = Dselect(means, 0, len(means) - 1, int(len(means) / 2) - 1)
        piv = 0
        for val in input_array:
            if val == piv_val:
                break
            piv += 1
        temp_val = input_array[piv]
        input_array[piv] = input_array[l]
        input_array[l] = temp_val

        pivot_idx = partition(input_array, l, r)
        if pivot_idx == k:
            stat_val = input_array[pivot_idx]
        elif pivot_idx > k:
            stat_val = Dselect(input_array, l, pivot_idx - 1, k)
        else:
            stat_val = Dselect(input_array, pivot_idx + 1, r, k)
    else:
        stat_val = input_array[l]
    return stat_val
def calc_duration_insert_sort():
    t1 = duration.start_time()
    insertionsort.insertion_sort(B)
    t2 = duration.stop_time()
    # print("By insertion sort, sorted array is: ", end="\n")
    # print(B)
    print("Insertion sorting time(sec): ", duration.calc_duration(t1, t2))
    print(end="\n")
Exemple #6
0
def quick_insert_sort(A, p, r):
    if p<r:
        q = parition(A, p, r)
        if q>k():
            quick_sort(A, p, q-1)
            quick_sort(A, q+1, r)
        else:
            insertion_sort(A)
    return A
Exemple #7
0
def test_insertion_sort():
    insort = insertion_sort([2, 4, 6, 8, 3])

    eq_(next(insort), [2, 4, 6, 8, 8])
    eq_(next(insort), [2, 4, 6, 6, 8])
    eq_(next(insort), [2, 4, 4, 6, 8])
    eq_(next(insort), [2, 3, 4, 6, 8])
Exemple #8
0
def test_insertion_sort():
    insort = insertion_sort([1, 4, 3, 5, 6, 2])

    eq_(next(insort), [1, 4, 3, 5, 6, 2])
    eq_(next(insort), [1, 3, 4, 5, 6, 2])
    eq_(next(insort), [1, 3, 4, 5, 6, 2])
    eq_(next(insort), [1, 3, 4, 5, 6, 2])
    eq_(next(insort), [1, 2, 3, 4, 5, 6])
def StartAlgorithm():
    global data
    if not data: return

    if algMenu.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, drawData, speedScale.get())
        drawData(data, ['green' for x in range(len(data))])

    elif algMenu.get() == 'Bubble Sort':
        bubble_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Merge Sort':
        merge_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'insertion Sort':
        insertion_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'selection Sort':
        selection_sort(data, drawData, speedScale.get())
Exemple #10
0
def start_algo():
    global data
    if not data: return

    if algoMenu.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, draw_Data, speedScale.get())

    elif algoMenu.get() == 'Bubble sort':
        bubble_sort(data, draw_Data, speedScale.get())

    elif algoMenu.get() == 'Merge Sort':
        merge_sort(data, draw_Data, speedScale.get())

    elif algoMenu.get() == 'Insertion Sort':
        insertion_sort(data, draw_Data, speedScale.get())

    elif algoMenu.get() == 'Heap Sort':
        heap_sort(data, draw_Data, speedScale.get())

    draw_Data(data, ['green' for x in range(len(data))])
Exemple #11
0
def quick_vs_selec_vs_insr():
    data = []

    for i in range(40000):
        random_num = random.randint(0, 40000)
        data.append(random_num)

    start = time.time()
    quick_sort(data, 0, len(data) - 1)
    end = time.time()
    print("[QUICKSORT] 40000 (40K) integers random numbers \n [start] " +
          str(end - start))
    print("\n")

    data = []

    for i in range(40000):
        random_num = random.randint(0, 40000)
        data.append(random_num)

    start = time.time()
    selection_sort(data, len(data))
    end = time.time()
    print("[SELECTIONSORT] 40000 (40 mil) integers random_nums\n[start] " +
          str(end - start))
    print("\n")

    data = []

    for i in range(40000):
        random_num = random.randint(0, 40000)
        data.append(random_num)

    start = time.time()
    insertion_sort(data, len(data))
    end = time.time()
    print("[INSERTIONSORT] 40000 (40 mil) integers random numbers \n[start] " +
          str(end - start))
    print("\n")
Exemple #12
0
def quick_sort(array, desc=False):
    array_size = len(array)
    # print(array_size)
    if 16 > array_size > 1:
        return insertion_sort(array, desc)
    array = array.copy()
    if array_size <= 1:
        return array
    _quick_sort(array, 0, array_size - 1)
    if not desc:
        return array
    else:
        return array[::-1]
Exemple #13
0
def main():
    # a = [5,3,17,10,84,19,6,22,9]
    # a = [13,11,9,12,12,4,9,4,21,2,6,11]
    print("Quick-Insertion sort :", end=' ')
    a = rand(100000)
    # print_list(a)
    start = time.clock()
    a = quick_insert_sort(a, 0, len(a)-1)
    end = time.clock()
    # print_list(a)
    print(end - start)
    print("Quick sort :", end=' ')
    a = rand(100000)
    start = time.clock()
    a = quick_sort(a, 0, len(a)-1)
    end = time.clock()
    print(end - start)
    print("Insertion sort :", end=' ')
    a = rand(100000)
    start = time.clock()
    insertion_sort(a)
    end = time.clock()
    print(end - start)
Exemple #14
0
def merge_sort(nums: list, i: int, j: int) -> list:
    '''
    Sorts sublist nums[i:j] and returns it sorted. It does not sort
    in place.  It is stable.  
    
    To call it from the outside, you should do:
    merge_sort(nums, 0, len(nums))
    '''
    if j <= i + 2:
        return insertion_sort(nums[i:j])
    else:
        range_ = j - i
        boundary = range_ // 2
        a = merge_sort(nums, i, i + boundary)
        b = merge_sort(nums, i + boundary, j)
        return merge(a, b)
Exemple #15
0
def merge_sort(array, approach='top_down', desc=False):
    # Effective time reduction optimization: for small arrays invoke insertionsort
    if len(array) <= 43:
        return insertion_sort(array, desc)

    array = array.copy()
    if desc:
        comparison_operator = greater_than_equal_to_operator
    else:
        comparison_operator = less_than_equal_to_operator
    if approach == 'top_down':
        array_len = len(array)
        top_down_split_merge_sort(array, 0, array_len, comparison_operator)
    elif approach == 'bottom_up':
        bottom_up_collect_merge_sort(array, comparison_operator)
    else:
        raise NotImplementedError('{} type of merge sort not implemented.'.format(approach))
    return array
Exemple #16
0
sorted(lst)

print(time.time() - x)
x = time.time()

heap_sort(lst)

print(time.time() - x)
x = time.time()

merge_sort_recursion(lst)

print(time.time() - x)
x = time.time()

insertion_sort(lst)

print(time.time() - x)
x = time.time()

selection_sort(lst)

print(time.time() - x)
x = time.time()

bubble_sort(lst)

print(time.time() - x)
x = time.time()
    file_nm = input("Enter csv filename with data: ")
    if file_nm == "":
        file_nm = "sortedfil.csv"

    with open(file_nm, "r") as f:
        file_dat = f.readlines()

    for sublist in file_dat:
        for nums in sublist.split(","):
            inp_arr.append(int(nums))

start_time = time.time()
if sel == 1:
    sorted_array = ss.selection_sort(inp_arr)
elif sel == 2:
    sorted_array = ins.insertion_sort(inp_arr)
elif sel == 3:
    sorted_array = bs.bubble_sort(inp_arr)
elif sel == 4:
    sorted_array, invcount = ms.merge_sort(inp_arr, 0)
elif sel == 5:
    qs.quick_sort(inp_arr, 0, len(inp_arr) - 1)

elif sel == 6:

    is_sort = input("Is the input already sorted(Y/N): ")
    if not is_sort.upper().strip() == "Y":
        sorted_arr, x = ms.merge_sort(inp_arr, 0)
    start_time - time.time()
    num = int(input("Enter number to search:"))
    idx, found = bins.binary_search(inp_arr, int(num), 0)
Exemple #18
0
 def test_insertion_sort(self):
     values = [5, 2, 4, 1, 3]
     insertion_sort(values)
     required = [1, 2, 3, 4, 5]
     self.assertListEqual(values, required)
import random
from bubblesort import bubble_sort
from insertionsort import insertion_sort

storage = [random.randint(-30,100) for number in range(10)]
print("Before Sort: " , storage)

storage_sorted_b = bubble_sort(storage)
print("After Sort by Bubble Sort: ", storage_sorted_b )

storage_sorted_i = insertion_sort(storage)
print("After Sort by Insertion Sort: ", storage_sorted_i )
Exemple #20
0
def start_insertion(event, canvas, sort, i_count):
    global arr, count
    count = 0
    i_count["text"] = f"{count}"
    insertion_sort(arr, draw_data, canvas, sort, count, i_count)