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.

Slides:



Advertisements
Similar presentations
Zabin Visram Room CS115 CS126 Searching
Advertisements

College of Information Technology & Design
Back to Sorting – More efficient sorting algorithms.
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 Searching Arrays. COMP102 Prog. Fundamentals: Searching Arrays/ Slide 2 Copyright © 2000 by Broks/Cole Publishing Company A division of International.
Ver. 1.0 Session 5 Data Structures and Algorithms Objectives In this session, you will learn to: Sort data by using quick sort Sort data by using merge.
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.
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.
CIS 101: Computer Programming and Problem Solving Lecture 6 Usman Roshan Department of Computer Science NJIT.
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.
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
 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
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.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
CS212: DATASTRUCTURES Lecture 3: Searching 1. Search Algorithms Sequential Search It does not require an ordered list. Binary Search It requires an ordered.
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.
Chapter 8 ARRAYS Continued
Asymptotic Notations Iterative Algorithms and their analysis
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.
CPT: Search/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss searching: its implementation,
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.:
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
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.
SEARCHING (Linear/Binary). Searching Algorithms  method of locating a specific item of information in a larger collection of data.  two popular 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.
P-1 University of Washington Computer Programming I Lecture 15: Linear & Binary Search ©2000 UW CSE.
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.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
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.
CSE 2341 Honors Professor Mark Fontenot Southern Methodist University Note Set 03.
CSCI 130 Array Searching. Methods of searching arrays Linear –sequential - one at the time –set does not have to be ordered Binary –continuously cutting.
CSC 211 Data Structures Lecture 13
Data Structures & Algorithms Week 4. CHAPTER 3: SEARCHING TECHNIQUES 1. LINEAR (SEQUENTIAL) SEARCH 2.BINARY SEARCH 3. COMPLEXITY OF ALGORITHMS.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
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 & 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 & 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 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Concepts of Algorithms CSC-244 Unit 15 & 16 Divide-and-conquer Algorithms ( Binary Search and Merge Sort ) Shahid Iqbal Lone Computer College Qassim University.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
1 11/12/04CS150 Introduction to Computer Science 1 More Arrays.
CS 116 Object Oriented Programming II Lecture 4 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
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
Search Algorithms Sequential Search (Linear Search) Binary Search
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
Presentation transcript:

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 value is found. Otherwise, return -1. * 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

COMP104 Lecture 22 / Slide 3 Unordered Linear Search // Searches an unordered array of integers int search(int data[], // input: array int size, // input: array size int value){ // input: value to find // output: index if found // otherwise return -1 for(int n=0; n<size; n++) if(data[n] == value) return n; return -1; }

COMP104 Lecture 22 / Slide 4 Unordered Linear Search void main() { int A[8] = { 10, 7, 9, 1, 17, 30, 5, 6 }; int x; cout << "Enter search element: "; cin >> x; int index = search(A,8,x); if(index==-1) cout << "Not found!!\n"; else cout << "Found at: " << index << endl; }

COMP104 Lecture 22 / Slide 5 Ordered Linear Search * Search an ordered array of integers for a value and return its index if the value is found. Otherwise, return -1. * The key difference in design is that this array is ordered. * If we perform a linear search, and find that we have already passed where the element might be found, we can quit early.

COMP104 Lecture 22 / Slide 6 Ordered Linear Search * not found … n search for -1 n search for 8 n search for 100

COMP104 Lecture 22 / Slide 7 Ordered Linear Search * Algorithm: Start with the first array element (index 0) WHILE(more elements in the array){ If value at current index is greater than value, then value not found, return -1 If value found at current index, return index Try next element (increment index) } Value not found, return -1

COMP104 Lecture 22 / Slide 8 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 n=0; n<size; n++){ if(data[n] > value) return -1; else if(data[n] == value) return n; } return -1; }

COMP104 Lecture 22 / Slide 9 Ordered Linear Search void main() { int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30 }; int x; cout << "Enter search element: "; cin >> x; int index = lsearch(A,8,x); if(index==-1) cout << "Not found!!\n"; else cout << "Found at: " << index << endl; }

COMP104 Lecture 22 / Slide 10 Binary Search * Search an ordered array of integers for a value and return its index if the value is found. Otherwise, return -1. * Binary search takes advantage of the sorting, to search the array efficiently.

COMP104 Lecture 22 / Slide 11 Binary Search * Binary search is based on a divide-and- conquer strategy which works as follows: n Start by looking at the middle element of the array  1. If the middle element is smaller than the search element (e.g. 17), eliminate the first half.  2. If the middle element is larger than the search element (e.g. 6), eliminate the second half. n Repeat this process until the element is found, or until the entire array is eliminated. upper bound lower bound

COMP104 Lecture 22 / Slide 12 middle 6<10 Binary Search Search for 6 lowerupper middle 6=6

COMP104 Lecture 22 / Slide 13 middle 12>10 Binary Search Searching for 14 lowerupper lower middle 12<14 upper middle

COMP104 Lecture 22 / Slide 14 Binary Search * Algorithm: Set lower and upper bound of array to be searched Repeat the following: Find the middle element between lower and upper bounds IF ( middle element contains the search value ) return middle element position ELSE IF ( lower bound >= upper bound ) // nothing left to search return -1 ELSE IF ( value < the value of middle element ) set upperbound to middle element position - 1 ELSE set lower bound to middle element position + 1

COMP104 Lecture 22 / Slide 15 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: index if found // otherwise return -1 int lower, middle, upper; lower = 0; upper = size - 1; while (true) { middle = (lower + upper) / 2; if (data[middle] == value) return middle; else if (lower >= upper) return -1; else if (value < data[middle]) upper = middle - 1; else lower = middle + 1; }

COMP104 Lecture 22 / Slide 16 Binary Search void main() { int A[8] = { 1, 5, 6, 7, 9, 10, 17, 30 }; int x; cout << "Enter search element: "; cin >> x; cout << bsearch(A,8,x) << endl; int index = bsearch(A,8,x); if(index==-1) cout << "Not found!!\n"; else cout << "Found at: " << index << endl; }