1 Section 2.1 Algorithms. 2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem.

Slides:



Advertisements
Similar presentations
College of Information Technology & Design
Advertisements

College of Information Technology & Design
MATH 224 – Discrete Mathematics
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Chapter 9: Searching, Sorting, and Algorithm Analysis
8 Algorithms Foundations of Computer Science ã Cengage Learning.
CS4413 Divide-and-Conquer
HST 952 Computing for Biomedical Scientists Lecture 9.
CSE115/ENGR160 Discrete Mathematics 02/24/11 Ming-Hsuan Yang UC Merced 1.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
1 Section 3.5 Recursive Algorithms. 2 Sometimes we can reduce solution of problem to solution of same problem with set of smaller input values When such.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
1 © 2006 Pearson Addison-Wesley. All rights reserved Searching and Sorting Linear Search Binary Search -Reading p
The Fundamentals: Algorithms, the Integers & Matrices.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
February 17, 2015Applied Discrete Mathematics Week 3: Algorithms 1 Double Summations Table 2 in 4 th Edition: Section th Edition: Section th.
Chapter 3: The Fundamentals: Algorithms, the Integers, and Matrices
Discrete Structures Lecture 11: Algorithms Miss, Yanyan,Ji United International College Thanks to Professor Michael Hvidsten.
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.
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.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
ดร.สุรศักดิ์ มังสิงห์ SPU, Computer Science Dept.
1 Searching and Sorting Linear Search Binary Search.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 9 Searching Arrays.
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.
MCA-2012Data Structure1 Algorithms Rizwan Rehman CCS, DU.
CSC 211 Data Structures Lecture 13
1 Algorithms CS/APMA 202 Rosen section 2.1 Aaron Bloomfield.
Data Structure Introduction.
Chapter Algorithms 3.2 The Growth of Functions 3.3 Complexity of Algorithms 3.4 The Integers and Division 3.5 Primes and Greatest Common Divisors.
Section 3.1. Section Summary Properties of Algorithms Algorithms for Searching and Sorting Greedy Algorithms Halting Problem.
Chapter 12. Recursion Basics of Recursion Programming with Recursion Computer Programming with JAVA.
1 Algorithms CS 202 Epp section ??? Aaron Bloomfield.
CS 103 Discrete Structures Lecture 12
Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):
Computer Science 101 Fast Algorithms. What Is Really Fast? n O(log 2 n) O(n) O(n 2 )O(2 n )
ALGORITHMS.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 8 Algorithms.
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
CS212: DATASTRUCTURES Lecture 3: Searching 1. Lecture Contents  searching  Sequential search algorithm.  Binary search algorithm. 2.
1 Discrete Structures – CNS2300 Text Discrete Mathematics and Its Applications Kenneth H. Rosen (5 th Edition) Chapter 2 The Fundamentals: Algorithms,
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
Flowcharts C++ Lab. Algorithm An informal definition of an algorithm is: a step-by-step method for solving a problem or doing a task. Input data A step-by-step.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
Ch03-Algorithms 1. Algorithms What is an algorithm? An algorithm is a finite set of precise instructions for performing a computation or for solving a.
Chapter 3 Chapter Summary  Algorithms o Example Algorithms searching for an element in a list sorting a list so its elements are in some prescribed.
1 Algorithms Searching and Sorting Algorithm Efficiency.
Discrete Mathematics Chapter 2 The Fundamentals : Algorithms, the Integers, and Matrices. 大葉大學 資訊工程系 黃鈴玲.
Recursive Definitions
CSE15 Discrete Mathematics 03/06/17
Growth of Functions & Algorithms
Applied Discrete Mathematics Week 2: Functions and Sequences
COP 3503 FALL 2012 Shayan Javed Lecture 15
Algorithm Algorithm is a step-by-step procedure or formula or set of instruction for solving a problem Its written in English language or natural language.
Enough Mathematical Appetizers!
Lecture 2: Introduction to Algorithms
Computation.
Algorithms Chapter 3 With Question/Answer Animations
Chapter 8 Arrays Objectives
Applied Discrete Mathematics Week 6: Computation
24 Searching and Sorting.
Click to add Text Computers & Instructions. Computers are given instructions in the form of computer programs that are created through the development.
Chapter 8 Arrays Objectives
CSCE 222 Discrete Structures for Computing
Algorithms.
Discrete Mathematics CS 2610
Presentation transcript:

1 Section 2.1 Algorithms

2 Algorithm A finite set of precise instructions for performing a computation or for solving a problem

3 Properties of an Algorithm Input: values from a specified (finite) set Output: solution to problem for specified set Definiteness: steps precisely defined, unambiguous Correctness: produces correct output for specified input

4 Properties of an Algorithm Finiteness: finite number of steps required to produce output Effectiveness: must be possible to perform each step exactly in finite amount of time Generality: should be applicable to all problems of its form, not just specific inputs

5 Example: Algorithm for finding sum of finite integer sequence 1. Assign 0 to sum 2. While there are integers to examine, add next integer to sum 3. When all integers have been examined, sum holds sum of all values n  a x x=1

6 Properties applied to example Input: sequence of integers Output: sum of sequence values Definiteness: each step spelled out unambiguously Finiteness: terminates when all integers in sequence have been read Effectiveness: each step is finite Generality: works for any finite integer sequence

7 Searching Algorithms Searching problem: finding an element in an ordered list Output is position of element found (0 if element is not present) Linear and Binary Search are examples

8 Linear (Sequential) Search Algorithm: –Examine each item in succession to see if it matches target value –If target found or no elements left to examine, stop search –If target was found, location = found index; otherwise, location = 0 Works whether or not list is ordered; just as efficient (or inefficient) either way

9 Pseudocode for Linear Search LinearSearch (inputs: target, set of N values to search) foundIndex =1 (start at beginning of list) while (N  foundIndex and target  current value) increment foundIndex if (N  foundIndex) then location = foundIndex else location = 0

10 Binary Search Requires elements in list to be sorted Algorithm we’ll look at is different from the one we use in CS2 - this one is not recursive Classic example of divide-and-conquer algorithm

11 Binary Search Works by splitting list in half, then examining the half that might contain the target value –if not found, split and examine again –eventually, set is split down to one element If the one element is the target, set location to index of item; otherwise, location = 0

12 Pseudocode for Binary Search BinarySearch (inputs: target, sorted list of N elements) left = 1 (left endpoint of search interval) right = N (right endpoint) while (left < right) midpoint =  (left + right) / 2  if (target > element at midpoint) then left = midpoint + 1 else right = midpoint if (target = element at left index) then location = left index else location = 0

13 Binary Search Example Given the following ordered set: a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a Find target value: N=11, left=1, right=11; since 1<11, midpoint=6 since 6th element (19) < 23, left = 7 2. Since 7<11 (left < right), midpoint=9 since 9th element (56) > 23, right = 9 3. Since 7(left) < 9 (right), midpoint=8 since 8th element (42) > 23, right = 8 Continued on next slide...

14 Binary Search Example Given the following ordered set: a 1 a 2 a 3 a 4 a 5 a 6 a 7 a 8 a 9 a 10 a Find target value: Since 7(left) < 8(right), midpoint=7 since 7th element (23) = 23, right = 7 5. Since 7(left)=7(right), drop out of loop Since 23 = 23, location = left(7)

15 Section 2.1 Algorithms -ends-