Exemplo n.º 1
0
def _partition(array, lo, hi):
    pivot = array[hi]
    i = lo
    for j in range(lo, hi + 1):
        if array[j] < pivot:
            swap(array, i, j)
            i += 1
    swap(array, i, hi)
    return i
Exemplo n.º 2
0
def insertion_sort(li):
    sort_utils.swap_tracker_reset()
    for i in range(len(li) - 1):
        for j in range(i + 1, 0, -1):
            value_before = j - 1
            if li[j] < li[value_before]:
                sort_utils.swap(li, j, value_before)
            else:
                break
    return li
Exemplo n.º 3
0
def selection_sort(li):
    sort_utils.swap_tracker_reset()
    for i in range(len(li) - 1):
        index_smallest = i
        for j in range(i + 1, len(li)):
            if li[j] < li[index_smallest]:
                index_smallest = j
        if index_smallest != 0:
            sort_utils.swap(li, index_smallest, i)
    return li
Exemplo n.º 4
0
def sort(array):
    """
    Selection sort
    Complexity
        Memory
            O(n) - since all swaps "in place"
            and no addition data strictures requires
        Time
            Always O(n^2) - because no matter how array element is placed,
            second pointer go till the end

    """
    for i in range(len(array)):
        j = i
        while j > 0 and array[j - 1] > array[j]:
            swap(array, j, j - 1)
            j -= 1
Exemplo n.º 5
0
def sort(array):
    """
    Selection sort
    Complexity
        Memory
            O(n) - since all swaps "in place"
            and no addition data strictures requires
        Time
            Always O(n^2)
                Inner loop always lookup for minimum element,
                even when array is sorted

    """
    for i in range(len(array)):
        min_element = i
        for j in range(i, len(array)):
            if (array[j] < array[min_element]):
                min_element = j
        swap(array, i, min_element)
Exemplo n.º 6
0
def sort(array):
    """
    Bubble sort
    Complexity
        Memory
            O(n) - use same array and swap "in place"
        Time
            Worst: O(n^2) - when array is reversed
                ((n - 1) + (n - 2) + ... + (n - n))/2 ~ O(n^2)
            Average: O(n^2)
            Best: ϴ(n) - when array is sorted
    """
    is_sorted = False
    while not is_sorted:
        is_sorted = True
        for i in range(len(array) - 1):
            if array[i] > array[i + 1]:
                is_sorted = False
                swap(array, i, i + 1)
#!/usr/bin/env python3

import sort_utils

tests_passed = 0
total_tests = 0

test_name = "Test 1"
print(f"---- {test_name} ----------------------------")
orig = list("abcdef")
li = list(orig)
sort_utils.swap(li, 2, 3)
print(li)
print("----------------------------------------")

total_tests += 1
expected = list("abdcef")
if expected == li:
    tests_passed += 1
else:
    print(f"{test_name} failed.")

test_name = "Test 2"
print(f"---- {test_name} ----------------------------")
orig = list("abcdef")
li = list(orig)
sort_utils.swap(li, 0, 5)
sort_utils.swap(li, 1, 4)
sort_utils.swap(li, 2, 3)
print(li)
print("----------------------------------------")