<filename>Code/sorting_iterative.py
#!python


def is_sorted(items):
    """Return a boolean indicating whether given items are in sorted order.
    Running time: Worst case is O(n)
    Memory usage: O(1)"""
    for i in range(len(items)-1):
        if items[i] > items[i+1]:
            return False
    return True



def bubble_sort(items):
    """Sort given items by swapping adjacent items that are out of order, and
    repeating until all items are in sorted order.
    Running time: O(n^2)
    Memory usage: O(1)"""
    for i in range(len(items)-1):
        done = True
        for j in range(len(items)-1-i):
            if items[j] > items[j+1]:
                items[j], items[j+1] = items[j+1], items[j]
                done = False
        if done:
            break


def selection_sort(items):
    """Sort given items by finding minimum item, swapping it with first
    unsorted item, and repeating until all items are in sorted order.
    Running time: O(n^2)
    Memory usage: O(1)"""
    for i in range(len(items)):
        min_index = i
        for j in range(i+1, len(items)):
            if items[j] < items[min_index]:
                min_index = j
        items[i], items[min_index] = items[min_index], items[i]


def insertion_sort(items):
    """Sort given items by taking first unsorted item, inserting it in sorted
    order in front of items, and repeating until all items are in order.
    Running time: O(n^2)
    Memory usage: O(1)"""
    for i in range(1, len(items)):
        j = i
        while j > 0 and items[j-1] > items[j]:
            items[j], items[j-1] = items[j-1], items[j]
            j -= 1

<filename>Code/sorting_iterative.py
#!python


def is_sorted(items):
    """Return a boolean indicating whether given items are in sorted order.
    Running time: Worst case is O(n)
    Memory usage: O(1)"""
    for i in range(len(items)-1):
        if items[i] > items[i+1]:
            return False
    return True



def bubble_sort(items):
    """Sort given items by swapping adjacent items that are out of order, and
    repeating until all items are in sorted order.
    Running time: O(n^2)
    Memory usage: O(1)"""
    for i in range(len(items)-1):
        done = True
        for j in range(len(items)-1-i):
            if items[j] > items[j+1]:
                items[j], items[j+1] = items[j+1], items[j]
                done = False
        if done:
            break


def selection_sort(items):
    """Sort given items by finding minimum item, swapping it with first
    unsorted item, and repeating until all items are in sorted order.
    Running time: O(n^2)
    Memory usage: O(1)"""
    for i in range(len(items)):
        min_index = i
        for j in range(i+1, len(items)):
            if items[j] < items[min_index]:
                min_index = j
        items[i], items[min_index] = items[min_index], items[i]


def insertion_sort(items):
    """Sort given items by taking first unsorted item, inserting it in sorted
    order in front of items, and repeating until all items are in order.
    Running time: O(n^2)
    Memory usage: O(1)"""
    for i in range(1, len(items)):
        j = i
        while j > 0 and items[j-1] > items[j]:
            items[j], items[j-1] = items[j-1], items[j]
            j -= 1

#!python Return a boolean indicating whether given items are in sorted order.     Running time: Worst case is O(n)     Memory usage: O(1) Sort given items by swapping adjacent items that are out of order, and     repeating until all items are in sorted order.     Running time: O(n^2)     Memory usage: O(1) Sort given items by finding minimum item, swapping it with first     unsorted item, and repeating until all items are in sorted order.     Running time: O(n^2)     Memory usage: O(1) Sort given items by taking first unsorted item, inserting it in sorted     order in front of items, and repeating until all items are in order.     Running time: O(n^2)     Memory usage: O(1)