180 likes | 371 Views
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?.
E N D
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? How can we modify its elements?
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”
Last Lecture - Algorithm of Linear Search For each element Is element == target? If so, found!
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
Implementation of Linear Search • Using Repl.It
Let’s test it! ourList= [ 0, 6, 9, 2, 5, 3, 7, 1, 2, 4 ] target: 2
What other test cases can we use? ourListtarget
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?
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!
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!
First, the basic algorithms: Bubble, Selection and Insertion Sort Sorting
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
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
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