September 26, 2011 Sorting and Searching Lists. Agenda Review quiz #3 Team assignment #1 due tonight  One per team Arcade game Searching  Linear  Binary.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Chapter 9: Searching, Sorting, and Algorithm Analysis
Outline Polymorphic References Polymorphism via Inheritance Polymorphism via Interfaces Sorting Searching Event Processing Revisited File Choosers and.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching and Sorting SLA Computer Science 4/16/08 Allison Mishkin.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
1 Sorting/Searching and File I/O Sorting Searching Reading for this lecture: L&L
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 9 Searching.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Chapter 19: Searching and Sorting Algorithms
1 Searching. 2 Searching Searching refers to the operation of finding an item from a list of items based on some key value. Two Searching Methods (1)
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Computer Science Searching & Sorting.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Chapter 2 ARRAYS.
Arrays The concept of arrays Using arrays Arrays as arguments Processing an arrays data Multidimensional arrays Sorting data in an array Searching with.
Chapter 5 Searching and Sorting. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine the linear search and binary.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
CSCI 51 Introduction to Programming March 12, 2009.
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Data Structures Arrays and Lists Part 2 More List Operations.
CMPT 120 Topic: Sorting Algorithms – Part 1. Last Lectures Searching algorithms and their time efficiency Linear search is of order n -> O(n) i.e., has.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
Chapter 16: Searching, Sorting, and the vector Type.
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Searching algorithms with simple arrays
Searching and Sorting Arrays
Searching and Sorting Algorithms
Introduction to Search Algorithms
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Linear and Binary Search
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
Introduction to Programming
Chapter 8 Search and Sort
Outline Late Binding Polymorphism via Inheritance
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Linear Search Binary Search Tree
Principles of Computing – UFCFA3-30-1
Searching and Sorting Arrays
Heaps By JJ Shepherd.
Principles of Computing – UFCFA3-30-1
Applications of Arrays
Sorting Algorithms.
Presentation transcript:

September 26, 2011 Sorting and Searching Lists

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

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

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

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]

Example in Scratch

Binary Search Phonebook example Information must be sorted first!  How do we sort?

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

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  sc241/samples/sort/Sort2-E.html

Selection Sort – Players

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

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

Binary Search - BYOB