Beruflich Dokumente
Kultur Dokumente
Prog. – 1: Find square root of a number. Can we use Divide & Conquer approach for this
problem?
LOGIC:
1. Select a number n (a good first guess would be m/2) < m.
2. if n 2 > m, n <- n-1 and repeat step 2, else goto step 3.
3. if n 2 < m, start adding 0.1 to n.
4. compute n 2 , until n 2 > m.
5. n <- n-0.1; repeat steps 3, 4, 5, using 0.01 instead of 0.1, etc.
Program :
#include <stdio.h>
void main()
{
float n,m,root,num;
n=m/2;
while(n*n>m)
{
n=n-1;
}
while(n*n<m)
{
n=n+0.1;
if(n*n>m)
{
n=n-0.1;
n=n+0.01;
}
}
if(n*n>m)
{
//n=n-0.1;
n=n-0.01;
}
printf("%2f ",n);
printf("\n");
}
INPUT:
N number- 49
OUTPUT:
Square root =7.0000
1|Page
Design and Analysis of Algorithms
LOGIC:
1. If n is even return 2 as the divisor, Else
2. (a) Let r = √ n
(b) initialize divisor d to 3;
(c) while(n%d 0 and d < r) do: d d+2.
(d) If(n%d = 0) then return d; else return 1;
Program :
#include<stdio.h>
void main()
{
int d,r,n;
scanf("%d",&n);
if(n%2==0)
{
printf("2 is ans");
}
else
{
r=n*(n);
d=3;
while((n%d!=0)&&d<r)
{
d=d+2;
}
if(n%d==0)
{
printf("ans is %d",d);
}
else
{
printf("ans is 1");
}
printf("\n");
}
}
INPUT:
N number- 35
OUTPUT:
Smallest divisor =5
2|Page
Design and Analysis of Algorithms
LOGIC:
int main()
{
unsigned long long int i,j;
int *primes;
int z = 1,n;
primes = malloc(sizeof(int) * limit);
Scanf(“%d”,&n);
for (i = 2;i < limit; i++)
primes[i] = 1;
for (i = 2;i < limit; i++)
if (primes[i])
for (j = i;i * j < limit; j++)
primes[i * j] = 0;
printf("\nPrime numbers in range 1 to 100 are: \n");
for (i = 2;i < limit; i++)
if (primes[i])
printf("%d\n", i);
return 0;
}
Program :
#include <stdio.h>
int main()
{
int i, j, start, end;
int isPrime; //isPrime is used as flag variable
3|Page
Design and Analysis of Algorithms
{
/*
* If i is divisible by any number other than 1 and self
* then it is not prime number
*/
if(i%j==0)
{
isPrime = 0;
break;
}
}
return 0;
}
INPUT :
Enter lower limit: 1
Enter upper limit: 100
OUTPUT:
All prime numbers between 1 to 100 are:
1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
4|Page
Design and Analysis of Algorithms
LOGIC:
Recursive :
int main()
{
int pow, num;
long result;
printf("Enter a number: ");
scanf("%d", &num);
printf("Enter it's power: ");
scanf("%d", &pow);
result = power(num, pow);
printf("%d^%d is %ld", num, pow, result);
return 0;
}
long power (int num, int pow)
{
if (pow)
{ return (num * power(num, pow - 1)); }
return 1;
}
Iterative:
int power(int x, unsigned int y)
{
int res = 1; // Initialize result
while (y > 0)
{
// If y is odd, multiply x with result
if (y & 1)
res = res*x;
// n must be even now
y = y>>1; // y = y/2
x = x*x; // Change x to x^2
}
return res;
}
Program:
#include <stdio.h>
int main()
{
int base, exponent, counter, result = 1;
printf("Enter base and exponent \n");
5|Page
Design and Analysis of Algorithms
INPUT:
Enter base and exponent
3
5
OUTPUT :
3^5 = 243
6|Page
Design and Analysis of Algorithms
Code (recursion):
#include<stdio.h>
int multiply(int,int);
int main()
{
int a,b,product;
printf("Enter first integers:");
scanf("%d",&a);
printf("Enter second integers:");
scanf("%d",&b);
product = multiply(a,b);
printf("Multiplication of two integers is %d \n",product);
return 0;
}
INPUT:
Enter first integers:2
Enter second integers:3
OUTPUT:
Multiplication of two integers is 6
7|Page
Design and Analysis of Algorithms
Code (Iterative):
#include<stdio.h>
int multiply(int,int);
int main()
{
int a,b,product;
printf("Enter first integers:");
scanf("%d",&a);
printf("Enter second integers:");
scanf("%d",&b);
product = multiply(a,b);
printf("Multiplication of two integers is %d \n",product);
return 0;
}
INPUT:
Enter first integers:2
Enter second integers:3
OUTPUT:
Multiplication of two integers is 6
8|Page
Design and Analysis of Algorithms
Code (Iterative):
#include <stdio.h>
// Iterative function to find factorial of a number using for loop
unsigned long factorial(int n)
{
unsigned long fact = 1;
int i;
for (i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
int main()
{
int n = 5;
printf("The Factorial of %d is %lu", n, factorial(n));
printf("\n");
return 0;
}
Output :
9|Page
Design and Analysis of Algorithms
Code (recursive) :
#include <stdio.h>
// Recursive function to find factorial of a number
unsigned long factorial(int n)
{
// base case: if n is 0 or 1
if (n < 1)
{
return 1;
}
// use the recurrence relation
return n * factorial(n - 1);
}
int main()
{
int n = 5;
printf("The Factorial of %d is %lu", n, factorial(n));
return 0;
}
Output :
10 | P a g e
Design and Analysis of Algorithms
Prog. - 7: Generate Fibonacci series up to n terms Iterative and recursive algorithms are
possible.
Code (Iterative):
/* Fibonacci series up to n terms Iterative */
#include <stdio.h>
int main()
{
int a, b, c, start, end;
printf("Enter starting term: ");
scanf("%d", &start);
printf("Enter end term: ");
scanf("%d", &end);
// Fibonacci magic initialization
a = 0;
b = 1;
c = 0;
printf("Fibonacci terms: \n");
// Iterate through terms
while(c <= end)
{
// If current term is greater than start term
if(c >= start)
{
printf("%d, ", c);
}
a = b; // Copy n-1 to n-2
b = c; // Copy current to n-1
c = a + b; // New term
}
return 0;
}
Output :
11 | P a g e
Design and Analysis of Algorithms
Code (recursive):
/* Fibonacci series up to n terms recursive */
#include<stdio.h>
long factorial(int);
int main()
{
int n;
long f;
printf("Enter an integer to find factorial\n");
scanf("%d", &n);
if (n < 0)
{
printf("Negative integers are not allowed.\n");
}
else
{
f = factorial(n);
printf("%d! = %ld\n", n, f);
}
return 0;
}
long factorial(int n)
{
if (n == 0)
{
return 1;
}
else
{
return(n * factorial(n-1));
}
}
Output :
12 | P a g e
Design and Analysis of Algorithms
Prog. - 8: Determine product of 2 large integers using multiplication of their digits. For
simplicity, assume both numbers to have same number of digits. This assumption can be
relaxed subsequently.
Code :
#include<stdio.h>
int main()
{
int n,i,j,k,c,m,r,x,t,h,y;
printf("enter number of N:");
scanf("%d",&n);//no of test cases
for(i=0;i<n;i++)
{
char A[10002],B[10002];
int c1=0,c2=0,l;
printf("enter first number:");
scanf("%s",A);
printf("enter second number:");
scanf("%s",B);
printf("Answer:");//sacnning the no.s
for(j=0;A[j]!='\0';j++)
c1++;
for(j=0;B[j]!='\0';j++)
c2++;
l=29999;int a[30002]={0};
for(j=c2-1;j>=0;j--)
{
c=0;
x=l-1;
for(k=c1-1;k>=0;k--)
{
h=(int)B[j]-48;
y=(int)A[k]-48;
r=(h*y)+c;//multiply the last digit of B with all the digits of A.
m=r%10;
r=r/10;c=r;//c is the carry
a[x]=m+a[x];
if(a[x]>9)
{
a[x]=a[x]%10;
a[x-1]=a[x-1]+1;//adding 1 to previous posn of result in case of overflow.since only
maximum 1 can be the 1st digit.
x--;
13 | P a g e
Design and Analysis of Algorithms
}
l--;
a[x]=a[x]+c;
int flag=0;
for(k=0;k<=29998;k++)
{
if(a[k]!=0){
printf("%d",a[k]);
flag=1;
}
else if(a[k]==0 && flag==1)
printf("0");
}
if(flag==0)
printf("0");
printf("\n");
}
return 0;
}
Output :
14 | P a g e
Design and Analysis of Algorithms
Prog. - 9: Binary Search of an ordered array. Iterative and Recursive algorithms are possible.
Code (Iterative):
#include <stdio.h>
// A iterative binary search function. It returns location of x in
// given array arr[l..r] if present, otherwise -1
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r)
{
int m = l + (r-l)/2;
// Check if x is present at mid
if (arr[m] == x)
return m;
// If x greater, ignore left half
if (arr[m] < x)
l = m + 1;
// If x is smaller, ignore right half
else
r = m - 1;
}
// if we reach here, then element was not present
return -1;
}
int main(void)
{
int arr[] = {2, 3, 4, 10, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int x = 10;
int result = binarySearch(arr, 0, n-1, x);
(result == -1)? printf("Element is not present in array")
: printf("Element is present at index %d \n", result);
return 0;
}
Output :
15 | P a g e
Design and Analysis of Algorithms
Code (Recursive):
#include<stdio.h>
#include<stdlib.h>
#define size 10
int main()
{
int num, i, key, position;
int low, high, list[size];
low = 0;
high = num - 1;
printf("\nEnter element to be searched :");
scanf("%d", &key);
position = binsearch(list, key, low, high);
if (position != -1)
{
printf("\n Number present at %d \n", (position + 1));
}
else
{
printf("\n The number is not present in the list \n");
}
return (0);
}
16 | P a g e
Design and Analysis of Algorithms
{
return (mid);
}
else if (x < a[mid])
{
binsearch(a, x, low, mid - 1);
}
else
{
binsearch(a, x, mid + 1, high);
}
}
Output :
Number present at 3
17 | P a g e
Design and Analysis of Algorithms
Prog. - 10: Sort a given sequence of numbers using (a) Bubble Sort, and (b) Merge Sort
Code :
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
18 | P a g e
Design and Analysis of Algorithms
Output :
19 | P a g e
Design and Analysis of Algorithms
Code :
#include<stdio.h>
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("\nSorted array is :");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
20 | P a g e
Design and Analysis of Algorithms
Output :
Enter no of elements:10
Enter array elements:
14
99
35
2
45
28
57
62
78
82
Sorted array is :2 14 28 35 45 57 62 78 82 99
21 | P a g e
Design and Analysis of Algorithms
Code :
# include<stdio.h>
if (i < n)
{
x[i] = u / weight[i];
}
tp = tp + (x[i] * profit[i]);
int main()
{
float weight[20], profit[20], capacity;
22 | P a g e
Design and Analysis of Algorithms
int num, i, j;
float ratio[20], temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
23 | P a g e
Design and Analysis of Algorithms
Output :
24 | P a g e
Design and Analysis of Algorithms
Code :
/* Returns the best obtainable price for a rod of length n and price[] as prices of different pieces */
int cutRod(int price[], int n)
{
int val[n+1];
val[0] = 0;
int i, j;
// Build the table val[] in bottom up manner and return the last entry from the table
for (i = 1; i<=n; i++)
{
int max_val = INT_MIN;
for (j = 0; j < i; j++)
{
max_val = max(max_val, price[j] + val[i-j-1]);
}
val[i] = max_val;
}
return val[n];
}
Output :
25 | P a g e
Design and Analysis of Algorithms
Code :
void matmultiply(void)
{
long int q;
int k;
for(i=n;i>0;i--)
{
for(j=i;j<=n;j++)
{
if(i==j)
m[i][j]=0;
else
{
for(k=i;k<j;k++)
{
q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
if(q<m[i][j])
{
m[i][j]=q;
s[i][j]=k;
}
}
}
}
26 | P a g e
Design and Analysis of Algorithms
}
}
void main()
{
int k;
printf("Enter the no. of elements: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
{
m[i][i]=0;
m[i][j]=INFY;
s[i][j]=0;
}
printf("\nEnter the dimensions: \n");
for(k=0;k<=n;k++)
{
printf("P%d: ",k);
scanf("%d",&p[k]);
}
matmultiply();
printf("\nCost Matrix M:\n");
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
printf("m[%d][%d]: %ld\n",i,j,m[i][j]);
printf("\nMatrix S for k values:\n");
for(i=1;i<=n;i++)
for(j=i;j<=n;j++)
printf("m[%d][%d]: %d\n",i,j,s[i][j]);
i=1,j=n;
Output :
27 | P a g e
Design and Analysis of Algorithms
P1: 5
P2: 6
P3: 7
Cost Matrix M:
m[1][1]: 0
m[1][2]: 120
m[1][3]: 288
m[2][2]: 0
m[2][3]: 210
m[3][3]: 0
MULTIPLICATION SEQUENCE : ( ( A1 A2 ) A3 )
28 | P a g e
Design and Analysis of Algorithms
Code :
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left, *right;
};
29 | P a g e
Design and Analysis of Algorithms
else
{
int lheight = height(node->left);
int rheight = height(node->right);
if (lheight > rheight)
{
return(lheight+1);
}
else
{
return(rheight+1);
}
}
}
struct node* newNode(int data)
{
struct node* node = (struct node*) malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return(node);
}
int main()
{
struct node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
printf("Level Order traversal of binary tree is \n");
printLevelOrder(root);
printf("\n");
return 0;
}
Output:
30 | P a g e
Design and Analysis of Algorithms
Code :
#include<stdio.h>
void DFS(int);
int G[10][10],visited[10],n;
void main()
{
int i,j;
printf("Enter number of vertices:");
scanf("%d",&n);
//read the adjecency matrix
printf("\nEnter adjacency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
//visited is initialized to zero
for(i=0;i<n;i++)
visited[i]=0;
DFS(0);
}
void DFS(int i)
{
int j;
printf("\n%d",i);
visited[i]=1;
for(j=0;j<n;j++)
if(!visited[j]&&G[i][j]==1)
DFS(j);
printf("\n");
}
31 | P a g e
Design and Analysis of Algorithms
Output :
10000100
10000100
10000010
10000010
01100001
00011001
00000110
0
1
5
2
7
6
3
32 | P a g e
Design and Analysis of Algorithms
Code (8-puzzle) :
#include <stdio.h>
int main(void)
{
int i = 0,
j = 0;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
Output :
1 2 3
8 0 4
7 6 5
33 | P a g e
Design and Analysis of Algorithms
Code (16-Puzzle) :
#include<stdio.h>
int m=0,n=4;
int cal(int temp[10][10],int t[10][10])
{
int i,j,m=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(temp[i][j]!=t[i][j])
m++;
}
return m;
}
void main()
{
int p,i,j,n=4,a[10][10],t[10][10],temp[10][10],r[10][10];
int m=0,x=0,y=0,d=1000,dmin=0,l=0;
printf("\nEnter the matrix to be solved,space with zero :\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
34 | P a g e
Design and Analysis of Algorithms
while(!(check(a,t)))
{
l++;
d=1000;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(a[i][j]==0)
{
x=i;
y=j;
}
}
if(x!=0)
{
p=temp[x][y];
temp[x][y]=temp[x-1][y];
temp[x-1][y]=p;
}
m=cal(temp,t);
dmin=l+m;
if(dmin<d)
{
d=dmin;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
r[i][j]=temp[i][j];
}
35 | P a g e
Design and Analysis of Algorithms
temp[i][j]=a[i][j];
if(x!=n-1)
{
p=temp[x][y];
temp[x][y]=temp[x+1][y];
temp[x+1][y]=p;
}
m=cal(temp,t);
dmin=l+m;
if(dmin<d)
{
d=dmin;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
r[i][j]=temp[i][j];
}
36 | P a g e
Design and Analysis of Algorithms
dmin=l+m;
if(dmin<d)
{
d=dmin;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
r[i][j]=temp[i][j];
}
Output :
37 | P a g e
Design and Analysis of Algorithms
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
Entered Matrix is :
1 2 3 4
5 6 0 8
9 10 7 11
13 14 15 12
Target Matrix is :
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 0
38 | P a g e