Professional Documents
Culture Documents
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
Lec-38
Recap
Algorithms
Selection Sort Counting routes in a grid Number of bit-sum primes Next Palindrome
Lec-38
Lec-38
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
Lec-38
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
Lec-38
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
We need to analyze the number of steps/instructions taken by each algorithm for a problem
Lec-38
11
Lec-38
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
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
Lec-38
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
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
11
Lec-38
23
12
Any Questions?
Lec-38
24
13