Esempio n. 1
0
def partition(unsorted, start, end):
    """
    Will partition a list given a certain range for values greater and
    less than the pivot to be a certain side
    Expected complexity: O(n) (time) and O(1) (space)

    :param unsorted: an unsorted Python list to be partitioned
    :param start: integer of starting index within the list
    :param end: integer of ending index within the list

    :return: index of where hte pivot value ends in the list
    """
    pivot_value = find_pivot(unsorted, start, end)

    i = start - 1

    for j in range(start, end + 1):
        if unsorted[j] < pivot_value:
            i += 1
            _swap(unsorted, i, j)

    i += 1
    _swap(unsorted, i, unsorted.index(pivot_value))

    return i
Esempio n. 2
0
def bubble_sort(data_list, draw_data, time_value):
    """
    Does a bubble sort on a list and visualize the sort
    Expected Complexity (Sort Only): O(n^2) (time) and O(1) (space)

    :param data_list: Python list to be sorted
    :param draw_data: Function written in main.py that visualizes the list
    :param time_value: Float based on the input for time between each step
    """
    for i in range(len(data_list) - 1):

        for j in range(0, len(data_list) - i - 1):

            # bubble up the largets value to the top of the list
            if data_list[j] > data_list[j + 1]:
                _swap(data_list, j, j + 1)

                # generate a color list for the visualization function
                color_list = ["red" for x in range(len(data_list))]

                # color the two elements being swapped green
                for x in range(len(color_list)):
                    if (x == j) or (x == j + 1):
                        color_list[x] = "green"

                # visualize the step and wait for the time specified
                draw_data(data_list, color_list)
                time.sleep(time_value)

    # finally color all of the elements in the list green after the sort
    draw_data(data_list, ["green" for i in range(len(data_list))])
Esempio n. 3
0
def insertion_sort(data_list, draw_data, time_value):
    """
    Does an insertion list and visualizes the steps
    Expected Complexity (Sort only): O(n^2) (time) and O(1) (space)

    :param data_list: Python list to be sorted
    :param draw_data: Function written in main.py that visualizes the list
    :param time_value: Float based on the input for time between each step
    """
    for i in range(1, len(data_list)):
        j = i

        # takes one of the remaining values and inserts it back in the list
        while (j > 0) and (data_list[j] < data_list[j - 1]):
            _swap(data_list, j, j - 1)

            # generate the color list to be visualized
            color_list = ["red" for x in range(len(data_list))]

            # color the values being swapped green
            for x in range(len(color_list)):
                if (x == j) or (x == j - 1):
                    color_list[x] = "green"

            # visualize the step and wait for the specified amount of time
            draw_data(data_list, color_list)
            time.sleep(time_value)

            j -= 1

    # color the whole list green after the sort
    draw_data(data_list, ["green" for i in range(len(data_list))])
Esempio n. 4
0
def partition(data_list, start, end, draw_data, time_value):
    """
    Partitions the sub list and visualizes the steps
    Expected Complexity: O(n) (time) and O(1) (space)

    :param data_list: Python list to be partitioned
    :param start: Integer for the starting index in the list
    :param end: Integer for the ending index in the list
    :param draw_data: Function written in main.py that visualizes the list
    :param time_value: Float based on the input for time between steps

    :return: Integer for the index of the pivot value after the partition
    """
    pivot_value = find_pivot(data_list, start, end)

    i = start - 1

    for j in range(start, end + 1):

        # moves each value that is less than the pivot to the left
        if data_list[j] < pivot_value:
            i += 1
            _swap(data_list, i, j)

            # generate the color list to be visualized
            color_list = ["red" for x in range(len(data_list))]

            # color the values being swapped green
            for x in range(len(color_list)):
                if (x == i) or (x == j):
                    color_list[x] = "green"

            # visualizes the list and wait for the specified amount of time
            draw_data(data_list, color_list)
            time.sleep(time_value)

    i += 1

    # does one last swap to move the pivot value in the right spot
    swap_index = data_list.index(pivot_value)
    _swap(data_list, i, swap_index)

    # generate the color list to be visualized
    color_list = ["red" for x in range(len(data_list))]

    # color the values being swapped green
    for x in range(len(color_list)):
        if (x == i) or (x == swap_index):
            color_list[x] = "green"

    # visualizes the list and wait for the specified amount of time
    draw_data(data_list, color_list)
    time.sleep(time_value)

    return i
def bubble_sort(unsorted):
    """
    Does a bubble sort given a Python list
    Expected Complexity: O(n^2) (time) and O(1) (space)

    :param unsorted: unsorted Python list to be sorted
    """
    for i in range(len(unsorted)):
        for j in range(len(unsorted) - 1, i, -1):
            if unsorted[j] < unsorted[j - 1]:
                _swap(unsorted, j, j - 1)
Esempio n. 6
0
def bubble_sort(unsorted):
    """
    Does a bubble sort given a Python list
    Expected Complexity: O(n^2) (time) and O(1) (space)

    :param unsorted: unsorted Python list to be sorted
    """
    for i in range(len(unsorted) - 1):
        for j in range(0, len(unsorted) - i - 1):

            # bubbles up the largest value through the list
            if unsorted[j] > unsorted[j + 1]:
                _swap(unsorted, j, j + 1)
Esempio n. 7
0
def selection_sort(unsorted):
    """
    Does an selection sort on a Python list
    Expected Complexity: O(n^2) (time) and O(1) (space)

    :param unsorted: unsorted Python list to be sorted
    """
    for i in range(len(unsorted)):

        min_index = i
        for j in range(i + 1, len(unsorted)):
            if unsorted[min_index] > unsorted[j]:
                min_index = j

        _swap(unsorted, i, min_index)
def insertion_sort(unsorted, start, end):
    """
    Does an insertion sort on a Python list given a range of indicies
    Expected Complexity: O(n^2) (time) and O(1) (space)

    :param unsorted: unsorted Python list to be sorted
    :param start: integer of the starting index to be sorted
    :param end: integer of the ending index to be sorted
    """

    for i in range(1, end + 1):
        j = i

        while (j > 0) and (unsorted[j] < unsorted[j - 1]):
            _swap(unsorted, j, j - 1)
            j -= 1
Esempio n. 9
0
def selection_sort(data_list, draw_data, time_value):
    """
    Does a selection sort on a list and visualizes it
    Expected Complexity (Sort only): O(n^2) (time) and O(1) (space)

    :param data_list: Python list to be sorted
    :param draw_data: Function written in main.py to visualize the list
    :param time_value: Float based on the input for the time between steps
    """
    for i in range(len(data_list)):
        min_index = i

        # locates the min in the remaining elements and move it to the bottom
        for j in range(i + 1, len(data_list)):
            if data_list[min_index] > data_list[j]:
                min_index = j

                # generate the color list to be visualized
                color_list = ["red" for x in range(len(data_list))]

                # color the min index blue
                for x in range(len(data_list)):
                    if x == min_index:
                        color_list[x] = "blue"

                # visualize the list and wait for the specified amount of time
                draw_data(data_list, color_list)
                time.sleep(time_value)

        _swap(data_list, i, min_index)

        # generate the color list to be visualized
        color_list = ["red" for x in range(len(data_list))]

        # color the values being swapped green
        for x in range(len(color_list)):
            if (x == i) or (x == min_index):
                color_list[x] = "green"

        # visualize the list and wait for the specified amount of time
        draw_data(data_list, color_list)
        time.sleep(time_value)

    # color the whole list green after the sort
    draw_data(data_list, ["green" for i in range(len(data_list))])
Esempio n. 10
0
def heapify(data_list, size, root_index, draw_data, time_value):
    """
    Heapifies the list and visualizes the steps
    Expected Complexity (heapify only): O(log(n)) (time) and O(1) (space)

    :param data_list: Python list to be heapified
    :param size: Integer of the size of the list to be heapified
    :param root_index: Integer of the index in the list of the root
    :param draw_data: Function written in main.py that visualizes the list
    :param time_value: Float based on the input for time between each step
    """

    # declare and locate the largest index and the children of the root
    largest_index = root_index
    left_index = (2 * root_index) + 1
    right_index = (2 * root_index) + 2

    # change the largest if the root is smaller than the left child
    if (left_index < size) and (data_list[root_index] < data_list[left_index]):
        largest_index = left_index

    # change the largest if the largest is smaller than the right child
    if (right_index < size) and (data_list[largest_index] <
                                 data_list[right_index]):
        largest_index = right_index

    # only changes if either the left or right child is larger than the root
    if largest_index != root_index:
        _swap(data_list, root_index, largest_index)

        # generate the color list to be visualized
        color_list = ["red" for x in range(len(data_list))]

        # color the two elements being swapped as blue
        for x in range(len(color_list)):
            if (x == root_index) or (x == largest_index):
                color_list[x] = "blue"

        # visualize the step and wait for the specified amount of time
        draw_data(data_list, color_list)
        time.sleep(time_value)

        # recurse again so that it is a complete heap
        heapify(data_list, size, largest_index, draw_data, time_value)
Esempio n. 11
0
def heap_sort(data_list, draw_data, time_value):
    """
    Does a heap sort on a list and visualizes the steps
    Expected Complexity (Sort only): O(n*log(n)) (time) and O(1) (space)

    :param data_list: Python list to be sorted
    :param draw_data: Function written in main.py that visualizes the list
    :param time_value: Float based on the input for time between each step
    """

    # heapifies the list
    for i in range((len(data_list) // 2) - 1, -1, -1):
        heapify(data_list, len(data_list), i, draw_data, time_value)

    # draw the heapified list as blue before starting the popping from the heap
    draw_data(data_list, ["blue" for i in range(len(data_list))])
    time.sleep(time_value)

    for i in range(len(data_list) - 1, 0, -1):
        _swap(data_list, i, 0)

        # generate the color list to be visualized
        color_list = ["red" for x in range(len(data_list))]

        # color the two elements being swapped green
        for x in range(len(color_list)):
            if (x == i) or (x == 0):
                color_list[x] = "green"

        # visualize the swap and wait the specified amount of time
        draw_data(data_list, color_list)
        time.sleep(time_value)

        # heapify the remaining portion of the list
        heapify(data_list, i, 0, draw_data, time_value)

    # color the whole list as green after the sort
    draw_data(data_list, ["green" for i in range(len(data_list))])
Esempio n. 12
0
def cocktail_sort(data_list, draw_data, time_value):
    """
    Does a cocktail sort on a list and visualze the sort
    Expected Complexity (Sort Only): O(n^2) (time) and O(1) (space)

    :param data_list: Python list to be sorted
    :param draw_data: function written in main.py that visualizes the list
    :param time_value: Float based on the input for time between each step
    """

    # declare and initalize basic variables
    swapped_bool = True
    start_index = 0
    end_index = len(data_list) - 1

    while swapped_bool == True:
        swapped_bool = False

        # similar to bubble sort, moves the largest values to the top
        for i in range(start_index, end_index):
            if data_list[i] > data_list[i + 1]:
                _swap(data_list, i, i + 1)

                # generate the color list for the visualization function
                color_list = ["red" for i in range(len(data_list))]

                # color the two elements being swapped green
                for x in range(len(color_list)):
                    if (x == i) or (x == i + 1):
                        color_list[x] = "blue"

                # visualize the step and wait for the specified amount of time
                draw_data(data_list, color_list)
                time.sleep(time_value)

                # keep the sort going
                swapped_bool = True

        # stops after each element has been swapped in the right place
        if swapped_bool == False:
            break

        swapped_bool = False

        # move the end iterator one back
        end_index -= 1

        # does the opposite of the first half, moving values to the bottom
        for i in range(end_index - 1, start_index - 1, -1):
            if data_list[i] > data_list[i + 1]:
                _swap(data_list, i, i + 1)

                # generate the color list for the visualization function
                color_list = ["red" for i in range(len(data_list))]

                # color the two elements being swapped green
                for x in range(len(color_list)):
                    if (x == i) or (x == i + 1):
                        color_list[x] = "orange"

                # visualize the step and wait for the specified amount of time
                draw_data(data_list, color_list)
                time.sleep(time_value)

                swapped_bool = True

        # move the starting iterator one forward
        start_index += 1

    # finally color all of the values in the list green after the sort
    draw_data(data_list, ["green" for i in range(len(data_list))])