1 Insertion Sort

Selection sort although not very efficient is often used when the arrays you are sorting are very small. Another benefit of insertion sort is that it is very easy to program. Essentially, this algorithm has a sorted section which slowly grows as it pull in new elements to their sorted position.

1.1 Functional Notation

If you are not familiar with functional programming, this way of writing insertion sort may scare you. Essentially the ‘s’ stands for sort and the ‘i’ stands for insert. For the sort section you are taking off the first element and inserting it into the rest of the sorted array. For the insert section you are placing the element in its sorted position.

This notation will make python programmers feel a lot more comfortable.

2 Merge Sort

Merge sort is a classic example of a divide and conquer algorithm. Each iteration, the problem is cut in half making sorting it easier. Once you have your array divided into sorted sections, it is easy to combine into a larger sorted array.

3 Quick Sort

This sorting algorithm asymptotically is the same as merge sort: \(O(nlog(n))\). However, in practice this algorithm is actually faster than merge sort due to constant factors. The general premise of this algorithm is that each iteration you will divide your array into three sections : less, equal, greater. The items in each section are based on a random element in the array. You will continue this process until you get every element by itself which makes it trivial to sort.

3.4 In-Place Sorting Implementation

def iterative_partition(data, left, right):
"""
Function which partitions the data into two segments,
the left which is less than the pivot and the right
which is greater than the pivot. The pivot for this
algo is the right most index. This function returns
the ending index of the pivot.
:param data: array to be sorted
:param left: left most portion of array to look at
:param right: right most portion of the array to look at
"""
x = data[right]
i = left - 1
j = left
while j < right:
if data[j] <= x:
i = i + 1
data[i], data[j] = data[j], data[i]
j = j+1
data[i + 1], data[right] = data[right], data[i + 1]
return i + 1
def iterative_quick_sort(data):
"""
In place implementation of quick sort
Wrapper function for iterative_quick_sort_helper which
initializes, left, right to be the extrema of the array.
"""
iterative_quick_sort_helper(data, 0, len(data) -1)
return data
def iterative_quick_sort_helper(data, left, right):
"""
Uses the divide and conquer algo to sort an array
:param data: array of data
:param left: left index bound for sorting
:param right: right bound for sorting
"""
if left < right:
pivot = iterative_partition(data, left, right)
iterative_quick_sort_helper(data, left, pivot -1)
iterative_quick_sort_helper(data, pivot+1, right)