1 / 18

CMPT 120

CMPT 120. Lecture 29 – Unit 5 – Internet and Big Data Algorithm – Searching and Sorting. Lists of Lists - Homework. How can we create myMatrix ?. myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. How can we access each of its elements?. How can we slice it?.

bing
Download Presentation

CMPT 120

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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? myMatrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] How can we access each of its elements? How can we slice it? How can we modify its elements?

  3. First, the basic algorithm: Linear Search Searching

  4. Last Lecture – We gave it a try! 42 8 12 34 2 67 33 26 89 54 • Is 89 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? ourListtarget

  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

  15. Sorting Visualization and Videos! • There are plenty of sorting visualization web sites and videos on the Internet to help you understand these algorithms • https://visualgo.net/bn/sorting

  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 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

  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 

More Related