Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
5 views

C Programming Week8

The document provides an overview of recursive programming concepts, including factorial and Fibonacci functions, as well as tree recursion. It discusses various algorithms such as selection sort and insertion sort, highlighting their complexities and implementations. Additionally, it touches on mathematical sequences and their historical context, particularly focusing on Fibonacci's contributions to mathematics.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C Programming Week8

The document provides an overview of recursive programming concepts, including factorial and Fibonacci functions, as well as tree recursion. It discusses various algorithms such as selection sort and insertion sort, highlighting their complexities and implementations. Additionally, it touches on mathematical sequences and their historical context, particularly focusing on Fibonacci's contributions to mathematics.

Uploaded by

ABHISHEK GOUTAM
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

05/10/17

Factorial (n) – Recursive Program

fact(n) = n*fact(n-1)

CS1100 int fact (int n){


if (n == 1) return 1;
Introduction to Programming return n*fact(n-1);

Recursion and Sorting • Shorter, simpler to understand


Madhu Mutyam • Uses fewer variables
Department of Computer Science and Engineering
Indian Institute of Technology Madras • Machine has to do more work running this one!
Course Material – SD, SB, PSK, NSN, DK, TAG – CS&E, IIT M 1 2
SD, PSK, NSN, DK, TAG – CS&E, IIT M

Tree Recursion Fibonacci Numbers


• When the recursive call is made more than once int fib(int n) { /* n >= 0 */
inside the function. For example, if (n == 0) return 0;
• Fibonacci numbers 5 if (n == 1) return 1;
– fib(n) = fib(n-1) + fib(n-2) if n > 1
fib(5) return fib(n - 1) + fib(n - 2);
3 2
=n if n is 0 or 1 fib(4) fib(3)
• Ackerman’s function 2 1 1 1
– One of the fastest growing functions fib(3) fib(2) fib(2) fib(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

Fibonacci Numbers – Linear Recursion Who Was Fibonacci?


fib(5) • The "greatest European mathematician of
int fib(int n) the middle ages"
{ return fib_aux( n, 1, 0) } fib_aux(5, 1, 0) • His full name was Leonardo of Pisa
int fib_aux(int n, int next, int result) { • Born in Pisa (Italy), about 1175 AD
if (n == 0) return result; fib_aux(4, 1, 1)
• Was one of the first people to introduce the Hindu-Arabic
return fib_aux(n - 1, next + result, next); }
fib_aux(3, 2, 1)
number system into Europe
– “These are the nine figures of the Indians: 9 8 7 6 5 4 3 2 1. With
Computation being done in
the recursive call fib_aux(2, 3, 2) these nine figures, and with this sign 0 which in Arabic is called
zephirum, any number can be written.”
f: 0,1, 1, 2, 3, 5, 8 fib_aux(1, 5, 3) – Part 1 of his book Liber abaci

n: 0,1, 2, 3, 4, 5, 6 • Best known for a simple series of numbers, later named


fib_aux(0, 8, 5)
5
the Fibonacci numbers in his honour. 6
SD, PSK, NSN, DK, TAG – CS&E, IIT M SD, PSK, NSN, DK, TAG – CS&E, IIT M

1
05/10/17

Fibonacci Numbers Rabbit Pairs


• The series begins with 0 and 1. After that, use the simple • How many pairs will there be in one year?
rule: 1. At the end of the first month, they mate, but there is still one only 1 pair.
• Add the last two numbers to get the next 2. At the end of the second month the female produces a new pair, so now
there are 2 pairs of rabbits in the field.
• 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987,...
3. At the end of the third month, the original female produces a second pair,
• Suppose a newly-born pair of rabbits, one male, one making 3 pairs in all in the field.
female, are put in a field. Rabbits are able to mate at the 4. At the end of the fourth month, the original female has produced yet
age of one month so that at the end of its second month a another new pair, the female born two months ago produces her first pair
female can produce another pair of rabbits. Suppose also, making 5 pairs.
that our rabbits never die and that the female always • In general, imagine that there are xn pairs of rabbits after n
produces one new pair (one male, one female) every months. The number of pairs in month n+1 will be xn (in this
month from the second month on. The puzzle that problem, rabbits never die) plus the number of new pairs born.
Fibonacci posed was... But new pairs are only born to pairs at least 1 month old, so there
How many pairs will there be in one year? will be xn-1 new pairs.
• xn+1 = xn + xn-1

SD, PSK, NSN, DK, TAG – CS&E, IIT M 7 SD, PSK, NSN, DK, TAG – CS&E, IIT M 8

Rabbit Pairs An Erratic Sequence


• In Godel, Escher, Bach: An Eternal Golden Braid, D. R.
Hofstadter introduces several recurrences which give rise to
particularly intriguing integer sequences.
• Hofstadter's Q sequence (also known as Meta-Fibonacci
sequence)
• Q(1) = Q(2) = 1
• Q(n) = Q(n - Q(n - 1)) + Q(n - Q(n - 2)) for n > 2
• Each term of the sequence is the sum of two preceding terms, but
(in contrast to the Fibonacci sequence) not necessarily the two last
terms.
• The sequence Q(n) shows an erratic behaviour
• 1, 1, 2, 3, 4, 5, 5, 6, 6, 6, 8, 8, 8, 10, 9, 10, …
– gets more and more erratic

SD, PSK, NSN, DK, TAG – CS&E, IIT M 9 SD, PSK, NSN, DK, TAG – CS&E, IIT M 10

Which is the Biggest? Highest Marks


• Given three numbers a, b, and c, find the biggest • Given an array marks[100], find the highest
amongst them. Define a variable max to store the marks in the class.
value.
max = marks[0]; /* for the time being */
if (a > b && a > c) for (i=1; i<100; i++)
max = a; if (marks[i] > max)
else if (b > c) • Other similar code also works max = marks[i]; /* update if bigger */
max = b; • Method works for array
else elements as well A(1), A(2), A(3)
max = c; • But what if the array is large?
This approach is not feasible

SD, PSK, NSN, DK, TAG – CS&E, IIT M 11 SD, PSK, NSN, DK, TAG – CS&E, IIT M 12

2
05/10/17

More Statistics Exchanging Values


• Given an array marks[100], find the highest, • Exchange the values of variables (a and b)
lowest and average marks in the class.
{ a = b; INCORRECT
max = marks[0]; /* for the time being */ b = a;
min = marks[0]; }
sum = marks[0]; Value of a is lost!
for (i=1; i<100; i++){
if (marks[i] > max) max = marks[i]; Need to use a temporary variable
if (marks[i] < min) min = marks[i]; { temp = a; /* save the value of a */
sum += marks[i]; a = b;
} b = temp;
average = sum/100; /*assuming floating point*/ }
SD, PSK, NSN, DK, TAG – CS&E, IIT M 13 SD, PSK, NSN, DK, TAG – CS&E, IIT M 14

Exchanging Values without temp Swap Array Elements


• What about the following method that does not
use an extra variable? void swap (int array[ ], int i, int j)
{ {
a = a+b; int temp;
b = a - b; temp = array[i];
a = a - b; array[i] = array[j];
} array[j] = temp;
}
• Exercise: Does it work? What are the limitations?
Do you need to be careful about something?

SD, PSK, NSN, DK, TAG – CS&E, IIT M 15 SD, PSK, NSN, DK, TAG – CS&E, IIT M 16

Where is the Highest Number? Sorting an Array of Numbers


• Given an array of n elements, a starting index i, • Problem: Arrange the marks in decreasing order
find out where the largest element lies beyond starting with the maximum.
and including i. • One approach
int MaxIndex (int array[ ], int start, int arraySize){ – Find the maximum value in marks[0] … marks[99]
int i = start; int index = start;
int max = array[i]; – Remember the index i where it occurred
for ( ; i < arraySize; i++) /* observe null statement */ – Exchange (values of) marks[0] and marks[i]
if (array[i] > max){ – Find the maximum value in marks[1] to marks[99]
max = array[i];
index = i; – exchange marks[1] and marks[i]
} – . . . do this till marks[98]
return index;
}
SD, PSK, NSN, DK, TAG – CS&E, IIT M 17 SD, PSK, NSN, DK, TAG – CS&E, IIT M 18

3
05/10/17

Selection Sort Selection Sort as a Function


swap is an function that
passes array by reference.
for (i=0, i <= n - 2, i++) { void selectSort(int array[ ], int size)
swap (marks, i, MaxIndex(marks, i, n)); {
} int maxIndex, i;
for (i = 0; i <= size – 2; i++)
The last element need not be tested {
or more legibly maxIndex = MaxIndex(array, i, size)
for (i=0, i <= n - 2, i++){ if (maxIndex != i) swap(array, i, maxIndex);
}
int maxIndex = MaxIndex(marks, i, n);
}
if (maxIndex != i) swap(marks, i, maxIndex);
}

SD, PSK, NSN, DK, TAG – CS&E, IIT M 19 SD, PSK, NSN, DK, TAG – CS&E, IIT M 20

An Example Complexity of Selection Sort


0 1 2 3 4 5 6 7 i maxIndex 0 1 2 3 4 5 6 7 8 9
2 1 7 5 8 3 6 4 0 4
n=10
i=0 n-1=9 comparisons
8 1 7 5 2 3 6 4 1 2
i=1 n-2=8 comparisons
i=2 n-3=7 comparisons
8 7 1 5 2 3 6 4 2 6
i=3 n-4=6 comparisons
i=4 n-5=5 comparisons
8 7 6 5 2 3 1 4 3 3
i=5 n-6=4 comparisons
i=6 n-7=3 comparisons
8 7 6 5 2 3 1 4 4 7 i=7 n-8=2 comparisons
i=8 n-9=1 comparison (n-1)*n
8 7 6 5 4 3 1 2 5 5
2
≅ n2/2
SD, PSK, NSN, DK, TAG – CS&E, IIT M 21 SD, PSK, NSN, DK, TAG – CS&E, IIT M 22

Complexity of Selection Sort Insertion Sort


• In each iteration, MaxIndex finds the maximum • Insertion sort also scans the array from left to
element right
• When it looks at the ith element, it has elements
– Complexity of MaxIndex is order n à O(n) up till (i-1) sorted
– Can we do this faster? Yes, by arranging the numbers sorted
in a data structure called a MaxHeap
– MaxHeap can extract max element in O(log(n)) i

– Algorithm Heapsort – complexity O(n log(n))


• Selection sort does (n-1) passes of reducing • It moves the ith element to its correct place by
length (average length n/2) shifting the smaller elements to the right
– Complexity (n-1)*n/2 à O(n2/2) à O(n2)
SD, PSK, NSN, DK, TAG – CS&E, IIT M 23 SD, PSK, NSN, DK, TAG – CS&E, IIT M 24

4
05/10/17

InsertMax Function Complexity of InsertMax


sorted sorted
i i

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

Insertion Sort Function An Example


0 1 2 3 4 5 6 7 i # of comp
void InsertionSort(int array[ ], int size){
2 1 7 5 8 3 6 4 1 1
int i;
for(i = 1; i <= size – 1; i++)
InsertMax(array[ ], i); 2 1 7 5 8 3 6 4 2 2
}
7 2 1 5 8 3 6 4 3 3
• Complexity
– best case O(n) 7 5 2 1 8 3 6 4 4 4
– worst case O(n2/2) = O(n2)
8 7 5 2 1 3 6 4 5 3

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

Selection Vs Insertion Sort Selection Vs Insertion


• Scanning from left to right • Selection sort always does the same number of
computations irrespective of the input array
• Selection sort i • Insertion sort does less work if the elements are
– Swaps the ith element with the largest unsorted element partially sorted
– when the ith element is in place, it does not have to
shift any elements – constant time
• Insertion sort i
• If the input is already sorted, Insertion sort
– Inserts the ith element into its proper place
merely scans the array left to right – confirming
that it is sorted
• On the average, Insertion sort performs better
SD, PSK, NSN, DK, TAG – CS&E, IIT M 29 than
SD, PSK, Selection
NSN, DK, TAG – CS&E, IIT sort
M 30

5
05/10/17

Insertion Sort – Sorted Input Insertion Sort – Reverse Sorted Input

The best case complexity of The worst case complexity of


InsertMax is O(1). InsertMax is O(n).
In each pass, function InsertMax In each pass, function InsertMax has
makes one comparison. to move the element to the leftmost
“Area” ∝ n position.
(n-1 lines of unit length “ Area” ∝ n2
per area) (n-1 lines of average
length per area as
n/2)

SD, PSK, NSN, DK, TAG – CS&E, IIT M 31 SD, PSK, NSN, DK, TAG – CS&E, IIT M 32

Insertion Sort – Random Input Exercise for this Week


• Given an array of strings, called names, and an
The worst case complexity of array of marks, called marks, such that marks[i]
Insertion sort is O(n2). contains the marks of names[i]
In each pass, function InsertMax
has to move the element to the – sort the two lists in decreasing order of marks
leftmost position. – sort the two lists in alphabetic order of names
“Area” < n2/2 • figure out how to compare two names to decide which
comes first.

SD, PSK, NSN, DK, TAG – CS&E, IIT M 33 SD, PSK, NSN, DK, TAG – CS&E, IIT M 34

You might also like