1 / 13

Sorting and Searching Lists

Learn how to implement linear and binary search algorithms to create a high score tracker for an arcade game. Explore sorting methods like selection sort to organize the data efficiently. Interactive examples provided.

starke
Download Presentation

Sorting and Searching Lists

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. Sorting and Searching Lists September 26, 2011

  2. Agenda • Review quiz #3 • Team assignment #1 due tonight • One per team • Arcade game • Searching • Linear • Binary • Sorting • Selection Search

  3. What else can we do with a List? • What about a high score tracker for an arcade game? • What data needs to be stored? • Is the data sorted in some way? • Let’s try it out… • Movement • Add info to lists

  4. Linear Search – High Score • Examine each component in the list • Similar to a checkout line • The line of customers is a list • The checkout clerk processes each customer’s purchase one at a time

  5. Linear Search Algorithm – Searching for Highest Score Set largestIndex = 1 Set index = 1 Repeat for the length of L if L[index] > L[largestIndex] set largestIndex = index index = index + 1 Print L[largestIndex]

  6. Example in Scratch

  7. Binary Search • Phonebook example • Information must be sorted first! • How do we sort?

  8. Sorting • How can we sort the scores from the highest score to the lowest score and the players alphabetically? • Selection Sort • Find the largest number in the list • Swap it with the item in the first position • Repeat starting with the second element in the list • What do we need? • Swap method • Sort method

  9. Selection Sort • Similar to a linear search • Find the smallest (or largest) value and place it in the first slot • Repeat this for each position in the list • Animated example • http://www.cs.oswego.edu/~mohammad/classes/csc241/samples/sort/Sort2-E.html

  10. Selection Sort – Players

  11. Binary Search • Phonebook • Look at the middle of the list • If the value is larger than the item in the middle of the list • Look at the last half • Otherwise • Look at the first half

  12. Binary Search Repeat until found = true or min > max mid = max+min/2 if value > L[mid] min = mid + 1 else if value < L[mid] max = mid – 1 else found = true if found = false mid = -1 print mid

  13. Binary Search - BYOB

More Related