Programming Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
Starting Out with C++, 3 rd Edition 1 Chapter 8 – Searching and Sorting Arrays.
Programming Recursion.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Recursion COMP171 Fall Recursion / Slide 2 Recursion * In some problems, it may be natural to define the problem in terms of the problem itself.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
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.
Programming Sorting Arrays. COMP102 Prog. Fundamentals. Sorting I/ Slide 2 Sorting l To arrange a set of items in sequence. l It is estimated that 25~50%
Sorting and Searching. Problem Read in a parameter value n, then read in a set of n numbers. Print the numbers in their original order. Sort the numbers.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
Searching Arrays Linear search Binary search small arrays
CSE202: Lecture 14The Ohio State University1 Arrays.
Searching and Sorting Arrays
Ordered Arrays An array is ordered if the elements are in ascending or descending order. The array may be ordered numerically or alphabetically (which.
Searching Arrays. COMP104 Array Sorting & Searching / Slide 2 Unordered Linear Search * Search an unordered array of integers for a value and save its.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
1 Techniques of Programming CSCI 131 Lecture 29 Search.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
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.
Data Structures Using C++1 Search Algorithms Sequential Search (Linear Search) Binary Search.
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
AL-HUSEEN BIN TALAL UNIVERSITY College of Engineering Department of Computer Engineering Algorithms and Data Structures Search Algorithms Course No.:
1 Lecture 5: Part 1 Searching Arrays Searching Arrays: Linear Search and Binary Search Search array for a key value Linear search  Compare each.
1 Searching and Sorting Linear Search Binary Search.
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.
COMP102 Lab 121 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
Lecture 16: Searching and Sorting Arrays Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
1 2. Program Construction in Java. 2.8 Searching.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
SEARCHING.  This is a technique for searching a particular element in sequential manner until the desired element is found.  If an element is found.
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.
CSC 211 Data Structures Lecture 13
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
Math & Recursion COMP171 Fall Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition * Theorem.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
STARTING OUT WITH STARTING OUT WITH Class 3 Honors.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
 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.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Searching Arrays Linear search Binary search small arrays
Binary Search.
The Ohio State University
Search Algorithms Sequential Search (Linear Search) Binary Search
Searching CSCE 121 J. Michael Moore.
Chapter 8 – Searching and Sorting Arrays
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
Presentation transcript:

Programming Searching Arrays

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International Thomson Publishing Inc. l The process used to find the location of a target among a list of objects l Searching an array finds the index of first element in an array containing that value Searching

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 3 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc.

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 4

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 5 Unordered Linear Search l Search an unordered array of integers for a value and return its index if the value is found. Otherwise, return -1. A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] l Algorithm: Start with the first array element (index 0) while(more elements in array){ if value found at current index, return index; Try next element (increment index); } Value not found, return -1;

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 6 Unordered Linear Search // Searches an unordered array of integers int search(int data[], //input: array int size, //input: array size int value){ //input: search value // output: if found, return index; // otherwise, return –1. for(int index = 0; index < size; index++){ if(data[index] == value) return index; } return -1; }

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 7 Unordered Linear Search #include Using namespace std; int main() { const int array_size = 8; int list[array_size]={14,2,10,5,1,3,17,2}; int search_value; cout << "Enter search value: "; cin >> search_value; cout << search(list,array_size,search_value) << endl; return 0; }

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 8 Ordered Linear Search l Search an ordered array of integers for a value and return its index if the value is found; Otherwise, return -1. A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] l Linear search can stop immediately when it has passed the possible position of the search value

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 9 Ordered Linear Search l Algorithm: Start with the first array element (index 0) while(more elements in the array){ if value at current index is greater than value, value not found, return –1; if value found at current index, return index; Try next element (increment index); } value not found, return –1;

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 10 Ordered Linear Search // Searches an ordered array of integers int lsearch(int data[],// input: array int size, // input: array size int value // input: value to find ) { // output: index if found for(int index=0; index<size; index++){ if(data[index] > value) return -1; else if(data[index] == value) return index; } return -1; }

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 11 Ordered Linear Search #include using namespace std; int main() { const int array_size = 8; int list[array_size]={1,2,3,5,7,10,14,17}; int search_value; cout << "Enter search value: "; cin >> search_value; cout << lsearch(list,array_size,search_value) << endl; return 0; }

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 12 Binary Search l Search an ordered array of integers for a value and return its index if the value is found. Otherwise, return -1. A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] l Binary search skips over parts of the array if the search value cannot possibly be there

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 13 Copyright © 2000 by Brooks/Cole Publishing Company A division of International Thomson Publishing Inc. 6

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 14

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 15 Binary Search l Binary search is based on the “divide-and- conquer” strategy which works as follows: n Start by looking at the middle element of the array –1. If the value it holds is lower than the search element, eliminate the first half of the array from further consideration. –2. If the value it holds is higher than the search element, eliminate the second half of the array from further consideration. n Repeat this process until the element is found, or until the entire array has been eliminated.

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 16 Binary Search l Algorithm: Set first and last boundary of array to be searched Repeat the following: Find middle element between first and last boundaries; if (middle element contains the search value) return middle_element position; else if (first >= last ) return –1; else if (value < the value of middle_element) set last to middle_element position – 1; else set first to middle_element position + 1;

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 17 Binary Search // Searches an ordered array of integers int bsearch(int data[], // input: array int size, // input: array size int value // input: value to find ) // output: if found,return index { // otherwise, return -1 int first, middle, last; first = 0; last = size - 1; while (true) { middle = (first + last) / 2; if (data[middle] == value) return middle; else if (first >= last) return -1; else if (value < data[middle]) last = middle - 1; else first = middle + 1; }

COMP102 Prog. Fundamentals: Searching Arrays/ Slide 18 Binary Search int main() { const int array_size = 8; int list[array_size]={1,2,3,5,7,10,14,17}; int search_value; cout << "Enter search value: "; cin >> search_value; cout << bsearch(list,array_size,search_value) << endl; return 0; }

COMP102 Prog. Fundamentals: Searching Arrays/ Slide A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] l 14 ? first mid last A[4] A[5] A[6] A[7] first mid last A[6] A[7] f mid last Example: binary search In this case, (data[middle] == value) return middle;

COMP102 Prog. Fundamentals: Searching Arrays/ Slide A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] l 8 ? first mid last A[4] A[5] A[6] A[7] first mid last A[4] f m l Example: binary search 7 In this case, (first == last) return -1;

COMP102 Prog. Fundamentals: Searching Arrays/ Slide l 4 ? A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] first mid last A[0] A[1] A[2] first mid last A[2] f m l 3 Example: binary search In this case, (first == last) return -1;