Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
CSE Lecture 3 – Algorithms I
HST 952 Computing for Biomedical Scientists Lecture 10.
Fundamentals of Python: From First Programs Through Data Structures
Algorithm Complexity Analysis: Big-O Notation (Chapter 10.4)
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.
Fall 2006CENG 7071 Algorithm Analysis. Fall 2006CENG 7072 Algorithmic Performance There are two aspects of algorithmic performance: Time Instructions.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Standard Template Library Ming.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Complexity Analysis (Part I)
Cmpt-225 Sorting. Fundamental problem in computing science  putting a collection of items in order Often used as part of another algorithm  e.g. sort.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Analysis of Algorithm.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
Introduction to Data Structure, Spring 2007 Slide- 1 California State University, Fresno Introduction to Data Structure Sort, Search, and Running Time.
Main Index Contents 11 Main Index Contents Lecture 4 CSN 331 – Fall 2004 Chapter 15 Fibonacci (again) Dynamic Programming Permutations Eight Queens BacktrackingSummary.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
1 Divide and Conquer Binary Search Mergesort Recurrence Relations CSE Lecture 4 – Algorithms II.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Chapter 8 ARRAYS Continued
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Searching – Linear and Binary Searches. Comparing Algorithms Should we use Program 1 or Program 2? Is Program 1 “fast”? “Fast enough”? P1P2.
Week 2 CS 361: Advanced Data Structures and Algorithms
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Chapter 13 Recursion. Topics Simple Recursion Recursion with a Return Value Recursion with Two Base Cases Binary Search Revisited Animation Using Recursion.
1 Sorting Algorithms (Basic) Search Algorithms BinaryInterpolation Big-O Notation Complexity Sorting, Searching, Recursion Intro to Algorithms Selection.
1 Chapter 24 Developing Efficient Algorithms. 2 Executing Time Suppose two algorithms perform the same task such as search (linear search vs. binary search)
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Chapter 19 Searching, Sorting and Big O
Chapter 12 Recursion, Complexity, and Searching and Sorting
1 Programming with Recursion. 2 Recursive Function Call A recursive call is a function call in which the called function is the same as the one making.
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:
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
ADSA: IntroAlgs/ Advanced Data Structures and Algorithms Objective –introduce algorithm design using basic searching and sorting, and remind.
CSC 211 Data Structures Lecture 13
Main Index Contents 11 Main Index Contents Building a Ruler: drawRuler() Building a Ruler: drawRuler() Merge Algorithm Example (4 slides) Merge Algorithm.
Data Structure Introduction.
ADSA: Generics/ Advanced Data Structures and Algorithms Objective –to describe basic forms of generic classes, interfaces, and methods for searching.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 5 Generic.
Chapter 18: Searching and Sorting Algorithms. Objectives In this chapter, you will: Learn the various search algorithms Implement sequential and binary.
Chapter 8 Search and Sort ©Rick Mercer. Outline Understand how binary search finds elements more quickly than sequential search Sort array elements Implement.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
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.
Introduction to Analysis of Algorithms CS342 S2004.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
1. Searching The basic characteristics of any searching algorithm is that searching should be efficient, it should have less number of computations involved.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Algorithm Complexity Analysis (Chapter 10.4) Dr. Yingwu Zhu.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Algorithm Analysis 1.
Introduction to Search Algorithms
Searching & Sorting "There's nothing hidden in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting.
Algorithm Analysis (for Divide-and-Conquer problems)
Algorithm design and Analysis
CSc 110, Spring 2017 Lecture 39: searching.
CS 201 Fundamental Structures of Computer Science
24 Searching and Sorting.
Data Structures for Java William H. Ford William R. Topp
Recursive Algorithms 1 Building a Ruler: drawRuler()
Presentation transcript:

Main Index Contents 11 Main Index Contents Selection Sort Selection SortSelection Sort Selection Sort (3 slides) Selection Sort Alg. Selection Sort Alg.Selection Sort Alg. Selection Sort Alg. (3 slides) Search Algorithms Search AlgorithmsSearch Algorithms Search Algorithms (6 slides) Illustrating the Binary Search -Successful (3 slides)Successful -Unsuccessful (3 slides)Unsuccessful Binary Search Alg. Binary Search Alg.Binary Search Alg. Binary Search Alg. (3 slides) Big-O Notation Big-O Notation Constant Time Algorithms Constant Time Algorithms Linear Time Algorithms Linear Time Algorithms Exponential Algs. Exponential Algs.Exponential Algs. Exponential Algs. (2 slides) Logarithmic Time Algorithms Logarithmic Time Algorithms Chapter 3 – Introduction to Algorithms Selection Sort Algorithm -Integer VersionInteger Version -String VersionString Version Template Syntax Template SyntaxTemplate Syntax Template Syntax (4 slides) Recursive Def n of the Power Fnc Recursive Def n of the Power Fnc Stopping Conditions for- Stopping Conditions for- Recursive Algorithms Recursive Algorithms Implementing the Recursive- Implementing the Recursive- Power Function Power Function Tower of Hanoi w/ Recursion Tower of Hanoi w/ Recursion (3 slides) Fibonacci Numbers Using- Fibonacci Numbers Using- Iteration Iteration Iteration Iteration (2 slides) Summary Slides Summary SlidesSummary Slides Summary Slides (5 slides)

Main Index Contents 22 Main Index Contents Selection Sort - 5 Element Array Pass 0: Scan the entire list from arr[0] to arr[4] and identify 20 at index 1 as the smallest element. Exchange 20 with arr[0] = 50, the first element in the list.

Main Index Contents 33 Main Index Contents Selection Sort - 5 Element Array Pass 1: Scan the sublist 50, 40, 75, and 35. Exchange the smallest element 35 at index 4 with arr[1] = 50.

Main Index Contents 44 Main Index Contents Selection Sort - 5 Element Array Pass 2: Locate the smallest element in the sublist 40, 75, and 50.

Main Index Contents 55 Main Index Contents Selection Sort - 5 Element Array Pass 3: Two elements remain to be sorted. Scan the sublist 75, 50 and exchange the smaller element with arr[3]. The exchange places 50 at index 4 in arr[3].

Main Index Contents 66 Main Index Contents Selection Sort - 5 Element Array

Main Index Contents 77 Main Index Contents Selection Sort Algorithm void selectionSort(int arr[], int n) { int smallIndex;// index of smallest // element in the sublist int pass, j; int temp; // pass has the range 0 to n-2

Main Index Contents 88 Main Index Contents Selection Sort Algorithm for (pass = 0; pass < n-1; pass++) { // scan the sublist starting at index // pass smallIndex = pass; // j traverses the sublist arr[pass+1] // to arr[n-1] for (j = pass+1; j < n; j++) // if smaller element found, assign // smallIndex to that position

Main Index Contents 99 Main Index Contents Selection Sort Algorithm if (arr[j] < arr[smallIndex]) smallIndex = j; // if smallIndex and pass are not the // same location, exchange the // smallest item in the sublist with // arr[pass] if (smallIndex != pass) { temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; }

Main Index Contents 10 Search Algorithms Search algorithms start with a target value and employ some strategy to visit the elements looking for a match. – If target is found, the index of the matching element becomes the return value.

Main Index Contents 11 Main Index Contents Search Algorithms

Main Index Contents 12 Main Index Contents Search Algorithms - Sequential Search Algorithm int seqSearch(const int arr[], int first, int last, int target) { int i = first; // scan indices in the range first <= I < last; // test for a match or index out of range. while(i != last && arr[i] != target) i++; return i; // i is index of match or i = last if no match }

Main Index Contents 13 Search Algorithms Case 1. A match occurs. The search is complete and mid is the index that locates the target. if (midValue == target) // found match return mid;

Main Index Contents 14 Search Algorithms Case 2. The value of target is less than midvalue and the search must continue in the lower sublist. Reposition the index last to the end of the sublist (last = mid). // search the lower sublist if (target <search sublist arr[first]…arr[mid-1]

Main Index Contents 15 Search Algorithms Case 3. The value of target is greater than midvalue and the search must continue in the upper sublist. Reposition the index first to the front of the sublist (first = mid+1). // search upper sublist if (target > midvalue)

Main Index Contents 16 Main Index Contents Illustrating the Binary Search - Successful Search 1.Search for target = 23 Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4. Since target = 23 > midvalue = 12, step 2 searches the upper sublist with first = 5 and last = 9.

Main Index Contents 17 Main Index Contents Illustrating the Binary Search - Successful Search Step 2: Indices first = 5, last = 9, mid = (5+9)/2 = 7. Since target = 23 < midvalue = 33, step 3 searches the lower sublist with first = 5 and last = 7.

Main Index Contents 18 Main Index Contents Illustrating the Binary Search - Successful Search Step 3:Indices first = 5, last = 7, mid = (5+7)/2 = 6. Since target = midvalue = 23, a match is found at index mid = 6.

Main Index Contents 19 Illustrating the Binary Search - Unsuccessful Search Search for target = 4. Step 1: Indices first = 0, last = 9, mid = (0+9)/2 = 4. Since target = 4 < midvalue = 12, step 2 searches the lower sublist with first = 0 and last = 4.

Main Index Contents 20 Illustrating the Binary Search - Unsuccessful Search Step 2: Indices first = 0, last = 4, mid = (0+4)/2 = 2. Since target = 4 < midvalue = 5, step 3 searches the lower sublist with first = 0 and last 2.

Main Index Contents 21 Illustrating the Binary Search - Unsuccessful Search Step 3: Indices first = 0, last = 2, mid = (0+2)/2 = 1. Since target = 4 > midvalue = 3, step 4 should search the upper sublist with first = 2 and last =2. However, since first >= last, the target is not in the list and we return index last = 9.

Main Index Contents 22 Main Index Contents Binary Search Algorithm Int binSearch(const int arr[], int first, int last, int target) { int mid;// index of the midpoint int midvalue;// object that is // assigned arr[mid] int origLast = last; // save original value of last

Main Index Contents 23 Main Index Contents Binary Search Algorithm while (first < last) // test for nonempty sublist { mid = (first+last)/2; midvalue = arr[mid]; if (target == midvalue) return mid;// have a match // determine which sublist to // search

Main Index Contents 24 Main Index Contents Binary Search Algorithm else if (target < midvalue) last = mid; // search lower sublist. reset last else first = mid+1; // search upper sublist. Reset first } return origLast; // target not found }

Main Index Contents 25 Big-O notation For the selection sort, the number of comparisons is T(n) = n 2 /2 - n/2. Entire expression is called the "Big-O" measure for the algorithm. ** Big-O notation provides a machine independent means for determining the efficiency of an Algorithm. n = 100: T(100) = /2 -100/2 = 10000/ /2 = 5, = 4,950

Main Index Contents 26 Main Index Contents Constant Time Algorithms An algorithm is O(1) when its running time is independent of the number of data items. The algorithm runs in constant time. The storing of the element involves a simple assignment statement and thus has efficiency O(1).

Main Index Contents 27 Linear Time Algorithms An algorithm is O(n) when its running time is proportional to the size of the list. When the number of elements doubles, the number of operations doubles.

Main Index Contents 28 Main Index Contents Exponential Algorithms Algorithms with running time O(n 2 ) are quadratic. – practical only for relatively small values of n. Whenever n doubles, the running time of the algorithm increases by a factor of 4. Algorithms with running time O(n 3 )are cubic. – efficiency is generally poor; doubling the size of n increases the running time eight-fold.

Main Index Contents 29 Main Index Contents Exponential Algorithms

Main Index Contents 30 Logarithmic Time Algorithms The logarithm of n, base 2, is commonly used when analyzing computer algorithms. Ex.log 2 (2) = 1 log 2 (75) = When compared to the functions n and n 2, the function log 2 n grows very slowly.

Main Index Contents 31 Main Index Contents Selection Sort Algorithm Integer Version void selectionSort(int arr[], int n) {... int temp; // int temp used for the exchange for (pass = 0; pass < n-1; pass++) {... if (arr[j] < arr[smallIndex]) // compare integer elements... }

Main Index Contents 32 Main Index Contents Selection Sort Algorithm String Version void selectionSort(string arr[], int n) {... string temp; // double temp used for the exchange for (pass = 0; pass < n-1; pass++) {... if (arr[j] < arr[smallIndex]) // compare string element... } }

Main Index Contents 33 Template Syntax template function syntax includes the keyword template followed by a non-empty list of formal types enclosed in angle brackets. In the argument list, each type is preceded by the keyword typename, and types are separated by commas. // argument list with a multiple template // types template

Main Index Contents 34 Template Syntax Example template void selectionSort(T arr[], int n) { int smallIndex; // index of smallest element in the // sublist int pass, j; T temp;

Main Index Contents 35 Template Syntax Example // pass has the range 0 to n-2 for (pass = 0; pass < n-1; pass++) { // scan the sublist starting at // index pass smallIndex = pass; // j traverses the sublist // a[pass+1] to a[n-1] for (j = pass+1; j < n; j++) // update if smaller element found

Main Index Contents 36 Template Syntax Example if (arr[j] < arr[smallIndex]) smallIndex = j; // if smallIndex and pass are not // the same location, exchange the // smallest item in the sublist with // arr[pass] if (smallIndex != pass) { temp = arr[pass]; arr[pass] = arr[smallIndex]; arr[smallIndex] = temp; } } }

Main Index Contents 37 Main Index Contents Recursive Definition of the Power Function A recursive definition distinguishes between the exponent n = 0 (starting point) and n  1 which assumes we already know the value x n-1. After determining a starting point, each step uses a known power of 2 and doubles it to compute the next result. – Using this process gives us a new definition for the power function, x n. We compute all successive powers of x by multiplying the previous value by x.

Main Index Contents 38 Stopping Conditions for Recursive Algorithms Use a recursive function to implement a recursive algorithm. – The design of a recursive function consists of 1.One or more stopping conditions that can be directly evaluated for certain arguments. 2.One or more recursive steps in which a current value of the function can be computed by repeated calling of the function with arguments that will eventually arrive at a stopping condition.

Main Index Contents 39 Main Index Contents Implementing the Recursive Power Function Recursive power(): double power(double x, int n) // n is a non-negative integer { if (n == 0) return 1.0;// stopping condition else return x * power(x,n-1); // recursive step }

Main Index Contents 40 Main Index Contents Solving the Tower of Hanoi Puzzle using Recursion

Main Index Contents 41 Main Index Contents Solving the Tower of Hanoi Puzzle using Recursion

Main Index Contents 42 Main Index Contents Solving the Tower of Hanoi Puzzle using Recursion

Main Index Contents 43 Fibonacci Numbers using Iteration int fibiter(int n) { // integers to store previous two // Fibonacci value int oneback = 1, twoback = 1, current; int i; // return is immediate for first two numbers if (n == 1 || n == 2) return 1;

Main Index Contents 44 Fibonacci Numbers using Iteration else // compute successive terms beginning at 3 for (i = 3; i <= n; i++) { current = oneback + twoback; twoback = oneback; // update for next calculation oneback = current; } return current; }

Main Index Contents 45 Main Index Contents Summary Slide 1 §- The simplest form of searching is the sequential search. §-It compares the target with every element in a list until matching the target or reaching the end of the list. §- If the list is in sorted order, the binary search algorithm is more efficient. §-It exploits the structure of an ordered list to produce very fast search times.

Main Index Contents 46 Main Index Contents Summary Slide 2 §- Big-O notation measures the efficiency of an algorithm by estimating the number of certain operations that the algorithm must perform. -For searching and sorting algorithms, the operation is data comparison. -Big-O measure is very useful for selecting among competing algorithms.

Main Index Contents 47 Main Index Contents Summary Slide 3 §- The running time of the sequential search is O(n) for the worst and the average cases. §- The worst and average case for the binary search is O(log2n). §- Timing data obtained from a program provides experimental evidence to support the greater efficiency of the binary search.

Main Index Contents 48 Main Index Contents Summary Slide 4 §- C++ provides a template mechanism that allows a programmer to write a single version of a function with general type arguments. -If a main program wants to call the function several times with different runtime arguments, the compiler looks at the types of the runtime arguments and creates different versions of the function that matches the types.

Main Index Contents 49 Main Index Contents Summary Slide 5 §- An algorithm is recursive if it calls itself for smaller problems of its own type. §-Eventually, these problems must lead to one or more stopping conditions. -The solution at a stopping condition leads to the solution of previous problems. -In the implementation of recursion by a C++ function, the function calls itself.