L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: 2 13 5 19 10 7 27 17 1 int a[8] 5191072717113.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

CSE Lecture 3 – Algorithms I
Analysis of Algorithms CS Data Structures Section 2.6.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
HST 952 Computing for Biomedical Scientists Lecture 10.
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Chapter 9: Searching, Sorting, and Algorithm Analysis
HST 952 Computing for Biomedical Scientists Lecture 9.
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.
the fourth iteration of this loop is shown here
FIT Objectives By the end of this lecture, students should: understand iteration over arrays searching and sorting in arrays the differences.
1 CSE1301 Computer Programming Lecture 31: List Processing (Search)
Objectives Learn how to implement the sequential search algorithm Explore how to sort an array using the selection sort algorithm Learn how to implement.
Concept of Basic Time Complexity Problem size (Input size) Time complexity analysis.
Searching Arrays Linear search Binary search small arrays
Elementary Data Structures and Algorithms
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
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.
CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)
Analysis of Algorithms
Complexity of algorithms Algorithms can be classified by the amount of time they need to complete compared to their input size. There is a wide variety:
Data Structures & Algorithms CHAPTER 4 Searching Ms. Manal Al-Asmari.
1 Searching and Sorting Linear Search Binary Search.
Searching CS 105 See Section 14.6 of Horstmann text.
Searching. RHS – SOC 2 Searching A magic trick: –Let a person secretly choose a random number between 1 and 1000 –Announce that you can guess the number.
CSE1301 Computer Programming: Lecture 26 List Processing (Search)
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 م. م علي عبد الكريم حبيب.
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. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
CSC 211 Data Structures Lecture 13
Searching CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University of Wisconsin.
Review 1 Arrays & Strings Array Array Elements Accessing array elements Declaring an array Initializing an array Two-dimensional Array Array of Structure.
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.
Algorithm Analysis Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Sorting.
CAPTER 6 SEARCHING ALGORITHM. WHAT IS SEARCHING Process of finding an item in an array Two ways to find an item By position / By value.
Algorithm Analysis (Big O)
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Algorithm Analysis. What is an algorithm ? A clearly specifiable set of instructions –to solve a problem Given a problem –decide that the algorithm is.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Searching Topics Sequential Search Binary Search.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Chapter 16: Searching, Sorting, and the vector Type.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
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
COP 3503 FALL 2012 Shayan Javed Lecture 15
Introduction to Search Algorithms
Introduction to complexity
Searching an Array: Binary Search
Sorting by Tammy Bailey
Algorithm Analysis CSE 2011 Winter September 2018.
Teach A level Computing: Algorithms and Data Structures
Recursion "To understand recursion, one must first understand recursion." -Stephen Hawking.
searching Concept: Linear search Binary search
Searching and Sorting Arrays
Programming and Data Structure
Searching: linear & binary
Searching CLRS, Sections 9.1 – 9.3.
Programming and Data Structure
Presentation transcript:

L. Grewe

 An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]

 Problem: determine if an element is present in an array  Method: ◦ start at one end ◦ look at each array element until the sought element is found  Also called sequential search 3

isPresent (array, val, arraySize) { set count to 0 for (each element in the array) { if ( this array element is val ) { return true } increment count } return false } 4 int isPresent (int *arr, int val, int N) { int count; for (count=0;count<N;count++) { if (arr[count]==val) { return 1; } return 0; }

 How would you modify the program so that it returns the position of the sought item (i.e., findPosition rather than isPresent )?  How would you indicate “not found”? 5

 Algorithm: a set of instructions describing how to do a task  Program: an implementation of an algorithm 6 The time and space requirements of an algorithm enable us to measure how efficient it is

 Time: elapsed period from start to finish of the execution of an algorithm  Space (memory): amount of storage required by an algorithm  Hardware: physical mechanisms required for executing an algorithm 7

 Use your watch? Use the computer clock?  Not a good idea, because: ◦ What if you run your program on different computers? ◦ Your program may also wait for I/O or other resources ◦ While running a program, a computer performs many other computations ◦ Depends on programming/coding skill 8

 We are interested in the number of steps executed by an algorithm ◦ step  execution of an instruction  The running time of an algorithm is proportional to the number of steps it takes to execute the algorithm 9

 What is the size of the input data? ◦ The size of the array being searched is N  How efficient is this algorithm? ◦ Each time through the loop we perform  2 comparisons  count<N  arr[count]==val  1 increment and 1 assignment  count++  Total: 4 operations 10

 Best case? ◦ Wanted item is at the start of the list  1 (initialization) + 4 operations  Worst case? ◦ Wanted item is not found  1 + 4N + 2 (last increment and test)  Average case? ◦ Average of [Wanted item is in position 1, 2, …, N ] 11

 Can we do any better than linear search?  Example: ◦ How do you find a word in the dictionary, or a number in the phone directory? ◦ Choose a spot, then work out whether you're before or after the entry you're looking for ◦ Assume that the array is sorted and use bisection 12

13 If ( value == middle element ) value is found else if ( value < middle element ) search left-half of list with the same method else search right-half of list with the same method

14 Case 1: val == a[mid] val = 10 low = 0, high = a: lowhigh Binary Search -- Example 1 mid mid = (0 + 8) / 2 = 4 10

15 Case 2: val > a[mid] val = 19 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example a: mid lowhigh new low new low = mid + 1 =

16 Case 3: val < a[mid] val = 7 low = 0, high = 8 mid = (0 + 8) / 2 = 4 Binary Search -- Example a: mid lowhigh new high new high = mid - 1 =

17 val = 7 low = 0, new high = 3 new mid = (0 + 3) / 2 = 1 val>a[new mid] Binary Search -- Example 3 (cont) a: lownew high new mid new low new low = mid + 1 = 2 79

18 7 newest mid newest mid = (2 + 3) / 2 = 2 7 val = 7 new low = 2, new high = 3 Binary Search -- Example 3 (cont) a: new low new high

set low to lower bound of array set high to upper bound of array while ( low ≤ high ) { set mid to half of low + high if (array element in mid is val ) { finish - val is in array } else if ( middle value < val ) { set low to mid + 1 } else { set high to mid - 1 } } finish - val is not in array 19

 What happens if the sought value is not in the list?  How would you modify the code so that it returns the position of the sought item (i.e., findPosition rather than isPresent )? 20

 Linear search can be done on any (sorted or unsorted) list, but it is inefficient  Binary search ◦ requires a list to be sorted ◦ is more efficient  Sorting a list: A future lecture 21