예제 #1
0
def split_sort_merge(items):
    """Sort given items by splitting list into two approximately equal halves,
    sorting each with an iterative sorting algorithm, and merging results into
    a list in sorted order.
    TODO: Running time: O of whatever the iterative algorithm you used is :/
    TODO: Memory usage: O(1)"""
    # TODO: Split items list into approximately equal halves
    # TODO: Sort each half using any other sorting algorithm
    # TODO: Merge sorted halves into one list in sorted order

    # how long is half the list?
    half = len(items) // 2
    if half == 0:  # len is 0 or 1
        return items

    # split list in two
    first_half = items[:half]
    second_half = items[half:]

    # sort w/ imported selection sort
    sorting_iterative.selection_sort(first_half)
    sorting_iterative.selection_sort(second_half)

    # merge them back together and assign to items
    items[:] = merge(first_half, second_half)
def split_sort_merge(items):
    """Sort given items by splitting list into two approximately equal halves,
    sorting each with an iterative sorting algorithm, and merging results into
    a list in sorted order.

    Running time: O(n/2)^2 = O(n^2)/4 Why and under what conditions? each sorting methods take
    quadratic time and items is divided into two parts before being sorted.
    Memory usage: 2*O(n) Why and under what conditions? we are making a copy for items1 and items2"""
    # print("items: ", items)
    if len(items) <= 1:
        return items

    mid = len(items)//2 #split array in to two parts
    items1 = items[:mid]
    items2 = items[mid:]
    # sort each half
    selection_sort(items1)

    insertion_sort(items2)
    # store returned mereged
    sorted_items = merge(items1, items2, items)
    # replace all indexes in items with items new arrangemnt
    items[:] = sorted_items

    return items
def split_sort_merge(items):
    """Sort given items by splitting list into two approximately equal halves,
    sorting each with an iterative sorting algorithm, and merging results into
    a list in sorted order.
     Running time: O(N^2) running time
        Memory usage: O(N), because we are creating a new list when we merge the two sublists
        that are equal size to N (M+M = N)
    """
    list1, list2 = items[0:len(items) // 2], items[len(items) // 2:]
    print(list1)
    print(list2)
    selection_sort(list1)
    selection_sort(list2)
    return merge(list1, list2)
예제 #4
0
def split_sort_merge(items):
    """Sort given items by splitting list into two approximately equal halves,
    sorting each with an iterative sorting algorithm, and merging results into
    a list in sorted order.
    TODO: Running time: ??? Why and under what conditions?
    TODO: Memory usage: ??? Why and under what conditions?"""
    # TODO: Split items list into approximately equal halves
    # TODO: Sort each half using any other sorting algorithm
    # TODO: Merge sorted halves into one list in sorted order
    center = len(items) // 2
    left = selection_sort(items[:center])
    right = selection_sort(items[center:])
    merged_items = merge(left, right)

    for i in range(len(items)):
        items[i] = merged_items[i]
    return items
예제 #5
0
def split_sort_merge(items):
    """Sort given items by splitting list into two approximately equal halves,
    sorting each with an iterative sorting algorithm, and merging results into
    a list in sorted order.
    Running time: O(n)
    Memory usage: O(n)"""
    # Split items list into approximately equal halves
    # Sort each half using any other sorting algorithm
    # Merge sorted halves into one list in sorted order

    items1, items2 = bisect_list(items)

    selection_sort(items1)

    insertion_sort(items2)

    items[:] = merge(items1,items2)

    return items
예제 #6
0
def split_sort_merge(items):
    """Sort given items by splitting list into two approximately equal halves,
    sorting each with an iterative sorting algorithm, and merging results into
    a list in sorted order.
    TODO: Running time: ??? Why and under what conditions?
    TODO: Memory usage: ??? Why and under what conditions?"""
    n = len(items)

    if n >= 2:
        mid = len(items) // 2
        l = items[:mid]
        r = items[mid:]

        l = selection_sort(l)
        r = selection_sort(r)

        merged = merge(l, r)
        for i in range(len(merged)):
            items[i] = merged[i]
    return items
예제 #7
0
def split_sort_merge(items):
    """
    Sort given items by splitting list into two approximately equal halves,
    sorting each with an iterative sorting algorithm, and merging results into
    a list in sorted order.
    Running time: O(sorting_method()) - depends on the sorting method chosed
    Memory usage: O(n + sorting_method()) - slicing and sorting method
    """
    # Split items list into approximately equal halves
    mid = (len(items) - 1) // 2
    half1, half2 = items[:mid], items[mid:]
    # print(half1, half2)

    # Sort each half using any other sorting algorithm
    selection_sort(half1)  # O(n^2)
    selection_sort(half2)  # O(n^2)
    # print(half1, half2)

    # Merge sorted halves into one list in sorted order
    items[:] = merge(half1, half2)
    # print(items)
    return items
예제 #8
0
 def test_selection_sort_on_sorted_integers(self):
     # Positive test cases (examples) with lists of sorted integers
     assert selection_sort([]) == []  # Empty lists are vacuously sorted
     assert selection_sort([3]) == [3]  # Single item is trivially sorted
     assert selection_sort([3, 3]) == [3, 3]  # Duplicate items are in order
     assert selection_sort([3, 5]) == [3, 5]
     assert selection_sort([3, 5, 7]) == [3, 5, 7]
     # new added test cases
     sample_1 = [3, 7, 9, 11, 13, 15, 16, 17, 19, 20]
     assert selection_sort(sample_1) == sample_1
     sample_2 = [1, 2, 4, 5, 7, 8, 10, 15, 25, 25, 32,
                 37, 37, 38, 38, 39, 39, 42, 42, 47]
     assert selection_sort(sample_2) == sample_2
     sample_3 = [6, 9, 13, 13, 16, 17, 21, 22, 22,
                 22, 23, 24, 25, 29, 30, 34, 34, 36, 40, 43]
     assert selection_sort(sample_3) == sample_3
예제 #9
0
 def test_selection_sort_on_unsorted_integers(self):
     # Negative test cases (counterexamples) with lists of unsorted integers
     sample_1 = [5, 3]
     assert selection_sort(sample_1) == sorted(sample_1)
     sample_2 = [3, 5, 3]
     assert selection_sort(sample_2) == sorted(sample_2)
     sample_3 = [7, 5, 3]
     assert selection_sort(sample_3) == sorted(sample_3)
     sample_4 = [37, 3, 5, 6, 41, 50, 17, 5, 18, 17]
     # new test cases
     assert selection_sort(sample_4) == sorted(sample_4)
     sample_5 = [21, 12, 19, 5, 30, 17, 22, 22, 15, 4]
     assert selection_sort(sample_5) == sorted(sample_5)
     sample_6 = [21, 23, 19, 16, 31, 27, 18, 6, 43, 41, 43, 12, 32, 38, 35]
     assert selection_sort(sample_6) == sorted(sample_6)
예제 #10
0
 def test_selection_sort(self):
     for _ in range(100):
         random_and_sorted = _generate_testcase()
         SortIter.selection_sort(random_and_sorted[0])
         assert random_and_sorted[0] == random_and_sorted[1]