Ejemplo n.º 1
0
def startAlgorithm():
    global data
    if not data: return

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

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

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

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

    elif algMenu.get() == 'Selection Sort':
        selection_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Selection Sort':
        selection_sort(data, drawData, speedScale.get())

    elif algMenu.get() == 'Radix Sort':
        radix_sort(data, drawData, speedScale.get())
    drawData(data, ['green' for x in range(len(data))])
Ejemplo n.º 2
0
import selectionSort
import mergeSort
import quickSort
import insertionSort
import bubbleSort
import heapSort
import random
import time

for i in range(1, 3):
    arr = []
    n = 10**i
    for j in range(0, n):
        arr.append(random.randrange(0, 1000, 1))
    selection_sort_time = selectionSort.selection_sort(arr, n)
    print("Selection sort time for size", n, "is ", selection_sort_time)
    arr.clear()
    for j in range(0, n):
        arr.append(random.randrange(0, 1000, 1))
    start_time = time.time()
    arr = mergeSort.merge_sort(arr)
    end_time = time.time()
    print("The merge sorted array with size ", len(arr), "is ")
    for i in range(0, len(arr)):
        print("array[", i, "] = ", arr[i])
    print("merge sort time for size", n, "is ", end_time - start_time)
    arr.clear()
    for j in range(0, n):
        arr.append(random.randrange(0, 1000, 1))
    insertion_sort_time = insertionSort.insertion(arr)
    print("Insertion sort time for size", n, "is ", insertion_sort_time)
    bucket_sort(temp, how_many // 100)
    temp = array[:]
    comb_sort(temp)
    temp = array[:]
    counting_sort(temp)
    temp = array[:]
    heap_sort(temp)
    temp = array[:]
    insertion_sort(temp)
    temp = array[:]
    shell_sort(temp)
    temp = array[:]
    lsd_radix_sort(temp)
    temp = array[:]
    pigeonhole_sort(temp)
    temp = array[:]
    selection_sort(temp)
    temp = array[:]
    (comparisons, array_accesses, additional_space) = merge_sort(temp)
    print("Merge sort:")
    print("No. comparisons: " + str(comparisons) + ", no. array accesses: " +
          str(array_accesses) + ", no. additional space required: " +
          str(additional_space))
    temp = array[:]
    (comparisons, array_accesses) = quick_sort(temp, 0, len(temp) - 1, 0, 0)
    additional_space = 0
    print("Quick sort:")
    print("No. comparisons: " + str(comparisons) + ", no. array accesses: " +
          str(array_accesses) + ", no. additional space required: " +
          str(additional_space))
Ejemplo n.º 4
0
MAX = 20
##
random.seed()
test1 = [random.randint(1, MAX) for i in range(MAX)]
test2 = test1
test3 = test1

# print opening
space(3)
border('D')
print("Input Size:", MAX)
print("Input:", test1)

# SELECTION SORT TEST
start = time.time()
selection_sort(test1)
end = time.time()
border('S')
space(1)
print("Selection Sort Timer: ", end-start, "s", sep='')
print("List Returned:", test1)
space(1)
border('M')

# MERGE SORT TEST
start = time.time()
merge_sort(test2)
end = time.time()
border('M')
space(1)
print("Merge Sort Timer: ", end-start, "s", sep='')
Ejemplo n.º 5
0
from selectionSort import selection_sort
from insertionSort import insertion_sort
from bubbleSort import bubble_sort
from binarySearch import binary_search
import random

random_array = [random.randint(1, 100) for _ in range(100)]

print("Selection sort time:")
selection_sort(random_array)

print(binary_search(random_array, 76))

print("Insertion sort time:")
insertion_sort(random_array)

print("Bubble sort time:")
bubble_sort(random_array)
Ejemplo n.º 6
0
def main():
    from quickSort import q_count

    sys.setrecursionlimit(10000)

    # instantiate write to file
    write_file = WriteToFile()
    merge_sort = MergeSort()

    # 100 list
    first_list, second_list, third_list, fourth_list = arrayGenerator.generate_random_list_number(100)
    print("{}, {}, {}, {}".format(len(first_list), len(second_list), len(third_list), len(fourth_list)))

    # sorting 100 list with bubblesort
    start_time = time.perf_counter()
    first_list, comparisons = bubbleSort.bubble_sort(first_list)
    end_time = time.perf_counter()
    print("Bubble Sort time: %s seconds" % (end_time - start_time))
    print("Bubble Sort comparisions: ", comparisons)

    # Creating the semisorted list
    first_list = arrayGenerator.randomizer(first_list)

    # sorting 100 semisorted list with bubblesort
    start_time_semisorted = time.perf_counter()
    first_list, semisorted_comparisons = bubbleSort.bubble_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Bubble Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Bubble Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Bubble Sort data to the file
    write_file.write_to_file('Bubble Sort', 'O(n^2)', 'O(n)', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 100 list with selectionsort
    start_time = time.perf_counter()
    second_list, comparisons = selectionSort.selection_sort(second_list)
    end_time = time.perf_counter()
    print("Selection Sort time: %s seconds" % (end_time - start_time))
    print("Selection Sort comparisions: ", comparisons)

    # Creating the semisorted list
    second_list = arrayGenerator.randomizer(second_list)

    # sorting 100 semisorted list with selectionsort
    start_time_semisorted = time.perf_counter()
    second_list, semisorted_comparisons = selectionSort.selection_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Selection Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Selection Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Selection Sort data to the file
    write_file.write_to_file('Selection Sort', 'O(n^2)', 'O(n^2)', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
    
    # sorting 100 list with mergesort
    start_time = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time = time.perf_counter()
    print("Merge Sort time: %s seconds" % (end_time - start_time))
    comparisons = merge_sort.count
    print("Merge Sort comparisions: ", merge_sort.count)

    # Creating the semisorted list
    third_list = arrayGenerator.randomizer(third_list)

    # sorting 100 semisorted list with mergesort
    start_time_semisorted = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time_semisorted = time.perf_counter()
    print("Merge Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = merge_sort.count
    print("Merge Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Merge Sort data to the file
    write_file.write_to_file('Merge Sort', 'O(nlog(n))', 'O(nlog(n))', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
   
    # sorting 100 list with quicksort
    start_time = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time = time.perf_counter()
    print("Quick Sort time: %s seconds " % (end_time - start_time))
    comparisons = quickSort.q_count
    print("Quick Sort comparisions: ", quickSort.q_count)


    # Creating the semisorted list
    fourth_list = arrayGenerator.randomizer(fourth_list)

    # sorting 100 semisorted list with quicksort
    start_time_semisorted = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time_semisorted = time.perf_counter()
    print("Quick Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = quickSort.q_count
    print("Quick Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Quick Sort data to the file
    write_file.write_to_file('Quick Sort', 'O(n^2)', 'O(nlog(n))', 100, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)


    # 1000 list
    first_list, second_list, third_list, fourth_list = arrayGenerator.generate_random_list_number(1000)
    print("{}, {}, {}, {}".format(len(first_list), len(second_list), len(third_list), len(fourth_list)))
    
    # sorting 1000 list with bubblesort
    start_time = time.perf_counter()
    first_list, comparisons = bubbleSort.bubble_sort(first_list)
    end_time = time.perf_counter()
    print("Bubble Sort time: %s seconds" % (end_time - start_time))
    print("Bubble Sort comparisions: ", comparisons)

    # Creating the semisorted list
    first_list = arrayGenerator.randomizer(first_list)

    # sorting 1000 semisorted list with bubblesort
    start_time_semisorted = time.perf_counter()
    first_list, semisorted_comparisons = bubbleSort.bubble_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Bubble Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Bubble Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Bubble Sort data to the file
    write_file.write_to_file('Bubble Sort', 'O(n^2)', 'O(n)', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 1000 list with selectionsort
    start_time = time.perf_counter()
    second_list, comparisons = selectionSort.selection_sort(second_list)
    end_time = time.perf_counter()
    print("Selection Sort time: %s seconds" % (end_time - start_time))
    print("Selection Sort comparisions: ", comparisons)

    # Creating the semisorted list
    second_list = arrayGenerator.randomizer(second_list)

    # sorting 1000 semisorted list with selectionsort
    start_time_semisorted = time.perf_counter()
    second_list, semisorted_comparisons = selectionSort.selection_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Selection Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Selection Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Selection Sort data to the file
    write_file.write_to_file('Selection Sort', 'O(n^2)', 'O(n^2)', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
    
    # sorting 1000 list with mergesort
    start_time = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time = time.perf_counter()
    print("Merge Sort time: %s seconds" % (end_time - start_time))
    print("Merge Sort comparisions: ", merge_sort.count)

    # Creating the semisorted list
    third_list = arrayGenerator.randomizer(third_list)

    # sorting 1000 semisorted list with mergesort
    start_time_semisorted = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time_semisorted = time.perf_counter()
    print("Merge Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = merge_sort.count
    print("Merge Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Merge Sort data to the file
    write_file.write_to_file('Merge Sort', 'O(nlog(n))', 'O(nlog(n))', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 1000 list with quicksort
    start_time = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time = time.perf_counter()
    print("Quick Sort time: %s seconds " % (end_time - start_time))
    comparisons = quickSort.q_count
    print("Quick Sort comparisions: ", quickSort.q_count)

    # Creating the semisorted list
    fourth_list = arrayGenerator.randomizer(fourth_list)

    # sorting 1000 semisorted list with quicksort
    start_time_semisorted = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time_semisorted = time.perf_counter()
    print("Quick Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = quickSort.q_count
    print("Quick Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Quick Sort data to the file
    write_file.write_to_file('Quick Sort', 'O(n^2)', 'O(nlog(n))', 1000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # 10000 list
    first_list, second_list, third_list, fourth_list = arrayGenerator.generate_random_list_number(10000)
    print("{}, {}, {}, {}".format(len(first_list), len(second_list), len(third_list), len(fourth_list)))

    # sorting 10000 list with bubblesort
    start_time = time.perf_counter()
    first_list, comparisons = bubbleSort.bubble_sort(first_list)
    end_time = time.perf_counter()
    print("Bubble Sort time: %s seconds" % (end_time - start_time))
    print("Bubble Sort comparisions: ", comparisons)

    # Creating the semisorted list
    first_list = arrayGenerator.randomizer(first_list)

    # sorting 10000 semisorted list with bubblesort
    start_time_semisorted = time.perf_counter()
    first_list, semisorted_comparisons = bubbleSort.bubble_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Bubble Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Bubble Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Bubble Sort data to the file
    write_file.write_to_file('Bubble Sort', 'O(n^2)', 'O(n)', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 10000 list with selectionsort
    start_time = time.perf_counter()
    second_list, comparisons = selectionSort.selection_sort(second_list)
    end_time = time.perf_counter()
    print("Selection Sort time: %s seconds" % (end_time - start_time))
    print("Selection Sort comparisions: ", comparisons)

    # Creating the semisorted list
    second_list = arrayGenerator.randomizer(second_list)

    # sorting 10000 semisorted list with selectionsort
    start_time_semisorted = time.perf_counter()
    second_list, semisorted_comparisons = selectionSort.selection_sort(first_list)
    end_time_semisorted = time.perf_counter()
    print("Selection Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    print("Selection Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Selection Sort data to the file
    write_file.write_to_file('Selection Sort', 'O(n^2)', 'O(n^2)', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)
    
    # sorting 10000 list with mergesort
    start_time = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time = time.perf_counter()
    print("Merge Sort time: %s seconds" % (end_time - start_time))
    print("Merge Sort comparisions: ", merge_sort.count)

    # Creating the semisorted list
    third_list = arrayGenerator.randomizer(third_list)

    # sorting 10000 semisorted list with mergesort
    start_time_semisorted = time.perf_counter()
    third_list = merge_sort.merge_sort(third_list)
    end_time_semisorted = time.perf_counter()
    print("Merge Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = merge_sort.count
    print("Merge Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Merge Sort data to the file
    write_file.write_to_file('Merge Sort', 'O(nlog(n))', 'O(nlog(n))', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    # sorting 10000 list with quicksort
    start_time = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time = time.perf_counter()
    print("Quick Sort time: %s seconds " % (end_time - start_time))
    comparisons = quickSort.q_count
    print("Quick Sort comparisions: ", quickSort.q_count)

    # Creating the semisorted list
    fourth_list = arrayGenerator.randomizer(fourth_list)

    # sorting 10000 semisorted list with quicksort
    start_time_semisorted = time.perf_counter()
    quickSort.quick_sort(fourth_list)
    end_time_semisorted = time.perf_counter()
    print("Quick Sort time on semisorted array: %s seconds" % (end_time_semisorted - start_time_semisorted))
    semisorted_comparisons = quickSort.q_count
    print("Quick Sort comparisions on semisorted array: ", semisorted_comparisons)

    # Writing the Quick Sort data to the file
    write_file.write_to_file('Quick Sort', 'O(n^2)', 'O(nlog(n))', 10000, end_time - start_time, comparisons, end_time_semisorted - start_time_semisorted, semisorted_comparisons)

    write_file.close()