2420 Review Questions Chapter 6.

Slides:



Advertisements
Similar presentations
Analysis of Algorithms II
Advertisements

Introduction to Algorithms Quicksort
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.
Garfield AP Computer Science
Analysis of Algorithms
CSE 373: Data Structures and Algorithms Lecture 5: Math Review/Asymptotic Analysis III 1.
Recursion. Recursion is a powerful technique for thinking about a process It can be used to simulate a loop, or for many other kinds of applications In.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
Big-O and Friends. Formal definition of Big-O A function f(n) is O(g(n)) if there exist positive numbers c and N such that: f(n) = N Example: Let f(n)
Faster Sorting Methods Chapter 9. 2 Chapter Contents Merge Sort Merging Arrays Recursive Merge Sort The Efficiency of Merge Sort Merge Sort in the Java.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Lecture 2: Divide and Conquer I: Merge-Sort and Master Theorem Shang-Hua Teng.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Functional Design and Programming Lecture 4: Sorting.
CS 280 Data Structures Professor John Peterson. Log Complexity int j = n; while (j > 0) { System.out.println(j); j = j / 2; /* Integer division! */ }
Algorithm Analysis CS 201 Fundamental Structures of Computer Science.
Algorithm Efficiency and Sorting
 Last lesson  Arrays for implementing collection classes  Performance analysis (review)  Today  Performance analysis  Logarithm.
Searching1 Searching The truth is out there.... searching2 Serial Search Brute force algorithm: examine each array item sequentially until either: –the.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
DIVIDE & CONQUR ALGORITHMS Often written as first as a recursive algorithm Master’s Theorem: T(n) = aT(n/b) + cn i, for some constant integer i, and constants.
1 Chapter 2 Program Performance – Part 2. 2 Step Counts Instead of accounting for the time spent on chosen operations, the step-count method accounts.
Recursion, Complexity, and Searching and Sorting By Andrew Zeng.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
1 Recursion Algorithm Analysis Standard Algorithms Chapter 7.
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Analysis of Algorithms
2IL50 Data Structures Fall 2015 Lecture 2: Analysis of Algorithms.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Asymptotic Notation (O, Ω, )
Recursion Review: A recursive function calls itself, but with a smaller problem – at least one of the parameters must decrease. The function uses the results.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Modified for use by MSU Dept. of Computer Science.
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.
Chapter 12 Binary Search and QuickSort Fundamentals of Java.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Selection Sort Sorts an array by repeatedly finding the smallest.
Week 61 Introduction to Programming Ms. Knudtzon C Period Tuesday October 12.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
0 Introduction to asymptotic complexity Search algorithms You are responsible for: Weiss, chapter 5, as follows: 5.1 What is algorithmic analysis? 5.2.
Computability Sort homework. Formal definitions of time complexity. Big 0. Homework: Exercises. Searching. Shuffling.
1 Chapter 2 Algorithm Analysis All sections. 2 Complexity Analysis Measures efficiency (time and memory) of algorithms and programs –Can be used for the.
1 Chapter 2 Algorithm Analysis Reading: Chapter 2.
Lecture #3 Analysis of Recursive Algorithms
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Let’s try it one more time!. Allan Technique Programming Recursively 1.Decide that the problem needs a recursive solution. 2.Decide specifically what.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Chapter 2 Algorithm Analysis
CSCI 104 Sorting Algorithms
Chapter 2 Complexity.
COMP 53 – Week Seven Big O Sorting.
Chapter 2 Complexity.
Design and Analysis of Algorithms
Analysis of Algorithms
CS 201 Fundamental Structures of Computer Science
Sorting.
Algorithm Efficiency and Sorting
CS200: Algorithms Analysis
Divide and Conquer Algorithms Part I
Algorithms Dr. Youn-Hee Han April-May 2013
At the end of this session, learner will be able to:
Discrete Mathematics 7th edition, 2009
Divide and Conquer Merge sort and quick sort Binary search
Presentation transcript:

2420 Review Questions Chapter 6

Complexity Finish the sentence: We say a function f(n) is O(g(n)) if __________________ Why is big oh notation used rather than precise timing?

Complexity If you have timing information, how do you tell if your big oh is correct? Give a big oh definition explaining the use of x0 and c. What are the rules in doing math with big oh? Can we add, subtract, divide, multiply?

What is the complexity void doit( int n ) void doit( int n ) { if (n <=1) return; int x = x + i; doit(n/2); } void doit( int n ) { if (n <=1) return; int x = x + i; doit(n/2); }

What is the complexity void doit( int n ) { if (n <=1) return; for (int i=0; i < n; i++) x = x + i; doit(n/2); } void doit( int n ) { if (n <=1) return; for (i=0; i < n; i++) x = x + i; doit(n/2); }

Theorem: Assume T(n) = aT(n/b) + O(nk) is the time for the function. If a > bk, the complexity is O(n logba). If a = bk, the complexity is O(nk log n). If a < bk, the complexity is O(nk). Using the formula approach, explain why mergesort and quicksort have the same complexity.

2420 Review Questions Chapter 8

Recursion Write a recursive method that has one parameter which is an int value called x. The method prints x asterisks, followed by x exclamation points. Do NOT use any loops. Do NOT use any variables other than x.

Implement the following recursive method Implement the following recursive method. Do not use any local variables or loops. The game starts when I give you some bears. You can then give back some bears, but you must follow these rules (where n is the number of bears that you currently have): 1. If n is even, then you may give back exactly n/2 bears. 2. If n is divisible by 3 or 4, then you may multiply the last two digits of n and give back this many bears. (By the way, the last digit of n is n%10, and the next-to-last digit is ((n%100)/10). 3. If n is divisible by 5, then you may give back exactly 42 bears. The goal of the game is to end up with EXACTLY 42 bears. For example, suppose that you start with 250 bears. Then you could make these moves: --Start with 250 bears. --Since 250 is divisible by 5, you may return 42 of the bears, leaving you with 208 bears. --Since 208 is even, you may return half of the bears, leaving you with 104 bears. --Since 104 is even, you may return half of the bears, leaving you with 52 bears. --Since 52 is divisible by 4, you may multiply the last two digits (resulting in 10) and return these 10 bears. This leaves you with 42 bears. --You have reached the goal – so return TRUE; bool canWin(int bearCt)

Write a recursive solution to the following Write a recursive solution to the following. Given n>=0, create a series of integers. The first line contains the number n. The next line is the number 2n. The next line is the number 4n, and so on until you reach a number that is larger than 1000. This list of numbers is then repeated backward until you get back to n. Example output with n = 40: 40 80 160 320 640

Write a recursive method with two int parameters, m and n Write a recursive method with two int parameters, m and n. The precondition requires 0 <= m and m <= n. The method prints a line of m asterisks, then a line of m+1 asterisks, and so on up to a line of n asterisks. Then the same pattern is repeated backward: a line of n asterisks, then n-1, and so on down to n.

2420 Review Questions Chapter 9, Sorting

Why do we have so many different kinds of sorts?

Is a heap sort oblivious? Is a heap sort stable?

For each of the following algorithms, code the pseudo code Bubble Sort Insertion Sort Selection Sort Shell Sort Quicksort Mergesort Heap sort

Quicksort Lots of improvements have been made to quicksort. Explain two of the most important.

Since quicksort is faster than mergesort (yet has the same complexity), why would we ever use mergesort?