TRAVERSING AN ARRAY My friend, the FOR loop Analyze an array in other ways than max,min,sort… 11.

Slides:



Advertisements
Similar presentations
Searching Algorithms Finding what you are looking for.
Advertisements

HST 952 Computing for Biomedical Scientists Lecture 9.
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.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
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.
Binary Search Visualization i j.
June Searching CE : Fundamental Programming Techniques 16 Searching1.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sorting Arrays Sorting data –Important computing application –Virtually every organization must sort.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
J. Michael Moore Searching & Sorting CSCE 110. J. Michael Moore Searching with Linear Search Many times, it is necessary to search an array to find a.
Searching/Sorting Introduction to Computing Science and Programming I.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching Arrays Linear search Binary search small arrays
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
Search Lesson CS1313 Spring Search Lesson Outline 1.Searching Lesson Outline 2.How to Find a Value in an Array? 3.Linear Search 4.Linear Search.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Data Structures Introduction Phil Tayco Slide version 1.0 Jan 26, 2015.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
General Computer Science for Engineers CISC 106 Lecture 07 James Atlas Computer and Information Sciences 06/29/2009.
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.
Computer Science 101 Fast Searching and Sorting. Improving Efficiency We got a better best case by tweaking the selection sort and the bubble sort We.
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.
Searching Given a collection and an element (key) to find… Output –Print a message (“Found”, “Not Found) –Return a value (position of key ) Don’t modify.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
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.
SEARCHING. Vocabulary List A collection of heterogeneous data (values can be different types) Dynamic in size Array A collection of homogenous data (values.
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.
L. Grewe.  An array ◦ stores several elements of the same type ◦ can be thought of as a list of elements: int a[8]
Traversing an array 1.Definition 2.Use the for loop 3.Some new functions 4.Use the while loop 1.
1 Chapter 3 Arrays (2) 1. Array Referencing 2. Common Operations 1. Slicing 2. Diminution 3. Augmentation 3. List of Commonly Used Built-in Functions 1.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
CSC 211 Data Structures Lecture 13
Data Structure Introduction.
CS261 Data Structures Ordered Bag Dynamic Array Implementation.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
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):
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.
Course Code #IDCGRF001-A 5.1: Searching and sorting concepts Programming Techniques.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
 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.
CMPT 120 Topic: Searching – Part 2 and Intro to Time Complexity (Algorithm Analysis)
Algorithm Analysis with Big Oh ©Rick Mercer. Two Searching Algorithms  Objectives  Analyze the efficiency of algorithms  Analyze two classic algorithms.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Searching/Sorting. Searching Searching is the problem of Looking up a specific item within a collection of items. Searching is the problem of Looking.
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.
CMPT 120 Topic: Searching – Part 2
Searching Given a collection and an element (key) to find… Output
Week 8 - Programming II Today – more features: Loop control
Searching CSCE 121 J. Michael Moore.
Linear and Binary Search
Adapted from Pearson Education, Inc.
Algorithm design and Analysis
Searching and Sorting Arrays
Searching: linear & binary
Topic 1: Problem Solving
CSCE 222 Discrete Structures for Computing
Searching.
Presentation transcript:

TRAVERSING AN ARRAY My friend, the FOR loop Analyze an array in other ways than max,min,sort… 11

Linear Searching “Linear searching” is exactly how we typically work a search – we look at each element and see if it matches. We stop when we reach the end of the array. For example: Our program has generated a matrix. We would like to know if any value in the matrix exceeds the value of 10.

Linear Searching, cont. s = size(M); found = []; for r = 1:s(1) for c = 1:s(2) if M(r, c) > 10 found(1) = r; found(2) = c; end size() gives us # of rows and # of columns found will contain the position of the last value found that is greater than 10

Binary Searching Binary searching is a fancy name for how we use the phone book – an extremely fast way to search. Since names are sorted in the phone book, we can skip massive portions. For example, if the last name starts with “T”, we can skip about 75% of the phonebook!

Binary Searching, cont. Just like with the phonebook, we can search arrays using binary searching. And like the phonebook, this method requires that the information already is sorted. Although it can be modified for matrices, it is most useful when searching vectors.

Binary Searching, cont. To use binary searching: Sort the vector Repeat until found or run out of values to check Compute the ½ way index for the current range Is it what you want? If not, is it too high or too low? If too high, move to middle of lower range If too low, move to middle of upper range

Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end

Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Repeat until found or run out of values to check

Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Compute the ½ way index for the current range

Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Is it what you want?

Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end If too high, move to middle of lower range

Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end If too low, move to middle of upper range

Binary Searching, cont. clear clc % Give us some values v = floor(25*rand(1, 10)) % Search for the value 10 value = 10; % Sort the data v = sort(v) % Setup the vars hi = 10; lo = 1; done = false; found = false; while (~done && ~found) range = hi - lo + 1; index = floor(range / 2) + lo; if (v(index) == value) found = true; elseif (v(index) > value) hi = index-1; else lo = index+1; end if hi<lo done = true; end Have we used up all the values?

Wrapping Up Linear Searching implies looking at each element. The for loop is the most appropriate Binary Searching (yellow-pages) zooms in faster on the element to find. DATA MUST BE PRE-SORTED. The while loop is the most appropriate 14