Vous êtes sur la page 1sur 7

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #40, November 15, 2011

Please switch off your mobile phones.

Announcements
Lab exam this week.
Please reach lab 10 minutes before the scheduled time. Follow all instructions sent by email.

End-sem exam is on 25th November, 8:00 AM Copies can be seen on 28th afternoon.

Lec-40

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

Recap
Algorithms to compare
GCD based on remainder and subtraction Merge sort and selection sort

GCD
Algorithm based on remainder function takes c log n steps Algorithm based on subtraction takes d n steps

Sorting
Selection sort takes c * n * n steps Merge sort takes d * n * log n steps
Lec-40 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 2

Recap: Selection Sort


SelectionSort (int a[ ], int n) { for (int i = 0; i < n; i++) { int j = find index of smallest value between i and (n-1); swap values at i and j; } }

Loop is executed n times Finding smallest value takes c (n-i) + d instructions.


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

Recap: Selection Sort


Total number of instructions
sum of c(n-i) + d, for i from 0 to n-1 c * n * (n+1) / 2 + d * n c1 * n^2 + c2 * n

For large values of n, we can ignore linear term Hence number of instructions are bounded by:
c1 * n * n

Lec-40

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

Recap: Merge Sort


First look at merging two arrays of size n/2 each into a third array of size n
For placing every element in to the third array, there is a constant number of operations
compare an element from two smaller arrays Copy the smaller one to the third array Increment the index of the array from where the element has f yf been copied

Hence number of instructions: cn

Lec-40

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

Recap: Merge Sort


We perform cn instructions at each level of mergers. level mergers There are log n levels or numbers of mergers. Hence the worst case number of instructions are: c n log n g

Lec-40

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

Comparison of Selection Sort and Merge Sort


c1 * n^2 versus c2 * n * log n n 2 For any values of c1 and c2, there is a value of n beyond which, c2 * n * log n will be smaller than c1 * n^2. Hence merge sort is considered faster.

Lec-40

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

Alternate Analysis of Merge Sort


Let T (n) be the number of instructions executed by merge mergesort on n numbers.
T(n) = d, if n = 1 T(n) = c * n + 2T (n/2), otherwise c * n is the number of instructions for merging two arrays. This is known as Recurrence Relation.

Lec-40

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

Alternate Analysis of Merge Sort


Gradually unfold the recurrence:
T(n) = cn + 2 (cn/2 + 2 T (n/4)) = cn + cn + 4 T (n/4) = cn + cn + cn + 8 T (n/8) if n = 2^k, k = log n (base 2) T (n) = cn + cn + + cn (k terms) + 2^k T (n/2^k) = c n log n + n T (1) = c n log n + n * d For large values of n, ignore n * d T(n) = c * n * log n
Lec-40 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9

Analysis of exponential function


How many instructions are executed to compute 2 n. 2^n If implemented by a single for loop with multiplication by 2 in every iteration, then c*n. If implemented smartly as below, then c*log n
pow (2, n) = 1, if n = 0 pow (2, n) = pow (2, n/2) * pow (2, n/2), if n is even pow (2, n) = pow (2, (n-1)/2) * pow (2, (n-1)/2) * 2, if n is odd

Lec-40

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

10

Analysis of Counting Sort


In counting sort we read every number once and do sort, constant number of operations
increase the frequency count of the that number in another array.

Similarly, we do constant number of operations in printing each number Hence, number of instructions executed: c*n
assuming n to be the numbers to be read, sorted, and printed
Lec-40 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

Analysis of Matrix Multiplication


How many instructions are executed when you multiply a n x m matrix with a m x r matrix. The resultant matrix is an n x r matrix. For computing each element of this result matrix, we need to multiply m elements of first matrix (a row) with corresponding elements of second matrix (a column) Al all these m numbers to be added. Also, ll th b t b dd d Hence number of instructions for 1 element of result is c*m Total number of instructions: c*m*n*r
Lec-40 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 12

Any Questions?

Lec-40

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

13

Vous aimerez peut-être aussi