#
# Bubblesort
# Time: O(n) best case, O(n2) average and worst-case
# Space: O(1)
#
# Very simple and largely useless algorithm
# Works through the list checking pairs of elements,
# if left is bigger than right, swap.
# The biggest elements 'bubble' to the end of the list
#

import Random

# the list to sort
listToSort = Random.createRandomList(20)

# swaps the two elements in the passed list
def swap(list, index1, index2):
    index1Val = list[index1]
    index2Val = list[index2]
    list[index1] = index2Val
    list[index2] = index1Val


# sort the list
def bubblesort(list):
    while True:
        swapped = False

        for index in range(len(list)):
            if index == 0:
#
# Basic Quicksort
# Time: O(n log n) O(n2) worst-case
# Space: O(n)
#
# List is split into two at a pivot point half way through the list
# Items smaller than the pivot go into one list, larger items in the other
# The algorithm is called recursively on the two lists
#

import Random

# the list to sort
list = Random.createRandomList(10)
sorted = []

# sort the list
def quicksort(toSort):
	if len(toSort) <= 1: return toSort # no need to sort an empty list
	
	pivotIndex = len(toSort)/2
	pivot = toSort[pivotIndex]
	toSort.pop(pivotIndex)
	
	# partition the list
	lower = []
	higher = []
	for i in range(len(toSort)):
		item = toSort[i]
		if item <= pivot: lower.append(item)
		else: higher.append(item)