Presentation is loading. Please wait.

Presentation is loading. Please wait.

Last Class We Covered Dictionaries Hashing Dictionaries vs Lists

Similar presentations


Presentation on theme: "Last Class We Covered Dictionaries Hashing Dictionaries vs Lists"— Presentation transcript:

1 CMSC201 Computer Science I for Majors Lecture 21 – Searching and Sorting

2 Last Class We Covered Dictionaries Hashing Dictionaries vs Lists
Creating Accessing Manipulating Methods Hashing Dictionaries vs Lists

3 Any Questions from Last Time?

4 Today’s Objectives To learn about some sorting algorithms
Bubble Sort Selection Sort Quicksort To learn about searching algorithms Linear search Binary search

5 Sorting

6 Sorting Algorithms Sorting algorithms put the elements of a list in a specific order A sorted list is necessary to be able to use certain other algorithms Like search algorithms! There must be an order to be able to search – sorting once means we can search quickly forever

7 Sorting Algorithms There are many different ways to sort a list
What method would you use? Now imagine you can only look at at most two elements at a time What method would you use now? Computer science has a number of commonly used sorting algorithms

8 Bubble Sort

9 Bubble Sort Algorithm Let’s take a look at a common sorting method!
We look at the first pair of items in the list, and if the first one is bigger than the second one, we swap them Then we look at the second and third one and put them in order, and so on Once we hit the end of the list, we start over at the beginning Repeat until the list is sorted!

10 Bubble Sort Example [ 4, 8, 1, 10, 13, 14, 6] First pass: 4 and 8 are in order 8 and 1 should be swapped: [ 4, 1, 8, 10, 13, 14, 6] 8 and 10 are in order 10 and 13 are in order 13 and 14 are in order 6 and 14 should be swapped: [ 4, 1, 8, 10, 13, 6, 14]

11 Bubble Sort Example (Cont)
[ 4, 1, 8, 10, 13, 6, 14] Second pass: 4 and 1 should be swapped: [ 1, 4, 8, 10, 13, 6, 14] 4 and 8 are in order 8 and 10 are in order 10 and 13 are in order 13 and 6 should be swapped: [ 1, 4, 8, 10, 6, 13, 14] 13 and 14 are in order

12 Bubble Sort Example (Cont)
[ 1, 4, 8, 10, 6, 13, 14] Third pass: 10 and 6 should be swapped: [ 1, 4, 8, 6, 10, 13, 14] Fourth pass: 8 and 6 should be swapped: [ 1, 4, 6, 8, 10, 13, 14]

13 Video from https://www.youtube.com/watch?v=lyZQPjUT5B4
Bubble Sort Video Video from

14 Selection Sort

15 Selection Sort Algorithm
Here is a very simple way of sorting a list: Find the smallest number in a list Move that to the end of a new list Repeat until the original list is empty Unfortunately, it’s also pretty slow!

16 Video from https://www.youtube.com/watch?v=Ns4TPTC8whw
Selection Sort Video Video from

17 Quicksort

18 Quicksort Algorithm Here’s one more method:
Start with any number (the first one works) Put everything less than that number on the left of it and everything greater than it on the right of it Quicksort the left side and the right side Does this method remind you of anything?

19 Video from https://www.youtube.com/watch?v=ywWBy6J5gz8
Quicksort Video Video from

20 Search

21 Motivations for Searching
Want to know if something exists Python can do this for us! Want to know where something exists Python can actually do this for us too! raceWinners.index("#718") But how does Python does this?

22 Exercise: find() Write a function that takes a list and a variable and returns the index of the variable in the list If it’s not found, return -1 You can’t use .index()! def find(searchList, var)

23 Exercise: find() Solution
def find(searchList, var): for i in range(len(searchList)): if searchList[i] == var: return i # outside the loop, means that # we didn't find the variable return -1

24 Linear Search You just programmed up a search function!
This algorithm is called linear search It’s a common, fundamental algorithm in CS It’s especially useful when our information isn’t in a sorted order But it isn’t very fast

25 Searching Sorted Information
Now, imagine we’re looking for information in something sorted, like a phone book We know someone’s name (it’s our “variable”), and want to find their number in the book What is a good method for locating their phone number? Think about how a person would do this

26 Algorithm in English Open the book midway through.
If the person’s name is on the page you opened to You’re done! If the person’s name is after the page you opened to Tear the book in half, throw the first half away and repeat this process on the second half If the person’s name is before the page you opened to Tear the book in half, throw the second half away and repeat this process on the first half This is rough on the phone book, but you’ll find the name!

27 Binary Search

28 Binary Search The algorithm we just demonstrated is better known as binary search Binary search is a divide and conquer algorithm We’ve talked about these before, remember? Binary search is only usable on sorted lists Why?

29 Binary Search Example Find the letter “J” using binary search 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 A B C D E F G H I J K L M N O

30 Binary Search Example Find the letter “J” using binary search 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 A B C D E F G H I J K L M N O

31 Binary Search Example Find the letter “J” using binary search 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 A B C D E F G H I J K L M N O

32 Binary Search Example Find the letter “J” using binary search 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 A B C D E F G H I J K L M N O

33 Binary Search Example Find the letter “G” using binary search 1 2 3 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 A B C D E F G H I J K L M N O

34 Solving Binary Search Binary search is a problem that can be broken down into Something simple (breaking a list in half) A smaller version of the original problem (searching that half of the list) That means we can use ... recursion!

35 Time for… LIVECODING!!!

36 Exercise: Recursive Binary Search
Write a recursive binary search! To make the problem slightly easier, make it “checking to see if something is in a sorted list” Simply return True or False to answer this If there’s no “middle” of the list, we’ll just look at the lower of the two “middle” indexes

37 Exercise: Recursive Binary Search
Write a recursive binary search! Remember to ask yourself: What is our base case(s)? What is the recursive step? def binarySearch(theList, item): A hint: in order to get the number at the middle of the list, use this bit of code: myList[len(theList) // 2]

38 Daily CS History Mark Dean Holds 3 of 9 patents for the IBM PC
Part of team that developed the ISA bus (used to connect I/O devices) Led design of the 1-gigahertz chip Computing visionary Predicted the tablet computer in 1999 /archive_ htm Early in the next century, Dean hopes his new concoction, which he says is "in the idea and invention stage," will be ready for the public: a sleek tablet that is magazine-size, inexpensive, programmable, and voice-activated. He expects his unnamed dream pad, which will run on a 24-hour battery, to provide everything a PC does, including streaming audio and video, word processing, and spreadsheets. It will even have a port for old fogies who can't give up their keyboards. And it will wirelessly put the Internet and other information at your fingertips.

39 Announcements Watch for a Blackboard announcement about Project 3 today Final exam is Friday, December 14th at 6 PM Course evaluations should be out to you soon, please take the time to fill them out Especially for this class!

40 Image Sources Sorting video screenshots: Mark Dean: Bubble sort:
Selection sort: Quicksort: Mark Dean:


Download ppt "Last Class We Covered Dictionaries Hashing Dictionaries vs Lists"

Similar presentations


Ads by Google