 # Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.

## Presentation on theme: "Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms."— Presentation transcript:

Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms recursion Comparing Algorithms (5.1) Given two algorithms that do the same thing, which is better? Is either good enough? What about special cases hardware differences software differences small data sets

Scott Grissom, copyright 2004 Chapter 5 Slide 2 Runtime Time Working code is not enough Running time is an important issue How to improve How to compare Space is not as important

Scott Grissom, copyright 2004 Chapter 5 Slide 3 Efficiency (5.2) Running Time based on algorithm AND input data Each statement takes constant time Determine total operations int total = 3; total = total + 3 * 6 - 4; What about this? int total = 0; for (int I=0; I<=10; I++) total = total+ I * I * I;

Scott Grissom, copyright 2004 Chapter 5 Slide 4 Data Set Size of input data N? Predict time t = f(n) Using a stopwatch (benchmarking) is not sufficient. What about this? int Total = 0 for (int i=0; i <=N; i++) Total = Total + i * i * i;

Scott Grissom, copyright 2004 Chapter 5 Slide 5 Big O Notation Predicting exact counts is a nuisance Instead, use Big Oh or Order t = O [f(n)] t n o We can round off t = 6n + 14 t = O(n) 6n 4 + 3n 3 + 12n + 6453 2n 2 + 6 6 NlogN + 2n 2 + 13 12,345 The Most Common n 3 n logN n 2 NlogN 1 2 n place in correct order

Scott Grissom, copyright 2004 Chapter 5 Slide 6 Linear Search Start at the beginning and compare with every item until it is found (or not) Code position = -1; for (int I=0; I<array.length; I++) if (target == array[I]) position = I; Consider best case average case worst case Benchmarking see Demo/LinearSearch.java Consider Enhancements Figure 5.4 on page 217

Scott Grissom, copyright 2004 Chapter 5 Slide 7 Binary Search Items must be sorted Guess middle item If target is less than item then look in first half If target is greater than item then look in second half Else, you found it Code position = -1; while (start <= end) middle = (start + end) / 2 if target < array[middle] end = middle - 1 if target > array[middle] start = middle + 1 if target == array[middle] position = middle Benchmarking see Demo/BinarySearch.java Consider Enhancements Figure 5.5 on page 219

Scott Grissom, copyright 2004 Chapter 5 Slide 8 Asymptotic Analysis 5.3 Nested Loops work inside out for (i=0; i< N; i++) for (j=0; j< N; j++) S; What about this? for (int i=1; i<1000; i++) S; And this one? for (int i=1; i<=N; i++) method (i);

Scott Grissom, copyright 2004 Chapter 5 Slide 9 Sorting Place items in correct order (ascending) Be aware of these terms swap comparison pass Many algorithms have been developed Selection Sort Bubble Sort Merge Sort Quick Sort

Scott Grissom, copyright 2004 Chapter 5 Slide 10 Selection Sort For each pass find smallest remaining item place in correct location Repeat N-1 times Benchmark see Demos/SelectionSort.java

Scott Grissom, copyright 2004 Chapter 5 Slide 11 Bubble Sort For each pass swap neighbors out of order Repeat N-1 times Consider Enhancements Figure 5.6 on page 220

Scott Grissom, copyright 2004 Chapter 5 Slide 12 Sorting Analysis Goal of sorting is to remove inversions How many inversions are there worst case? Consider algorithms that compare and swap neighbors. How many inversions are removed with a single swap? Therefore, what is the expected runtime for ALL sorting algorithms that compare and swap neighbors?

Scott Grissom, copyright 2004 Chapter 5 Slide 13 Other Complexity Measures (5.4) Big Omega best case t >= f(n) Big Theta best and worst case t == f(n) Consider Linear Search Binary Search Bubble Sort in book

Scott Grissom, copyright 2004 Chapter 5 Slide 14 Recursion -- 5.5 Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems Book coverage is limited to the analysis of recursive algorithms A recursive definition is one which uses the word or concept being defined in the definition itself All recursive definitions have to have a non-recursive part called the base case If they didn't, there would be no way to terminate the recursive path called infinite recursion

Scott Grissom, copyright 2004 Chapter 5 Slide 15 Recursive Definitions N!, for any positive integer N, is defined to be the product of all integers between 1 and N inclusive This definition can be expressed recursively as: 1! = 1 N! = N * (N-1)! Eventually, the base case of 1! is reached

Scott Grissom, copyright 2004 Chapter 5 Slide 16 Recursive Programming A method in Java can invoke itself a recursive method The code of a recursive method must be structured to handle both the base case and the recursive case Each call to the method sets up a new execution environment, with new parameters and local variables

Scott Grissom, copyright 2004 Chapter 5 Slide 17 Summation Consider the problem of computing the sum of all the numbers between 1 and any positive integer N This problem can be recursively defined as: Summation Solution int Sum (int N){ if (N < = 1) return 1 else return N + sum(N - 1) } Draw the recursive call stack Too much recursion can cause a Stack Overflow error See Demos/Recursion.java

Scott Grissom, copyright 2004 Chapter 5 Slide 18 Practice Recursion mystery (N){ if (N <= 1) print something else mystery (N - 1); Do it again with (N - 2) Do it again with (N / 2) big difference!

Scott Grissom, copyright 2004 Chapter 5 Slide 19 Recursive Binary Search The binary search algorithm can be implemented recursively It has the same general strategy as the iterative version Each recursive call cuts the search space in half Study code in Figure 5.10 Use the substitution method to solve T(n) = T(n/2) + 3

Scott Grissom, copyright 2004 Chapter 5 Slide 20 Group Projects Write code to solve the following problems How many methods are called for each solution with respect to N? Print the values 1 to N 1 2 3 4 5 6 Print the values N to 1 6 5 4 3 2 1 Fibonacci sequence 1 1 2 3 5 8 13 21 fibonacci (10) prints the first ten numbers in the sequence Factorial value = factorial (8); Power result = power (base, exponent)

Scott Grissom, copyright 2004 Chapter 5 Slide 21 Elegant Solutions? Note that just because we can use recursion to solve a problem, doesn't mean we should For instance, we usually would not use recursion to solve the sum of 1 to N problem, because the iterative version is easier to understand However, for some problems, recursion provides an elegant solution, often cleaner than an iterative version

Scott Grissom, copyright 2004 Chapter 5 Slide 22 Towers of Hanoi A classic problem! Three spindles and N disks of different sizes Move all disks from spindle 1 to 3 The world will end when the monks successfully move 64 disks How long will it take?

Scott Grissom, copyright 2004 Chapter 5 Slide 23 Merge Sort The approach: Divide the list into two equal parts Recursively sort both parts Merge the two parts into a single list again The merge process takes O(N) The runtime is O(n log n) T(n) = 2T(n/2) + n Use the substitution method to solve

Scott Grissom, copyright 2004 Chapter 5 Slide 24 Quick Sort The approach of Quick Sort: Select a pivot Divide the list into two lists based on the pivot all items less than the pivot all items greater than or equal to the pivot Recursively sort both lists Piece together the first list, pivot, and second list into the original list It is considered the fastest known sorting algorithm The runtime is O (n log n)

Scott Grissom, copyright 2004 Chapter 5 Slide 25 Summary Given a computer that performs one billion operations per second on input of one million log n would take almost no time n would take.001 seconds n log n would take _______ n 2 would take ________ n 3 would take ________ 2 n would take ________ 2 n - exponential growth is very slow if computer performs 1 bip n = 40 takes 18 minutes n = 50 13 days n = 60 310 years

Download ppt "Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms."

Similar presentations