Previous Lecture Lecture 14 Next Lecture

Lecture 14, Tue 11/17

Mergesort, Quicksort

Recorded Lecture: 11_17_20

Divide and Conquer Algorithms

Merge Sort

Hand drawn example

mergesortExample.png

Merge Sort Implementation

def mergeSort(alist):
	if len(alist) > 1:
		mid = len(alist) // 2

		# uses additional space to create the left / right
		# halves
		lefthalf = alist[:mid]
		righthalf = alist[mid:]

		# recursively sort the lefthalf, then righthalf
		mergeSort(lefthalf)
		mergeSort(righthalf)

		# merge two sorted sublists (left / right halves)
		# into the original list (alist)
		i = 0 # index for lefthalf sublist
		j = 0 # index for righthalf sublist
		k = 0 # index for alist

		while i < len(lefthalf) and j < len(righthalf):
			if lefthalf[i] <= righthalf[j]:
			alist[k] = lefthalf[i]
			i = i + 1
			else:
			alist[k] = righthalf[j]
			j = j + 1
			k = k + 1

		# put the remaining lefthalf elements (if any) into
		# alist
		while i < len(lefthalf):
			alist[k] = lefthalf[i]
			i = i + 1
			k = k + 1

		# put the remaining righthalf elements (if any) into
		# alist
		while j < len(righthalf):
			alist[k] = righthalf[j]
			j = j + 1
			k = k + 1
# pytest
def test_mergeSort():
   numList1 = [9,8,7,6,5,4,3,2,1]
   numList2 = [1,2,3,4,5,6,7,8,9]
   numList3 = []
   numList4 = [1,9,2,8,3,7,4,6,5]
   numList5 = [5,4,6,3,7,2,8,1,9]
   mergeSort(numList1)
   mergeSort(numList2)
   mergeSort(numList3)
   mergeSort(numList4)
   mergeSort(numList5)

   assert numList1 == [1,2,3,4,5,6,7,8,9]
   assert numList2 == [1,2,3,4,5,6,7,8,9]
   assert numList3 == []
   assert numList4 == [1,2,3,4,5,6,7,8,9]
   assert numList5 == [1,2,3,4,5,6,7,8,9]

Merge Sort Analysis

mergesortAnalysis.png

Quicksort

How do we partition the list into left / right sub lists?

  1. Choose pivot (typically first element in list)
  2. leftmark index is on left-most side of list, rightmark index is on right-most side of list, and both leftmark and rightmark work inwards
  3. Find an element in the left side (using leftmark index) that’s out-of-place (> pivot)
  4. Find an element in the right side (using rightmark index) that’s out-of-place (< pivot)
  5. Swap out-of-place elements with each other
    • We’re putting them in the correct side of the list!
  6. Continue doing steps 1 - 5 until the rightmark index < leftmark index
  7. Swap the pivot with rightmark index
    • We’re putting the pivot element in the correct place!