04 Sorting
04 Sorting
Bryce Boe
2012/08/13
CS32, Summer 2012 B
Overview
• Bubble Sort
• Insertion Sort
• Selection Sort
• Merge sort
• Heapsort
• Quicksort
• Project 1 Part 2
Bubble Sort
• Mini lecture by Tyralyn Tran
• Quick recap
– Until done, bubble largest elements to the right
side
– Worst case: O(n2)
– Best case: O(n) – requires optimization
Insertion Sort
• Mini lecture by Shanen Cross
• Quick recap
– Left side is sorted: continuously select the left
most unsorted element and move it left until it is
in the proper location
– Worst case: O(n2)
– Best case: O(n)
Selection Sort
• Mini lecture by Wei Guo
• Quick recap:
– Right side is sorted: Until done, find the largest
unsorted element, swap it into its final location
(right most unsorted position)
– Worst Case: O(n2)
– Best Case: O(n2)
– Benefits: Requires fewer swaps
Merge Sort
• Mini lecture by Grant Ball
• Quick recap:
– Recursively* divide into smaller and smaller chunks
until down to 1 element. Merge two already sorted
lists into a larger sorted list. Repeat until all the data
has been merged
– Divide and conquer algorithm
– Best/Worst Case: O(n*log(n))
• Requires log(n) merge steps of n amount of data