The Sequential Search (Linear Search)

Slides:



Advertisements
Similar presentations
Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Advertisements

Algorithm Analysis.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
The Quick Sort Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Searching Algorithms. Lecture Objectives Learn how to implement the sequential search algorithm Learn how to implement the binary search algorithm To.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Searching Chapter 16 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
The Insertion Sort Mr. Dave Clausen La Cañada High School.
The Binary Search Textbook Authors: Ken Lambert & Doug Nance PowerPoint Lecture by Dave Clausen
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Chapter 8 ARRAYS Continued
Array operations II manipulating arrays and measuring performance.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Starting Out with C++, 3 rd Edition 1 Searching an Arrays.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Standard Algorithms –search for an item in an array –count items in an array –find the largest (or smallest) item in an array.
Chapter Searching and Sorting Arrays 8. Introduction to Search Algorithms 8.1.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
CS 162 Intro to Programming II Searching 1. Data is stored in various structures – Typically it is organized on the type of data – Optimized for retrieval.
LAB#7. Insertion sort In the outer for loop, out starts at 1 and moves right. It marks the leftmost unsorted data. In the inner while loop, in starts.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
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.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Chapter 10: Class Vector and String, and Enumeration Types Java Programming: Program Design Including Data Structures Program Design Including Data Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Searching Topics Sequential Search Binary Search.
Data Structures Arrays and Lists Part 2 More List Operations.
The Selection Sort Mr. Dave Clausen La Cañada High School.
 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.
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 Arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
Lecture 14 Searching and Sorting Richard Gesick.
Introduction to Search Algorithms
Introduction to complexity
The Sequential Search (Linear Search)
Control Statements Lecture 6 from Chapter 5.
Mr. Dave Clausen La Cañada High School
Outline Late Binding Polymorphism via Inheritance
searching Concept: Linear search Binary search
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Lecture Set 9 Arrays, Collections and Repetition
Ken Lambert & Doug Nance
Mr. Dave Clausen La Cañada High School
Mr. Dave Clausen La Cañada High School
Searching and Sorting Arrays
Module 8 – Searching & Sorting Algorithms
The Binary Search by Mr. Dave Clausen
Sorting and Searching -- Introduction
Module 8 – Searching & Sorting Algorithms
The Sequential Search (Linear Search)
Module 8 – Searching & Sorting Algorithms
Sorting Algorithms.
Mr. Dave Clausen La Cañada High School
Presentation transcript:

The Sequential Search (Linear Search) Mr. Dave Clausen La Cañada High School http://www.lcusd.net/lchs/dclausen/apcs_java Mr. D. Clausen

The Sequential Search Description The Sequential (or Linear) Search examines the first element in the list and then examines each “sequential” element in the list (in the order that they appear) until a match is found. This match could be a desired word that you are searching for, or the minimum number in the list. 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

The Sequential Search Variations Variations on this include: searching a sorted list for the first occurrence of a data value, searching a sorted list for all occurrences of a data value (or counting how many matches occur: inventory), or searching an unsorted list for the first occurrence or every occurrence of a data value. You may indicate that a match has been found, the number of matches that have been found, or the indices where all the matches have been found. 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

Sequential Search Algorithm Set index to 0 while index < length if list[index] is equal to target then return index else Increment the index by 1 return -1 5/15/2019 Mr. Dave Clausen

Sequential Search Java int Sequential_Search(int target, int[] list, int length) { int index = 0; while (index < length) if (list[index] = = target) return index; else index++; return -1; } 5/15/2019 Mr. Dave Clausen

Sequential Search Java: for loop int Linear_Search (int[ ] list, int searchValue, int length) { for (int index = 0; index < length; index ++) if (list[index ] == searchValue) return index ; return -1; } 5/15/2019 Mr. Dave Clausen

Sequential Search C + + int search(int target, const apvector<int> &v) { int index = 0; while (index < v.length()) if (v[index] = = target) return index; else ++index; return -1; } 5/15/2019 Mr. Dave Clausen

A More Efficient Sequential Search Algorithm Set index to 0 (zero) Set found to false While index < length and not found do If list[index] is equal to target then set found to be true Else Increment the index by 1 (one) If found then return index Return -1 (negative one) 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

A Sequential Search Java int Sequential_Search(int target, int[] list, int length) { int index = 0; boolean found = false; while((index < length) && ! found) if (list[index] = = target) found = true; else index++; if (found) return index; return -1 } 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

A Sequential Search C + + int Sequential_Search(int target, apvector <int> &list, int length) { int index = 0; bool found = false; while((index < length) && ! found) if (list[index] = = target) found = true; else ++index; if (found) return index; return -1 } 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

The Sequential Search Java Variation #1 If the list is sorted, we can improve this code by adding the following extended if statement: if (list[index] = = target) found = true: else if (list[index] > target) //target is not in list index = length; else index++; 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

The Sequential Search C + + Variation #1 If the list is sorted, we can improve this code by adding the following extended if statement: if (list[index] = = target) found = true: else if (list[index] > target) //target is not in list index = length; else index++; 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

The Sequential Search Java Variation #2 Whether the list is sorted or not, we can return the number of occurrences of the target in the list: int Occurrences_Of (int target, int [ ] list, int length) { int count = 0; for(int index = 0; index < length; index++) if (list[index] = = target) count++; return count; } 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

The Sequential Search C + + Variation #2 Whether the list is sorted or not, we can return the number of occurrences of the target in the list: int Occurrences_Of (int target, const apvector <int> &list) { int count = 0; for(int index = 0; index < list.length(); ++index) if (list[index] = = target) + + count; return count; } 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

The Sequential Search Java Variation #3 Whether the list is sorted or not, we can return the indices of occurrences of the target in the list: void Indices_Of (int target, int [ ] list, int length) { for(int index = 0; index < length; index++) if (list[index] = = target) System.out.println(“” + target + “ located at index # “ + index); } 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

The Sequential Search C + + Variation #3 Whether the list is sorted or not, we can return the indices of occurrences of the target in the list: void Indices_Of (int target, const apvector<int> &list) { for(int index = 0; index < list.length(); ++index) if (list[index] = = target) cout<< target << “ located at index # “ <<index<<endl; } 5/15/2019 Mr. Dave Clausen Mr. D. Clausen

A Sequential Search Example Target ? We start by searching for the target at the first element in the List and then proceed to examine each element in the order in which they appear. 5/15/2019 Mr. Dave Clausen

A Sequential Search Example Target ? 5/15/2019 Mr. Dave Clausen

A Sequential Search Example Target ? 5/15/2019 Mr. Dave Clausen

A Sequential Search Example Target ? 5/15/2019 Mr. Dave Clausen

A Sequential Search Example Once the target data item has been found, you may return a Boolean true, or the index where it was found. Target ! 5/15/2019 Mr. Dave Clausen

Big - O Notation Big - O notation is used to describe the efficiency of a search or sort. The actual time necessary to complete the sort varies according to the speed of your system. Big - O notation is an approximate mathematical formula to determine how many operations are necessary to perform the search or sort. The Big - O notation for the Sequential Search is O(n), because it takes approximately n passes to find the target element. 5/15/2019 Mr. Dave Clausen