The Sequential Search (Linear Search)

Slides:



Advertisements
Similar presentations
Alan YorinksLecture 7 1 • Tonight we will look at:: • List ADT • Unsorted List • Sequential Search • Selection Sort • Sorted List • Binary Search.
Advertisements

Lesson 8 Searching and Sorting Arrays 1CS 1 Lesson 8 -- John Cole.
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Algorithm Analysis.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
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.
Sorting and Searching. Searching List of numbers (5, 9, 2, 6, 3, 4, 8) Find 3 and tell me where it was.
Searching Arrays. COMP104 Lecture 22 / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and return its index if the.
1 Lecture 23:Applications of Arrays Introduction to Computer Science Spring 2006.
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.
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
1 Techniques of Programming CSCI 131 Lecture 29 Search.
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 Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
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.
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 Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index.
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.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
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)
Write code to prompt for 5 grades, read them in, print “Thank you”, then reprint the 5 grades and their average. cout >
Search Algorithms Sequential Search (Linear Search) Binary Search
Mr. Dave Clausen La Cañada High School
Lecture 11 Searching and Sorting Richard Gesick.
Searching and Sorting Arrays
Standard Version of Starting Out with C++, 4th Edition
Ken Lambert & Doug Nance
Mr. Dave Clausen La Cañada High School
Mr. Dave Clausen La Cañada High School
Searching and Sorting Arrays
The Binary Search by Mr. Dave Clausen
The Sequential Search (Linear Search)
Sorting Algorithms.
Mr. Dave Clausen La Cañada High School
Presentation transcript:

The Sequential Search (Linear Search) Dave Clausen La Cañada High School 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. 7/22/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. 7/22/2019 Mr. Dave Clausen Mr. D. Clausen

A 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) 7/22/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 } 7/22/2019 Mr. Dave Clausen Mr. D. Clausen

Revised Sequential Search Algorithm Set index to 0 Set found to false While index < length and not found do If v[index] is equal to target then Return index Else Increment the index by 1 Return -1 7/22/2019 Mr. Dave Clausen

Revised 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; } 7/22/2019 Mr. Dave 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; 7/22/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; } 7/22/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; } 7/22/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. 7/22/2019 Mr. Dave Clausen

A Sequential Search Example Target ? 7/22/2019 Mr. Dave Clausen

A Sequential Search Example Target ? 7/22/2019 Mr. Dave Clausen

A Sequential Search Example Target ? 7/22/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 ! 7/22/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. 7/22/2019 Mr. Dave Clausen