Searching Arrays Linear search Binary search small arrays

Slides:



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

Copyright © 2012 Pearson Education, Inc. 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.
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.
Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.
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.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 1400 March 30, 2007 Chapter 8 Searching and Sorting.
 2003 Prentice Hall, Inc. All rights reserved Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
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.
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.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
1 Search Algorithms Sequential Search (Linear Search) Binary Search Rizwan Rehman Centre for Computer Studies Dibrugarh University.
1 Introduction to Arrays Problem: –Input 5 scores, compute total, average –Input Example –test scores,employees,temperatures.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Chapter 16: Searching, Sorting, and the vector Type.
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.
Problem Solving and 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.
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.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
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.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
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.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
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.
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.
1 Lecture 8 Arrays Part II Sorting Arrays Sorting data  Important computing application  Virtually every organization must sort some data Massive.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
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.
Objectives At the end of the class, students are expected to be able to do the following: Understand the searching technique concept and the purpose of.
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.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
Data Structures Arrays and Lists Part 2 More List Operations.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Arrays Case Study.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Arrays + Functions Outline 6.5Passing Arrays to Functions.
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
Chapter 16: Searching, Sorting, and the vector Type
Searching and Sorting Arrays
MSIS 655 Advanced Business Applications Programming
Standard Version of Starting Out with C++, 4th Edition
Searching and Sorting Arrays
Presentation transcript:

Searching Arrays Linear search Binary search small arrays unsorted arrays Binary search large arrays sorted arrays

Linear Search Algorithm Start at first element of array. Compare value to value (key) for which you are searching Continue with next element of the array until you find a match or reach the last element in the array. Note: On the average you will have to compare the search key with half the elements in the array.

Linear Search Code const int arraySize = 100; int a[arraySize] = {1, 100, 2, 66, 55, 44, 88, 77, 12, 23, 45, 9, 87}; int key = 88; bool found = false; for (int i = 0; i < arraySize; i++) { if (a[i] == key) { cout << “Found it at array subscript “ << i << endl; found = true; break; } if (! found) cout << “Could not find element “ << key << “ in array a” << endl;

Binary Search Algorithm May only be used on a sorted array. Eliminates one half of the elements after each comparison. Locate the middle of the array Compare the value at that location with the search key. If they are equal - done! Otherwise, decide which half of the array contains the search key. Repeat the search on that half of the array and ignore the other half. The search continues until the key is matched or no elements remain to be searched.

Binary Search Example a search key = 19 1 2 3 4 5 6 7 8 9 10 11 12 1 5 1 2 3 4 5 6 7 8 9 10 11 12 1 5 15 19 25 27 29 middle of the array compare a[6] and 19 19 is smaller than 29 so the next search will use the lower half of the array 31 33 45 55 88 100

Binary Search Pass 2 a search key = 19 1 2 3 4 5 1 5 15 1 2 3 4 5 1 5 15 use this as the middle of the array Compare a[2] with 19 15 is smaller than 19 so use the top half for the next pass 19 25 27

Binary Search Pass 3 search key = 19 a 3 4 5 19 use this as the middle of the array Compare a[4] with 19 25 is bigger than 19 so use the bottom half 25 27

Binary Search Pass 4 search key = 19 3 a 19 use this as the middle of the array Compare a[3] with 19 Found!!

Binary Search Example a search key = 18 1 2 3 4 5 6 7 8 9 10 11 12 1 5 1 2 3 4 5 6 7 8 9 10 11 12 1 5 15 19 25 27 29 middle of the array compare a[6] and 18 18 is smaller than 29 so the next search will use the lower half of the array 31 33 45 55 88 100

Binary Search Pass 2 a search key = 18 1 2 3 4 5 1 5 15 1 2 3 4 5 1 5 15 use this as the middle of the array Compare a[2] with 18 15 is smaller than 18 so use the top half for the next pass 19 25 27

Binary Search Pass 3 search key = 18 a 3 4 5 19 use this as the middle of the array Compare a[4] with 18 25 is bigger than 18 so use the bottom half 25 27

Binary Search Pass 4 search key = 18 3 a 19 use this as the middle of the array Compare a[3] with 18 Does not match and no more elements to compare. Not Found!!

const int arraySize = 13; int a[arraySize] = {1, 2, 9, 12, 23, 44, 45, 55, 66, 77, 87, 88, 100 }; int key = 23 , low = 0, middle, high = (arraySize - 1); while (low <= high) { middle = (low + high) / 2; if (key == a[middle]) { cout << “Found element “ << key << “ at index “ << middle << endl; break; } else if (key < a [middle]) high = middle -1; // search low end of array else low = middle + 1; // search high end of array

Efficiency Searching and array of 1024 elements will take at most 10 passes to find a match or determine that the element does not exist in the array. 512, 256, 128, 64, 32, 16, 8, 4, 2, 1 An array of one billion elements takes a maximum of 30 comparisons. The bigger the array the better a binary search is as compared to a linear search

Exercises 1) Write a function prototype for a function that does a bubble sort named BubbleSort. 2) Write a function prototype for a function that does a binary search named BinarySearch. The return value for BinarySearch is of type int. If the function successfully finds the key for which it is searching the return value contains the index in the array of the element that matches the key. If the function does not find the key for which it is searching the return value contains a -1 3) Write a main program that initializes an array with data that is out of order, calls the BubbleSort function to sort it and then calls the BinarySearch function to find an element in the array.