You are on page 1of 13

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #38, November 8, 2011

Please switch off your mobile phones.

Announcements
Lab exam in the week of 14th to 18th November End-sem exam is on 25th November, 8:00 AM Copies can be seen on 28th afternoon.

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Best Programmers of Lab 10


Section E1 E3 E5 E7 E9 E11 E13 E15 none Anyesha Ghosh Pratik Pradyot Rath Shivanshu Agrawal G V S Sasank Mouli Ankush Sachdeva Harsh Gupta none Name Section E2 E4 E6 E8 E10 E12 E14 Rajat Goel Raghav Goyal Nikunj Agrawal Prithvi Sharma Samyak Daga Amit Munje Name Harshad Sawhney

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Algorithms
Selection Sort Counting routes in a grid Number of bit-sum primes Next Palindrome

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Analyzing the efficiency of algorithms


Algorithms compared
Sequential Search and Binary Search GCD_fast and GCD_slow Merge Sort and Selection Sort

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Searching
Linear Search Start looking at all elements one by one When you find the element you were looking for, Stop. If you dont find the element, you will stop after looking at all the elements. Binary Search Look at the middle element. If this is the element you were looking for, Stop. If your element is smaller then the middle element, then your search space is the left half. p If your element is bigger than the middle element, then your search space is the right half. Recursively apply the same algorithm to the reduced search space.

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Comparing Searching Algorithms


Searching for an element x in a sorted array of n numbers x It appears that sequential search might be slower, since it is checking all elements, while binary search is leaving out almost half the nodes in every iteration Can we be more formal about it?

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

GCD
// Assume m >= n int GCD_fast (int m, int n) { int rem while (n!= 0) { rem = m % n; m = n; n = rem; } return m; }
Lec-38

int GCD_slow (int m, int n) ( , ) { while (n!= 0) { diff = m n; if (diff >= n) m = diff; else { m = n; n = diff; } } return m; }
7

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Comparing GCD algorithms


It appears that the algorithm based on remainder function may be faster than the algorithm based on subtraction. because it is reducing the numbers faster.

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Sorting
Merge Sort:
To sort an array, divide the array into two parts, sort each part recursively, and then merge the two sorted sub-arrays to get the sorted array.

Selection Sort:
Find the minimum value in the array, swap it with the first position, and repeat these steps for the remainder of the array, starting at the second position and advancing each time.

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Comparing Sorting Algorithms


If you use two algorithms for sorting very large arrays you arrays, may find merge sort completing the sort earlier than selection sort. It may not be obvious, but it appears that in selection sort many comparisons are being repeated in successive iterations, while in merge sort no two numbers are compared twice. This may be the reason for faster operation. Can we do this comparison more formally?
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 10

What is the reason for different running times?


Given that both algorithms for a given problem (Searching, (Searching GCD, Sorting):
Have same input and output Are executed in same environment (PC, Operating system, Compiler)

We need to analyze the number of steps/instructions taken by each algorithm for a problem

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

11

Algorithm design is incomplete until you analyze its running time

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Number of steps?
How many steps / instructions are executed by the following loop:
for (int i = 1; i <= n; i = i + 1) { sum = sum + i ; }

No. of steps =
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13

Number of steps?
How many steps / instructions are executed by the following loop:
for (int i = 1; i <= n; i = i + 1) { sum = sum + i ; }

No. of steps = 1 + 3n + 1
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14

Number of steps?
How many steps / instructions are executed by the following l f ll i loop:
for (int i = 1; i <= n; i = i + 1) { sum = sum + i ; }

For sake of simplicity, we can say: No. of steps = an + b, for some positive constants a, b
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

Number of steps?
How many steps/instructions are executed in the following loop:
for (int n = 1; n <= m; n = n + 1) for (int i = 1; i <= n; i = i + 1) sum = sum + i;

No. f Steps = N of S

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Number of steps?
How many steps/instructions are executed in the following loop:
for (int n = 1; n <= m; n = n + 1) for (int i = 1; i <= n; i = i + 1) sum = sum + i;

No. of Steps = 1 + 2m +

(1 + 3n + 1) = 1 + 4m + 3m(m+1)/2

= 3/2 m^2 + 11/2 m + 1

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

Number of steps?
How many steps/instructions are executed in the following loop:
for (int n = 1; n <= m; n = n + 1) for (int i = 1; i <= n; i = i + 1) sum = sum + i;

For k f i li i F sake of simplicity, we can say that: h No. of Steps = a m^2 + bm + c, for some constants a, b, c

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

18

Number of Steps?
How many instructions are executed
to search a number in an unsorted array storing n numbers to search a number in a sorted array of m numbers to sort n numbers by Selection sort to sort m numbers by Merge Sort

Ob Observation: I i a function of input size i It is f i fi i We shall focus on worst case of instructions executed by an algorithm
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 19

10

Sequential Search on n numbers


For sequential search, you can write a for loop which search iterates n times in the worst case. In each iteration, we execute constant number of instructions. No. of instructions in the worst case:
cn + d, for some constants c, d (for large values of n, we can ignore d, hence, say, cn )
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 20

Binary Search on n numbers


int bin_search (int a[ ], int s, int left, int right) { int mid; while (left < right) { mid = (left + right)/2; if (a [mid] == s) return mid; else if (a [mid] > s) right = mid 1; else left = mid + 1; l l f id 1 } return -1; }
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 21

11

Analysis of Binary Search


In each iteration of while loop
constant number of instructions in the worst case

After each iteration of the while loop,


search domain reduces by a factor of 2 hence, the number of iterations of loop: log n Hence the number of instructions in the worst case: a log n + b, for some constants a, b (again, for large n, we can ignore b, hence a log n)
Lec-38 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 22

Comparison of Search Algorithms


c n versus b log n Irrespective of the values of b and c, for large enough values of n, cn will be larger. Hence, we can say that sequential search takes more time compared to binary search.

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

23

12

Any Questions?

Lec-38

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

24

13

You might also like