LeC-17-Divide and Conquer - Recursion
LeC-17-Divide and Conquer - Recursion
Programming
CS F111
BITS Pilani `
Dubai Campus
BITS Pilani
Dubai Campus
Normal approach
Normal approach
DAC approach
DAC approach
Merge Sort is also a sorting algorithm. The algorithm divides the array
in two halves, recursively sorts them and finally merges the two sorted
halves.
BITS Pilani, Dubai Campus
Divide and Conquer- Examples
Strassen’s Algorithm is an efficient algorithm to multiply two matrices.
A simple method to multiply two matrices need 3 nested loops and is
O(n^3). Strassen’s algorithm multiplies two matrices in O(n^2.8974)
time.
The case at which the function doesn't recur is called the base case
whereas the instances where the function keeps calling itself to perform a
subtask, is called the recursive case.
This program ends when we've counted to ten, or more precisely, when
count is no longer less than ten. This is a good base case because it means
that if we have an input greater than ten, we'll stop immediately.
BITS Pilani, Dubai Campus
Eg. 1 Recursion- Sum of n natural numbers
#include <stdio.h> int sum(int n)
int sum(int n); {
if (n != 0)
int main() // sum() function calls itself
{ return n + sum(n-1);
int number, result;
else
printf("Enter a positive integer: "); return n;
scanf("%d", &number); }