COMP108 Algorithmic Foundations Searching

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

CSE Lecture 3 – Algorithms I
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Searching Prudence Wong
Efficiency of Algorithms Csci 107 Lecture 6-7. Topics –Data cleanup algorithms Copy-over, shuffle-left, converging pointers –Efficiency of data cleanup.
Efficiency of Algorithms
VB PROJECT “PROJECT SAMPLES”. For Next Loops Design a VB program that displays in a picture box the first N multiples of an input integer Input 3 exam.
Designing Algorithms Csci 107 Lecture 4. Outline Last time Computing 1+2+…+n Adding 2 n-digit numbers Today: More algorithms Sequential search Variations.
CS0007: Introduction to Computer Programming Array Algorithms.
Match-and-Stop Search Will find FIRST match Use Boolean variable to denote whether a match has been found or not Found initially False If a match is found,
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
Efficiency of Algorithms
Instruction count for statements Methods Examples
Chapter 2: Design of Algorithms
CSE115/ENGR160 Discrete Mathematics 03/10/11
Designing Algorithms Csci 107 Lecture 4.
Searching and Sorting Arrays
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Math – Getting Information from the Graph of a Function 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Maximum-sum contiguous subarray Prudence Wong.
Chapter Complexity of Algorithms –Time Complexity –Understanding the complexity of Algorithms 1.
Chapter 3 Sec 3.3 With Question/Answer Animations 1.
Sequences Jordi Cortadella Department of Computer Science.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 11 Sorting and Searching.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
CSC 211 Data Structures Lecture 13
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Basics Prudence Wong
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Chapter 4: Elementary Number Theory and Methods of Proof 4.8 Application: Algorithms 1 Begin at the beginning…and go on till you come to the end: then.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
Data Structures and Algorithms Searching Algorithms M. B. Fayek CUFE 2006.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Mathematical Induction Prudence Wong
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Algorithm efficiency Prudence Wong
7 - Programming 7J, K, L, M, N, O – Handling Data.
Pseudo-code 1 Running time of algorithm is O(n)
Applied Discrete Mathematics Week 2: Functions and Sequences
COMP108 Algorithmic Foundations Mathematical Induction
Data Searching and Sorting algorithms
COMP108 Algorithmic Foundations Polynomial & Exponential Algorithms
CSE15 Discrete Mathematics 03/13/17
Searching Given a collection and an element (key) to find… Output
COMP108 Algorithmic Foundations Algorithm efficiency
Data Structures and Algorithms
Lecture – 2 on Data structures
PROGRAM CONTROL STRUCTURE
Computation.
Control Structure Senior Lecturer
Linear and Binary Search
Describing algorithms in pseudo code
Computer Science 111 Fundamentals of Computer Programming I
Shortest Path Consider the following weighted undirected graph: 20 10
Algorithm Discovery and Design
Applied Discrete Mathematics Week 6: Computation
Sorting … and Insertion Sort.
Console.WriteLine(“Good luck!”);
Order Statistics Def: Let A be an ordered set containing n elements. The i-th order statistic is the i-th smallest element. Minimum: 1st order statistic.
Programming Concepts and Database
Revision of C++.
Podcast Ch22b Title: Inserting into a Heap
COMP108 Algorithmic Foundations Basics
COMP108 Algorithmic Foundations Searching
Programming Dr. Jalal Kawash.
Lecture 4: Introduction to Code Analysis
Computer Science 101 A Survey of Computer Science
COMPUTING.
COMP108 Algorithmic Foundations Mathematical Induction
Presentation transcript:

COMP108 Algorithmic Foundations Searching Prudence Wong http://www.csc.liv.ac.uk/~pwong/teaching/comp108/201617

Searching Input: n numbers a1, a2, …, an; and a number X Output: determine if X is in the sequence or not Algorithm (Sequential search): From i=1, compare X with ai one by one as long as i <= n. Stop and report "Found!" when X = ai . Repeat and report "Not Found!" when i > n.

Sequential Search 12 34 2 9 7 5 six numbers 7 number X 12 34 2 9 7 5 7 To find 7 12 34 2 9 7 5 six numbers 7 number X 12 34 2 9 7 5 7 12 34 2 9 7 5 7 12 34 2 9 7 5 7 12 34 2 9 7 5 7 found!

Sequential Search (2) To find 10 12 34 2 9 7 5 10 12 34 2 9 7 5 10 12 34 2 9 7 5 10 12 34 2 9 7 5 10 12 34 2 9 7 5 10 12 34 2 9 7 5 10 not found!

Pseudo Code - Ideas i = ? found = ? while i <= ? && found == ? do variable i to step through the array boolean found to indicate whether X is found i = ? found = ? while i <= ? && found == ? do begin /* check whether the i-th entry of the array equals X and set found accordingly */ i = i+1 end if found==true then report "Found!" else report "Not Found!"

Pseudo Code i = 1 found = false while i <= n && found == false do begin if X == a[i] then found = true else i = i+1 end if found==true then report "Found!" else report "Not Found!"

How many comparisons this algorithm requires? Number of comparisons? i = 1 found = false while i<=n && found==false do begin if X == a[i] then found = true else i = i+1 end How many comparisons this algorithm requires? Best case: X is 1st no.  1 comparison Worst case: X is last OR X is not found  n comparisons

Finding maximum / minimum... 2nd max / min…

Finding max from n +ve numbers input: a[1], a[2], ..., a[n] i = 1 M = 0 while (i <= n) do begin if a[i] > M then M = a[i] i = i + 1 end output M What about minimum? Skeleton is the same as before: i = 1 while (i <= n) do begin i = i + 1 end

Finding min from n +ve numbers input: a[1], a[2], ..., a[n] i = 1 M = a[1] while (i <= n) do begin if a[i] < M then M = a[i] i = i + 1 end output M How many comparisons? n

Finding location of minimum input: a[1], a[2], ..., a[n] loc = 1 // location of the min number i = 2 while (i <= n) do begin if (a[i] < a[loc]) then loc = i i = i + 1 end output a[loc] Example a[1..5]={50,30,40,20,10} (@ end of) Iteration loc a[loc] i 1 2 3 4 1 50 2 2 30 3 2 30 4 4 20 5 5 10 6

Finding 1st and 2nd min input: a[1], a[2], ..., a[n] for simplicity, assume function min() and max() input: a[1], a[2], ..., a[n] M1 = min(a[1], a[2]) M2 = max(a[1], a[2]) i = 3 while (i <= n) do begin // how to update M1 & M2? i = i + 1 end output M1, M2 Two variables: M1, M2 M1 M2 a[i] 3 cases:

Finding 1st and 2nd min input: a[1], a[2], ..., a[n] for simplicity, assume function min() and max() input: a[1], a[2], ..., a[n] M1 = min(a[1], a[2]) M2 = max(a[1], a[2]) i = 3 while (i <= n) do begin if (a[i] < M1) then M2 = M1, M1 = a[i] else if (a[i] < M2) then M2 = a[i] i = i + 1 end output M1, M2 M1 M2 a[i]