Running Time Exercises

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

Introduction to Computer Science Robert Sedgewick and Kevin Wayne Recursive Factorial Demo pubic class Factorial {
Back to Sorting – More efficient sorting algorithms.
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Monday, 12/9/02, Slide #1 CS 106 Intro to CS 1 Monday, 12/9/02  QUESTIONS??  On HW #5 (Due 5 pm today)  Today:  Recursive functions  Reading: Chapter.
Problem Solving #6: Search & Sort ICS Outline Review of Key Topics Review of Key Topics Problem 1: Recursive Binary Search … Problem 1: Recursive.
Lecture 8 Analysis of Recursive Algorithms King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
CS2420: Lecture 10 Vladimir Kulyukin Computer Science Department Utah State University.
Analysis of Recursive Algorithms
Analysis of Recursive Algorithms What is a recurrence relation? Forming Recurrence Relations Solving Recurrence Relations Analysis Of Recursive Factorial.
Analysis of Algorithms 7/2/2015CS202 - Fundamentals of Computer Science II1.
Prof. Amr Goneid, AUC1 Analysis & Design of Algorithms (CSCE 321) Prof. Amr Goneid Department of Computer Science, AUC Part 5. Recursive Algorithms.
CENG 7071 Recursion. CENG 7072 Recursion Recursion is a technique that solves a problem by solving a smaller problem of the same type. A recursive function.
CS Discrete Mathematical Structures Mehdi Ghayoumi MSB rm 132 Ofc hr: Thur, 9:30-11:30a.
Analysis of Algorithms Spring 2015CS202 - Fundamentals of Computer Science II1.
ALGORITHM ANALYSIS AND DESIGN INTRODUCTION TO ALGORITHMS CS 413 Divide and Conquer Algortihms: Binary search, merge sort.
Time Complexity Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Building Java Programs Chapter 13 Searching reading: 13.3.
Analyzing Complexity of Lists OperationSorted Array Sorted Linked List Unsorted Array Unsorted Linked List Search( L, x ) O(logn) O( n ) O( n ) Insert(
1 Linear and Binary Search Instructor: Mainak Chaudhuri
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Searching Course Lecture Slides 28 May 2010 “ Some things Man was never.
Today’s Material Recursive (Divide & Conquer) Algorithms Design
Recursion Trees1 Recursion is a concept of defining a method that makes a call to itself.
CSC 205 Programming II Lecture 9 More on Recursion.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
RECURSION Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
Chapter 3: Sorting and Searching Algorithms 3.1 Searching Algorithms.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Analysis of Algorithms Spring 2016CS202 - Fundamentals of Computer Science II1.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Recursive. Recursive F(n) = F(n-1) + F(n-2) n! = (n-1)! x n C(m,n) = C(m-1,n-1)+C(m-1,n)......
APPLICATIONS OF RECURSION Copyright © 2006 Pearson Addison-Wesley. All rights reserved
Recursion You may have seen mathematical functions defined using recursion Factorial(x) = 1 if x
Algorithm Analysis 1.
Unit 6 Analysis of Recursive Algorithms
Lecture 25: Searching and Sorting
Recursion CENG 707.
Binary Search.
Pseudo-code 1 Running time of algorithm is O(n)
Analysis of Recursive Algorithms
Analysis of Algorithms
Algorithm Analysis The case of recursion.
Analysis of Algorithms
Introduction to Recursion - What is it? - When is it used? - Examples
Applications of Recursion
CS 3343: Analysis of Algorithms
Recursive Binary Search
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Chapter 18-3 Recursion Dale/Weems.
CSE 1342 Programming Concepts
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Analysis of Algorithms
UNIT – V PART - I Searching By B VENKATESWARLU, CSE Dept.
And now for something completely different . . .
CS148 Introduction to Programming II
Fundamental Structures of Computer Science II
Analysis of Recursive Algorithms
Main() { int fact; fact = Factorial(4); } main fact.
        Linear Search 線性搜尋: char names[100][10];
Analysis of Algorithms
Analysis of Recursive Algorithms
DIVIDE AND CONQUER.
Presentation transcript:

Running Time Exercises

k=1; for (i = 1; i < n; i++) k++;

if (x < 3) { k++; } else { k = 1; for (i = 1; i < n; i++) }

k = 1; for (i = 1; i < n; i++) for (j = 1; j < m; j++) k++;

for (i = 1; i < n * n; i++) k++;

k = 1; for (i = 1; i < n; i++) for (j = i + 1; j < n; j++) k++;

i = 0; while (i < n * n *n) i = i + (2 * n);

i = 1; while (i < n * n) i = i + (n / 4);

for (i = 1; i < n; i = 3 * i) { j = 1; while (j < n) j = j + 1; }

for (i = 1; i < n; i++) { j = 1; while (j < 10000) j = j + 1; }

int. arr = new int[n]; int. i, int *arr = new int[n]; int *i,*j; for (i = arr; i < arr+n; i++) { j = i; while (j != arr) { *j = *i; j--; }

Compare Which one should be chosen? When? for (i = 0; i < n; i++) { j = 0; while (j < 10000) j = j + 1; } for (i = 0; i < n; i++) { j = 0; while (j < n) j = j + 1; } Which one should be chosen? When?

Compare for (i = 0; i < n; i++) { j = 0; while (j < 10000) j = j + 1; } for (i = 0; i < n; i++) { j = 0; while (j < n) j = j + 1; } n T1(n) - Θ(n) T2(n) - Θ(n^2) 1 ~ 10^4 ~ 1 10^2 ~ 10^6 ~ 100*100=10^4 10^4 ~ 10^8 ~ 10^4*10^4=10^8 10^6 ~ 10^10 ~ 10^6*10^6=10^12 10^8 ~ 10^12 ~ 10^8*10^8=10^16

CS202 - Fundamentals of Computer Science II More Problems 5/11/2019 CS202 - Fundamentals of Computer Science II

Recursive Algorithms

Factorial int factorial (int n) { if (n == 0) return 1; return n * factorial(n-1); } T(0) = Θ(1) T(n) = T(n-1) + Θ(1)

Hanoi T(0) = Θ(1) T(n) = 2 T(n-1) + Θ(1) void hanoi (int n, char source, char dest, char spare) { if (n > 0) { hanoi(n-1, source, spare, dest); hanoi(n-1, spare, dest, source); } T(0) = Θ(1) T(n) = 2 T(n-1) + Θ(1)

Binary Search Best Case ? T(1) = Θ(1) T(n) = T(n/2) + Θ(1) int binarySearch (int A[], int x, int low, int high) { if (low > high) return -1; int mid = (low + high) / 2; if (A[mid] == x) { return mid; if (A[mid] > x) return binarySearch(A, x, low, mid-1); return binarySearch(A, x, mid+1, high); } Best Case ? T(1) = Θ(1) T(n) = T(n/2) + Θ(1)

Foo T(1) = Θ(1) T(n) = 2T(n/2) + Θ(1) void foo (int n) { if (n > 1) { foo(n/2); } T(1) = Θ(1) T(n) = 2T(n/2) + Θ(1)

Merge Sort T(1) = Θ(1) T(n) = 2T(n/2) + Θ(n)