def test_one_in_list():
    arr= [1]
    expected = [1]
    arr2=insertion_sort(arr)
    assert expected == arr
def test_one():
    arr=[1,2,3,56,223,78,24,90,13]
    expected = [1,2,3,13,24,56,78,90,223]
    arr2=insertion_sort(arr)
    assert expected == arr2
def test_empty_list():
    arr= []
    expected = []
    arr2=insertion_sort(arr)
    assert expected == arr
def test_reverse_list():
    arr= [5,4,3,2,1]
    expected = [1,2,3,4,5]
    arr2=insertion_sort(arr)
    assert expected == arr
def test_random():
    arr = [i for i in np.random.randint(1,101,100)]
    insertion_sort(arr)
    for j in range (1,len(arr)):
        assert arr[j-1]<= arr[j]
def test_two():
    arr=['car','bar','tar','zar']
    expected = ['bar','car','tar','zar']
    insertion_sort(arr)
    assert expected == arr
Ejemplo n.º 7
0
import copy
from sorts import insertion_sort
from sorts.tests.list_generator import get_random_list

# tests with random lists with different length
test_list = get_random_list(1)
copy_list = copy.copy(test_list)
copy_list.sort()
assert (insertion_sort.insertion_sort(test_list) == copy_list)

test_list = get_random_list(3)
copy_list = copy.copy(test_list)
copy_list.sort()
assert (insertion_sort.insertion_sort(test_list) == copy_list)

test_list = get_random_list(5)
copy_list = copy.copy(test_list)
copy_list.sort()
assert (insertion_sort.insertion_sort(test_list) == copy_list)

test_list = get_random_list(10)
copy_list = copy.copy(test_list)
copy_list.sort()
assert (insertion_sort.insertion_sort(test_list) == copy_list)
Ejemplo n.º 8
0
def sort(image_columns, randomized_image, argv):
    """
    Chooses which sorting algorithm to use based on the parameters in argv.

    Parameters
    ----------
    image_columns : list
        A list of one-pixel wide columns of an image.

    randomized_image : PIL Image File
        The user's original image after it was randomized.

    argv : list
        A list of commands used in the program invocation.
        The command at argv[1] designates what sorting algorithm
        will be used.

    Returns
    -------
    list
        A list of images that are, essentially, the frames of the video.

    """
    video_frames = []

    if len(argv) == 1 or argv[1] == "bubble":
        video_frames = bubblesort.bubblesort(image_columns, randomized_image)

    elif argv[1] == "select":
        video_frames = selection_sort.selection_sort(image_columns,
                                                     randomized_image)

    elif argv[1] == "dselect":
        video_frames = double_selection_sort.double_selection_sort(
            image_columns, randomized_image)

    elif argv[1] == "insert":
        video_frames = insertion_sort.insertion_sort(image_columns,
                                                     randomized_image)

    elif argv[1] == "merge":
        print("Merge Sort")
        exit(0)

    elif argv[1] == "quick":
        counters = [
            0, 0
        ]  # Index 0 is the comparison counter, Index 1 is the swap counter

        # Because quicksort is a recursive sorting algorithm, it needs to be changed to fit my needs
        qs_ret = quicksort.quicksort(image_columns, 0,
                                     len(image_columns) - 1,
                                     [randomized_image], randomized_image,
                                     counters)

        # The frames of the video are returned at index 0, and the counters are returned at index 1.
        # Again, comparison counter is at index 0, and swap counter is at index 1.
        video_frames = qs_ret[0]
        comp_ctr = qs_ret[1][0]
        swap_ctr = qs_ret[1][1]

        # Since quicksort is recursive, we must print the statistics outside of the sorting function.
        print("Sorting Algorithm: Quicksort")
        print("Number of Columns to be Sorted: ", len(image_columns))
        print("Number of Array Comparisons: ", comp_ctr)
        print("Number of Array Swaps: ", swap_ctr)

    elif argv[1] == "heap":
        print("Heap Sort")
        exit(0)

    elif argv[1] == "count":
        print("Counting Sort")
        exit(0)

    elif argv[1] == "radix":
        print("Radix Sort")
        exit(0)

    elif argv[1] == "bucket":
        print("Bucket Sort")
        exit(0)

    else:
        print("Not a valid sort, try again.")
        exit(0)

    return video_frames
Ejemplo n.º 9
0
from graphs.data import graph
from sorts.data import get_list_of_randint


array = get_list_of_randint()

# Bubble sorting
from sorts.bubble_sort import bubble_sort
print(bubble_sort(array))

# Insertion sorting
from sorts.insertion_sort import insertion_sort
print(insertion_sort(array))

# Merge sorting
from sorts.merge_sort import merge_sort
print(merge_sort(array))

# Quick sorting
from sorts.quick_sort import quick_sort
print(quick_sort(array))

# Breadth first search
from graphs.breadth_first_search import bfs
print(bfs(graph, 1))

# Depth first search
from graphs.depth_first_search import dfs
print(dfs(graph, 1))