C Programming Week8
C Programming Week8
fact(n) = n*fact(n-1)
1 1 1 0 1 0
A(m, n) = n +1 if m = 0
fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)
= A(m-1, 1) if n = 0
= A(m-1, A(m, n-1)) otherwise 1 0
fib(1) fib(0)
SD, PSK, NSN, DK, TAG – CS&E, IIT M 3 SD, PSK, NSN, DK, TAG – CS&E, IIT M 4
1
05/10/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M 8
SD, PSK, NSN, DK, TAG – CS&E, IIT M 9 SD, PSK, NSN, DK, TAG – CS&E, IIT M 10
SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12
2
05/10/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 15 SD, PSK, NSN, DK, TAG – CS&E, IIT M 16
3
05/10/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 SD, PSK, NSN, DK, TAG – CS&E, IIT M 20
4
05/10/17
void InsertMax (int array[ ], int index){ • If the ith element is in sorted order (smaller than
int i = index;
int valueAtIndex = array[index]; the sorted set), no shift is done
while(i > 0 && array[i-1] < valueAtIndex) { • The maximum number of shifts is (i-1)
array[i] = array[i-1]; /*shift right*/
i--; • Complexity
}
array[i] = valueAtIndex; – worst case O(i)
} – best case O(1) – constant time
SD, PSK, NSN, DK, TAG – CS&E, IIT M 25 SD, PSK, NSN, DK, TAG – CS&E, IIT M 26
8 7 5 3 2 1 6 4 6 5
SD, PSK, NSN, DK, TAG – CS&E, IIT M 27 SD, PSK, NSN, DK, TAG – CS&E, IIT M 28
5
05/10/17
SD, PSK, NSN, DK, TAG – CS&E, IIT M 31 SD, PSK, NSN, DK, TAG – CS&E, IIT M 32
SD, PSK, NSN, DK, TAG – CS&E, IIT M 33 SD, PSK, NSN, DK, TAG – CS&E, IIT M 34