Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPT 120 Lecture 29 – Unit 5 – Internet and Big Data

Similar presentations


Presentation on theme: "CMPT 120 Lecture 29 – Unit 5 – Internet and Big Data"— Presentation transcript:

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

2 Lists of Lists - Homework
How can we create myMatrix? How can we access each of its elements? myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] How can we slice it? How can we modify its elements?

3 First, the basic algorithm: Linear Search
Searching First, the basic algorithm: Linear Search

4 Last Lecture – We gave it a try!
Is in this sequence? Our reading calls it “The Sequential Search” “target”

5 Last Lecture - Algorithm of Linear Search
For each element Is element == target? If so, found!

6 Another Example of Linear Search Algorithm
linearSearch(list, target) set result to value TARGET_NOT_FOUND set targetNotFound to value true if list not empty set currentElement to first element of list while targetNotFound AND have not looked at every element of list if currentElement == target set result to current element set targetNotFound to false otherwise set currentElement to next element of list return result

7 Implementation of Linear Search
Using Repl.It

8 Let’s test it! ourList = [ 0, 6, 9, 2, 5, 3, 7, 1, 2, 4 ] target: 2

9 What other test cases can we use?
ourList target

10 Observations Our linear search so far:
Finds the first occurrence of the target What if the target occurs many times in the list Returns True/False What else could it return?

11 Linear search algorithm
Advantages Simple to understand, implement and test Disadvantages Slow (time inefficient) because it looks at every element Wait a minute! Not always! We saw that for some of our test cases linear search did not look at every element That is true! We’ll come back to this real soon!

12 “Time Efficient” Searching?

13 Hey! How about if we sort first!
Difficult to find things (CD’s) when they are disorganized It could be quick (first CD we look at) or slow (last CD we look at) Sorting Although it will take some time to sort, it will make searching for a CD much quicker, every time! Unpredictable!

14 First, the basic algorithms: Bubble, Selection and Insertion Sort
Sorting First, the basic algorithms: Bubble, Selection and Insertion Sort

15 Sorting Visualization and Videos!
There are plenty of sorting visualization web sites and videos on the Internet to help you understand these algorithms

16 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 section of 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

17 In-Class Demo

18 Next Lecture Let’s have a look at one more sorting algorithm:
insertion sort Then let’s have a little activity which will allow us to discover a more efficient way of searching 


Download ppt "CMPT 120 Lecture 29 – Unit 5 – Internet and Big Data"

Similar presentations


Ads by Google