Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.

Slides:



Advertisements
Similar presentations
CMPS1371 Introduction to Computing for Engineers SORTING.
Advertisements

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.
Search and Recursion CS221 – 2/23/09. List Search Algorithms Linear Search: Simple search through unsorted data. Time complexity = O(n) Binary Search:
Trees UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
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.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
1 Sorting. 2 Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort a list,
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Searching Chapter Chapter Contents The Problem Searching an Unsorted Array Iterative Sequential Search Recursive Sequential Search Efficiency of.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Analysis of Algorithms COMP171 Fall Analysis of Algorithms / Slide 2 Introduction * What is Algorithm? n a clearly specified set of simple instructions.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Lecture 2 MAS 714 Hartmut Klauck
Numerical Integration UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Debugging UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Time Complexity UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Cell Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
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.
Recursion and Induction UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the.
 2005 Pearson Education, Inc. All rights reserved Searching and Sorting.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
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. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Merge sort, Insertion sort. Sorting I / Slide 2 Sorting * Selection sort (iterative, recursive?) * Bubble sort.
CSC 211 Data Structures Lecture 13
Sorting UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Script Files UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Numerical Differentiation UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under.
Struct Arrays UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Polynomials UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons.
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Array Search & Sort (continues). On the fly questions Array declaration: int[] a, b, c; 1. a is an array of integers, b and c are two integers 2. a, b,
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.
 MergeSort is a sorting algorithm which is more on the advanced end.  It is very fast, but unfortunately uses up a lot of memory due to the recursions.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Algorithms CSCI 235, Fall 2015 Lecture 12 Elementary Sorts II
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.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Root Finding UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Loop Invariants and Binary Search Chapter 4.4, 5.1.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Get/Set Methods UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Introduction to CMex E177 April 25, Copyright 2005, Andy Packard. This work is licensed under the Creative.
Searching CS 110: Data Structures and Algorithms First Semester,
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
BINARY SEARCH CS16: Introduction to Data Structures & Algorithms Thursday February 12,
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
ALGORITHMS PROVING ALGORITHMS (PROGRAMS) CORRECT WITH AND WITHOUT INDUCTION.
Fundamentals of Algorithms MCS - 2 Lecture # 11
16 Searching and Sorting.
Recursion Version 1.0.
Subject Name: Design and Analysis of Algorithm Subject Code: 10CS43
Recitation 13 Searching and Sorting.
Searching an Array: Binary Search
For/Switch/While/Try UC Berkeley Fall 2004, E77 me
QuickSort QuickSort Best, Worst Average Cases K-th Ordered Statistic
Chapter 14 Recursion. Chapter 14 Recursion Overview 14.1 Recursive Functions for Tasks 14.2 Recursive Functions for Values 14.3 Thinking Recursively.
Unit-2 Divide and Conquer
Searching: linear & binary
CSE 373 Data Structures and Algorithms
Function Handles UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Application: Efficiency of Algorithms II
Application: Efficiency of Algorithms II
Presentation transcript:

Searching UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative Commons Attribution-ShareAlike License. To view a copy of this license, visit or send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.

Searching Finding a specific entry in a 1-dimensional (eg, column vector, row vector) object. Brute force approach, scan (potentially) the entire list. function Idx = bfsearch(A,Key) N = length(A); Idx=1; while Idx<=N & A(Idx)~=Key Idx = Idx + 1; end if Idx==N+1 Idx = []; % no match found end Loop exits if Idx>N or A(Idx)==Key Match found Entire list scanned, no match found

Matlab logical conditionals The Matlab logical conditionals ( ==, ~=, >, >=, <, <= ) all do this form of searching, but –actually check every entry, and –return an vector of 0s and 1s reflecting the outcome They are implemented at “low level” and are essentially as fast as the brute force can be. PseudoCode looks like: function Idx = ltsearch(A,Key) szA = size(A); Idx = zeros(szA); for i=1:prod(szA) if A(i)<Key Idx(i) = 1; end If the number of elements in A doubles, we expect program will take about twice as long to run.

Matlab find The Matlab command find looks for all nonzero entries. Again, brute force, but implemented at “low level” and essentially as fast as the brute force can be. PseudoCode looks like: function Idx = find(A) szA = size(A); Idx = zeros(prod(szA),1); Cnt = 0; for i=1:prod(szA) if A(i)~=0 cnt = cnt + 1; Idx(cnt) = i; end End Idx = Idx(1:cnt); If the number of elements in A doubles, we expect program will take about twice as long to run.

Searching in a sorted list If the list is sorted, it should be easier to search, since checking for a match at any location in the list –finds the match, or (more likely) –Splits the list (at that location) into two lists, one to the “left” of the location, and one to the “right” Since the list is sorted, we immediately know which of the two sublists the match must belong to. Take a sorted list A, of length N. Match must be over here

Searching in a sorted list Take a sorted list A, of length N. Choose k in the “middle”, halving the size of the relevant list each time, until a match is found. Looks a lot like Bisection for root finding Match must be over here

Simple Binary Search Example Lets do an example with A as below, and Key = 0.1; L = 1; R = length(A); while R>L M = floor((L+R)/2); if A(M)<Key L = M+1; else R = M; end

Matlab code for Binary Search function Idx = bsearch(A,Key) Left = 1; Right = length(A); while Right>Left Mid = floor((Left+Right)/2); if A(Mid)<Key Left = Mid+1; else Right = Mid; end if A(Left)==Key Idx = Left; else Idx = []; % no match found end If Right-Left==1, then Mid equals Left, and subsequently Left==Right, leading to the correct final case If Right-Left>1, then Mid is between them, and subsequent value of Right-Left is reduced (essentially halved).

Operation Count for Binary Search Let R(n) denote the number of operations it takes to search for a key in a sorted list of length n. Clearly, if n=1, then R(n)=0, as there is nothing to do. Also, it only takes one comparison to split the list length in half (since it is sorted!), so R(n) = R(n/2) + 1. We can then prove that R(n) ≤ log 2 (n)

Rough Operation Count for BSearch The recursive relation for R R(1)=0, R(n) = R(n/2) + 1 Claim: For n=2 m, it is true that R(n) ≤ log 2 (n) Case (m=0): true, since log 2 (1)=0 Case (derive m=k+1 case from m=k) Recursive relation Induction hypothesis

Matlab code for Binary Search (Recursive) function Idx = bsearch(A,Key,Left,Right) if Left==Right if A(Left)==Key Idx = Left; else Idx = []; % no match found end else Mid = floor((Left+Right)/2); if A(Mid) < Key % match must be beyond Mid Idx = bsearch(A,Key,Mid+1,Right); else Idx = bsearch(A,Key,Left,Mid); end If Right-Left==1, then Mid equals Left, and both recursive calls have Left==Right, leading to the correct base case If Right-Left>1, then Mid is between them, both recursive calls involve “ smaller ” intervals