1 / 16

# CMPT 120

CMPT 120. Lecture 30 – Unit 5 – Internet and Big Data Algorithm – Searching and Sorting. Homework from Last Lecture. # HOMEWORK: # Write the linearSearchIndex (...) function https:// repl.it/repls/DefenselessWorseHashmap Solution: https://repl.it/repls/RewardingIdleComputergame.

## CMPT 120

E N D

### Presentation Transcript

1. CMPT 120 Lecture 30 – Unit 5 – Internet and Big Data Algorithm – Searching and Sorting

2. Homework from Last Lecture # HOMEWORK: # Write the linearSearchIndex(...) function https://repl.it/repls/DefenselessWorseHashmap Solution: https://repl.it/repls/RewardingIdleComputergame

3. Review from Last Lecture • Having a look at the Bubble Sort on https://visualgo.net/en/sorting • Which colour is the unsorted part of the data? • What is happening to the 2 green bars appear (2 actions)? Which of these two actions always occurs? • How often are the green bars swapped during one iteration of Bubble Sort? • How many pairs of green bars are “processed” during on iteration of Bubble Sort? • Is there a correlation between the number of iterations Bubble Sort does and the number of elements it is sorting?

4. Review - Selection Sort Algorithm How it works: • Repeatedly selects the next smallest (or largest) element from the unsorted section of the list and swaps it into the correct position in the already sorted sectionof the list: for index = 0 to len(data) – 2 do select: let x = location of element with smallest value in data from index to len(data) – 1 if index != x swap: tempVar = data[index] data[index] = data[x] data [x] = tempVar

5. Review from Last Lecture • Having a look at the Selection Sort on https://visualgo.net/en/sorting • Which colour is the sorted part of the data? • What does the red bar represent? • What does the Selection Sort do with the green bar and the red bar? • What does the Selection Sort do with the two red bars? • How often are the red bars swapped during one iteration of Selection Sort? • Is there a correlation between the number of iterations Selection Sort does and the number of elements it is sorting?

6. In-Class Demo

7. Let’s write some sorting code! • https://repl.it/repls/ParallelMindlessPresses

8. Another way to sort: Insertion Sort • Let’s have a look at the Insertion Sort on https://visualgo.net/en/sorting • What does the red bar represent? • What does the Insertion Sort do with the green bar and the red bar? • Does the Insertion Sort swap bars? • Is there a correlation between the number of iterations Insertion Sort does and the number of elements it is sorting? • Can you figure out the Insertion Sort algorithm?

9. Little Activity About Diving Bell, Butterfly … and Searching!

10. Or seen the movie? Have youreadLe Scaphandre et le Papillon?

11. What happens is … Locked-in syndrome (LIS) is a condition in which a patient is aware but cannot move or communicate verbally due to complete paralysis of nearly all voluntary muscles in the body except for the eyes. Source: wikipedia.org • Jean-Dominique Bauby, editor-in-chief of French fashion bible Elle magazine, has a devastating stroke at age 43 • The damage to his brain stem results in locked-in syndrome, which almost completely paralyzes him … he canonly blink one eye • Yet, Bauby, with the help of a speech therapist, “writes” this book (his memoir) How did he do it?

12. Activity A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Instructions: • Pair up • Think of a solution: • What questions would the speech therapist ask Baubyin order to construct the words of his book? • Baubycouldonly blink one eye • Test it • One of you is Bauby -> think of a word • The other is the speech therapist -> try to guess the word by asking Q’s • Switch!

13. This solution has a name … … Binary search algorithm

14. Another example • Suppose we have a sorted list • Using Binary Search algorithm, we can search for target = 7 without having to look at every element 1 3 4 7 9 11 12 14 21

15. How binary search works – In a nutshell • Binary search uses the fact that the list is sorted! • Find element in the middle -> 9 • Since we are looking for 7 and 7 < 9, then there is no need to search the second half of the list • We can ignore half of the list right away! • Then we repeat the above steps • Bottom line: using binary search, we do not need to look at every element to search a list • So binary search is moretime efficient than linear search

16. Next Lecture • We’ll practise • File I/O • Which is something we will need in order to do Assn 2! • Sorting • So, bring your laptop!  • Monday, we shall look at … • the implementation of Binary Search algorithm • Who to figure out which searching / sorting algorithms are the fastest

More Related