Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let.

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.
Garfield AP Computer Science
COSC 2006 Data Structures I Recursion III
Chapter 4: Divide and Conquer Master Theorem, Mergesort, Quicksort, Binary Search, Binary Trees The Design and Analysis of Algorithms.
Recurrences. What is a Recurrence Relation? A system of equations giving the value of a function from numbers to numbers in terms of the value of the.
Factorial Recursion stack Binary Search Towers of Hanoi
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L17 (Chapter 23) Algorithm.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Chapter 15 Recursive Algorithms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Analysis of Recursive Algorithms
Complexity Analysis (Part III ) Analysis of Some Sorting Algorithms. Analysis of Recursive Algorithms.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
1 Section 6.1 Recurrence Relations. 2 Recursive definition of a sequence Specify one or more initial terms Specify rule for obtaining subsequent terms.
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
Analysis of Algorithm.
Sorting Chapter 6 Chapter 6 –Insertion Sort 6.1 –Quicksort 6.2 Chapter 5 Chapter 5 –Mergesort 5.2 –Stable Sorts Divide & Conquer.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 23 Algorithm Efficiency.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
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)
CMPE13 Cyrus Bazeghi Chapter 17 Recursion. CMPE13 What is Recursion? A recursive function is one that solves its task by calling itself on smaller pieces.
Order Statistics The ith order statistic in a set of n elements is the ith smallest element The minimum is thus the 1st order statistic The maximum is.
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status.
CS 61B Data Structures and Programming Methodology July 28, 2008 David Sun.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
1 CSE 373 Sorting 3: Merge Sort, Quick Sort reading: Weiss Ch. 7 slides created by Marty Stepp
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.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Recursive Algorithms A recursive algorithm calls itself to do part of its work The “call to itself” must be on a smaller problem than the one originally.
1 Heapsort, Mergesort, and Quicksort Sections 7.5 to 7.7.
E.G.M. PetrakisAlgorithm Analysis1  Algorithms that are equally correct can vary in their utilization of computational resources  time and memory  a.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Lecture # 6 1 Advance Analysis of Algorithms. Divide-and-Conquer Divide the problem into a number of subproblems Similar sub-problems of smaller size.
SORTING ALGORITHMS Christian Jonsson Jonathan Fagerström And implementation.
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Divide and Conquer Prudence Wong
Lecture 2 What is a computational problem? What is an instance of a problem? What is an algorithm? How to guarantee that an algorithm is correct? What.
 2006 Pearson Education, Inc. All rights reserved. 1 Searching and Sorting.
Review of Recursion  a recursive method calls itself  to prevent infinite recursion you need to ensure that: 1. the method reaches a base case 2. each.
Sorting – Lecture 3 More about Merge Sort, Quick Sort.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
Recursion (Part 2).
Chapter 15 Recursive Algorithms
Recursion notes Chapter 8.
Towers of Hanoi Move n (4) disks from pole A to pole C
COMP108 Algorithmic Foundations Divide and Conquer
Divide and Conquer divide and conquer algorithms typically recursively divide a problem into several smaller sub-problems until the sub-problems are.
Recursion &Faster Sorting
Recursion Data Structures.
Recursion.
Chapter 15 Recursive Algorithms Animated Version
Ch. 2: Getting Started.
Presentation transcript:

Informal Analysis of Merge Sort  suppose the running time (the number of operations) of merge sort is a function of the number of elements to sort  let the function be T(n)  merge sort works by splitting the list into two sub-lists (each about half the size of the original list) and sorting the sub-lists  this takes 2 T(n/ 2 ) running time  then the sub-lists are merged  this takes O(n) running time  total running time T(n) = 2 T(n/ 2 ) + O(n) 1

Solving the Recurrence Relation T(n)  2 T(n/ 2 ) + O(n) T(n) approaches...  2 T(n/ 2 ) + n = 2 [ 2 T(n/ 4 ) + n/ 2 ] + n = 4 T(n/ 4 ) + 2 n = 4 [ 2 T(n/ 8 ) + n/ 4 ] + 2 n = 8 T(n/ 8 ) + 3 n = 8 [ 2 T(n/ 16 ) + n/ 8 ] + 3 n = 16 T(n/ 16 ) + 4 n = 2 k T(n/ 2 k ) + kn 2

Solving the Recurrence Relation T(n)= 2 k T(n/ 2 k ) + kn  for a list of length 1 we know T( 1 ) = 1  if we can substitute T(1) into the right-hand side of T(n) we might be able to solve the recurrence  we have T(n/ 2 k ) on the right-hand side, so we need to find some value of k such that n/ 2 k = 1  2 k = n  k = log 2 (n) 3

Solving the Recurrence Relation 4

5

Recursion notes Chapter 8 6

Is Merge Sort Efficient?  consider a simpler (non-recursive) sorting algorithm called insertion sort 7 // to sort an array a[0]..a[n-1] not Java for i = 0 to (n-1) { k = index of smallest element in sub-array a[i]..a[n-1] swap a[i] and a[k] } for i = 0 to (n-1) { not Java for j = (i+1) to (n-1) { if (a[j] < a[i]) { k = j; } tmp = a[i]; a[i] = a[k]; a[k] = tmp; } 1 comparison + 1 assignment 3 assignments find smallest element

8

Comparing Rates of Growth 9 O(n)O(n) O(n logn) O(n2)O(n2)O(2 n ) n

Comments  big O complexity tells you something about the running time of an algorithm as the size of the input, n, approaches infinity  we say that it describes the limiting, or asymptotic, running time of an algorithm  for small values of n it is often the case that a less efficient algorithm (in terms of big O) will run faster than a more efficient one  insertion sort is typically faster than merge sort for short lists of numbers 10

Revisiting the Fibonacci Numbers  the recursive implementation based on the definition of the Fibonacci numbers is inefficient public static int fibonacci(int n) { if (n == 0) { return 0; } else if (n == 1) { return 1; } int f = fibonacci(n - 1) + fibonacci(n - 2); return f; } 11

 how inefficient is it?  let T(n) be the running time to compute the nth Fibonacci number  T( 0 ) = T( 1 ) = 1  T(n) is a recurrence relation 12

T(n)T(n) 13

Solving the Recurrence Relation T(n)> 2 k T(n - 2 k )  we know T( 1 ) = 1  if we can substitute T( 1 ) into the right-hand side of T(n) we might be able to solve the recurrence  we have T(n - 2 k ) so we need to find a value for k such that: n - 2 k = 1  k = n  k = (n – 1 )/ 2 14

Towers of Hanoi  a problem easily solved using recursion  move the stack of n disks from A to C  can move one disk at a time from the top of one stack onto another stack  cannot move a larger disk onto a smaller disk 15 ABC

Towers of Hanoi  legend says that the world will end when a 64 disk version of the puzzle is solved  several appearances in pop culture  Doctor Who  Rise of the Planet of the Apes  Survior: South Pacific 16

Towers of Hanoi  n = 1  move disk from A to C 17 ABC

Towers of Hanoi  n = 1 18 ABC

Towers of Hanoi  n = 2  move disk from A to B 19 ABC

Towers of Hanoi  n = 2  move disk from A to C 20 ABC

Towers of Hanoi  n = 2  move disk from B to C 21 ABC

Towers of Hanoi  n = 2 22 ABC

Towers of Hanoi  n = 3  move disk from A to C 23 ABC

Towers of Hanoi  n = 3  move disk from A to B 24 ABC

Towers of Hanoi  n = 3  move disk from C to B 25 ABC

Towers of Hanoi  n = 3  move disk from A to C 26 ABC

Towers of Hanoi  n = 3  move disk from B to A 27 ABC

Towers of Hanoi  n = 3  move disk from B to C 28 ABC

Towers of Hanoi  n = 3  move disk from A to C 29 ABC

Towers of Hanoi  n = 3 30 ABC

Towers of Hanoi  n = 4  move (n – 1) disks from A to B using C 31 ABC

Towers of Hanoi  n = 4  move disk from A to C 32 ABC

Towers of Hanoi  n = 4  move (n – 1) disks from B to C using A 33 ABC

Towers of Hanoi  n = 4 34 ABC

 base case n = 1 1. move disk from A to C  recursive case 1. move (n – 1 ) disks from A to B 2. move 1 disk from A to C 3. move (n – 1 ) disks from B to C 35

Towers of Hanoi public static void move(int n, String from, String to, String using) { if(n == 1) { System.out.println("move disk from " + from + " to " + to); } else { move(n - 1, from, using, to); move(1, from, to, using); move(n - 1, using, to, from); } 36