Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to Computer Science CS1510 Dr. Sarah Diesburg

Similar presentations


Presentation on theme: "Intro to Computer Science CS1510 Dr. Sarah Diesburg"— Presentation transcript:

1 Intro to Computer Science CS1510 Dr. Sarah Diesburg
More Sorting Intro to Computer Science CS1510 Dr. Sarah Diesburg

2 Summary What have we done so far?
Refreshed ourselves on definition of algorithm Searches Linear and binary Big O notation Sorts

3 Sorting Methods Bubble Sort Higher elements “bubble” to the end
Compare two elements Move the lower element to the left Look at the next element Repeat Higher elements “bubble” to the end After each run, one more high element is in order Lower elements slowly “bubble” to the bottom

4 Bubble Sort See sortingAlgorithms.py

5 Big O of the Bubble Sort Roughly how many comparisons do we make with the bubble sort in the worst case Roughly n comparisons over n times = n2 What is this saying? As n grows, the time the algorithm takes grows by roughly a square Example: As you double your data, you quadruple your time

6 Big O In fact, the big O of the other sorts we will talk about today is also n2!

7 Sorting Methods Insertion Sort
Two chunks of data (sorted and unsorted) Go through unsorted data and insert it in order into sorted part of the list As humans, if we could look at all elements at once, we would probably perform an insertion sort

8 Insertion Sort See sortingAlgorithms.py

9 Sorting Methods Selection Sort Find smallest card by
Comparing two cards at a time Saving out the current smallest card Repeat until reach end of pile Put smallest card in sorted pile Repeat

10 Selection Sort See sortingAlgorithms.py

11 Quicksort We won’t be looking at the code, because it needs something called recursion to run We don’t have time to introduce this Instead, let’s think generally about how it works

12 Quicksort Pick a middle element
Place all elements lower than the middle partition into one list Place all elements higher than the partition into another list Repeat with lists until lists are small Sort small lists with bubble or insertion sort Merge sorted lists back together


Download ppt "Intro to Computer Science CS1510 Dr. Sarah Diesburg"

Similar presentations


Ads by Google