c programs with index
TRANSCRIPT
-
8/6/2019 C Programs With Index
1/51
Design and Analysis of Algorithms
Index
S. No. Contents Page No.
1. Bubble Sort Implementation 2
2. Selection Sort Implementation 3
3. Insertion Sort Implementation 5
4. Quick Sort Implementation 6
5. Merge Sort Implementation 8
6. Heap Sort Implementation 10
7. Comparison Counting Sort Implementation 12
8. Sequential Search Implementation 14
9. Quick Sequential Search Implementation 16
10. Binary Search Implementation 18
11. Brute Force Pattern Matching Technique 20
12. Brute Force Technique for finding closest points 21
13. Matrix Multiplication 23
14. Matrix Chain Multiplication 25
15. Coin Problem using Greedy Strategy 26
16. Coin Problem using Dynamic Programming 28
17. Knap Sack Problem 30
18. Prim Algorithm Implementation 31
19. Breadth First Search Implementation 35
20. N-Queen problem using Backtracking 37
21. Subset Problem Using Backtracking39
22. N-Queen Problem using FIFO Branch & Bound 41
23. Calculating Factorial 44
24. Exponential xn 45
25. Euclid Algorithm Implementation 46
26. Fibonacci numbers using Dynamic Programming 47
27. Sub Set Problem using FIFO Branch and Bound Technique 48
]
****************************
SCS DAVV, INDORE 1
-
8/6/2019 C Programs With Index
2/51
Design and Analysis of Algorithms
1. Program for Bubble Sort Implementation
#include#include
#include
void BubbleSort(int *a,int n);int n;
int *a;
void readData();void display();
int main()
{
clrscr();readData();
BubbleSort(a,n);
display();
free(a);getch();
return 0;}
void readData()
{
coutn;
a=(int*)malloc(sizeof(int)*(n));
cout
-
8/6/2019 C Programs With Index
3/51
Design and Analysis of Algorithms
2. Program for Selection Sort Implementation
#include#include
#include
void selectionSort(int *a);int n;
int *a;
void readData();void display();
int main()
{
clrscr();
readData();
selectionSort(a);
display();free(a);
getch();
return 0;
}
void readData()
{
coutn;
a=(int*)malloc(sizeof(int)*(n+1));
cout
-
8/6/2019 C Programs With Index
4/51
Design and Analysis of Algorithms
for(int j=i+1;j
-
8/6/2019 C Programs With Index
5/51
Design and Analysis of Algorithms
3. Program for Insertion Sort Implementation
#include#include
#include
void insertionSort(int *a);int n, *a;
void readData();
void display();int main()
{
clrscr();
readData();insertionSort(a);
display();
free(a);
return 0;}
void readData(){
coutn;
a=(int*)malloc(sizeof(int)*(n+1));cout
-
8/6/2019 C Programs With Index
6/51
Design and Analysis of Algorithms
4. Program for Quick Sort Implementation
#include#include
#include
void quickSort(int *a, int p , int r);int partition(int *a,int p, int r);
int n;
int *a;void readData();
void display();
int main()
{clrscr();
readData();
quickSort(a,1,n);display();
free(a);getch();
return 0;
}
void readData(){
coutn;a=(int*)malloc(sizeof(int)*(n+1));
cout
-
8/6/2019 C Programs With Index
7/51
Design and Analysis of Algorithms
int partition(int *a,int p, int r)
{int x=a[r];
int i=p-1;
for(int j=p;j
-
8/6/2019 C Programs With Index
8/51
Design and Analysis of Algorithms
5. Program for Merge Sort Implementation
#include#include
#include
void mergeSort(int *a, int p , int r);void merge(int *a,int p,int q, int r);
int n;
int *a;void readData();
void display();
int main()
{clrscr();
readData();
mergeSort(a,1,n);display();
free(a);getch();
return 0;
}
void readData(){
coutn;a=(int*)malloc(sizeof(int)*(n+1));
cout
-
8/6/2019 C Programs With Index
9/51
-
8/6/2019 C Programs With Index
10/51
Design and Analysis of Algorithms
6. Program for Heap Sort Implementation
#include#include
#include
void heapSort(int *a);void buildMaxHeap(int *a);
void maxHeapify(int *a, int i);
int n;int *a;
int heapSize;
void readData();
void display();int main()
{
clrscr();
readData();
heapSort(a);display();
free(a);
getch();
return 0;
}
void readData()
{coutn;
a=(int*)malloc(sizeof(int)*(n+1));cout
-
8/6/2019 C Programs With Index
11/51
Design and Analysis of Algorithms
for(int i=n;i>=2;i--)
{
a[1]^=a[i]^=a[1]^=a[i];heapSize--;
maxHeapify(a,1);
}}
void buildMaxHeap(int *a)
{
heapSize=n;
for(int i=n/2;i>=1;i--)maxHeapify(a,i);
}
void maxHeapify(int *a, int i){
int l=2*i;int r=2*i+1;
int largest;
if(la[i])
largest=l;else
largest=i;
if(ra[largest])
largest=r;
if(largest!=i)
{
a[i]^=a[largest]^=a[i]^=a[largest];maxHeapify(a,largest);
}
}
SCS DAVV, INDORE 11
-
8/6/2019 C Programs With Index
12/51
Design and Analysis of Algorithms
7. Program for Comparison Counting Sort Implementation
#include#include
#include
int *comparisionCountingSort(int *a,int n);int n;
int *a;
void readData();void display(int *a);
int main()
{
clrscr();
readData();
int *s=comparisionCountingSort(a,n);
display(s);free(a);
getch();
return 0;
}
void readData()
{coutn;
a=(int*)malloc(sizeof(int)*(n));cout
-
8/6/2019 C Programs With Index
13/51
Design and Analysis of Algorithms
for( i=0;i
-
8/6/2019 C Programs With Index
14/51
Design and Analysis of Algorithms
8. Program for Sequential Search Implementation
#include#include
#include
int search(int);int *a,n;
void readData();
void display();int main()
{
clrscr();
int key;
readData();
coutkey;
cout
-
8/6/2019 C Programs With Index
15/51
Design and Analysis of Algorithms
int i=0;
while(i
-
8/6/2019 C Programs With Index
16/51
Design and Analysis of Algorithms
9. Program for Quick Sequential Search Implementation
#include#include
#include
int search(int);int *a,n;
void readData();
void display();int main()
{
clrscr();
int key;
readData();
coutkey;
cout
-
8/6/2019 C Programs With Index
17/51
Design and Analysis of Algorithms
{
int i=0;a[n]=key;
while(a[i]!=key)
i++;return i==n?-1:i+1;
}
SCS DAVV, INDORE 17
-
8/6/2019 C Programs With Index
18/51
Design and Analysis of Algorithms
10. Program for Binary Search Implementation
#include#include
#include
void binarySearch(int low,int high,int key);int *a,n,found;
int mid;
void readData();void display();
int main()
{
clrscr();int key;
readData();
coutkey;binarySearch(0,n,key);
if(found)
cout
-
8/6/2019 C Programs With Index
19/51
Design and Analysis of Algorithms
cout
-
8/6/2019 C Programs With Index
20/51
-
8/6/2019 C Programs With Index
21/51
Design and Analysis of Algorithms
12. Program for finding closest points using Brute Force Technique
#include#include
#include
#include#define MAX 10
struct point
{int x;
int y;
};
void bruteForceClosestPoint(point *p,float *minDis,int *index1,int *index2);
int n;
void main()
{point p[MAX];
coutn;
cout
-
8/6/2019 C Programs With Index
22/51
Design and Analysis of Algorithms
if(d
-
8/6/2019 C Programs With Index
23/51
Design and Analysis of Algorithms
13. Program for Matrix Multiplication
#include#include
#include
#define MAX 5void matrixMultiply(int *a[MAX],int rowa, int cola,int *b[MAX], int rowb, int colb);
void readData(int *a[MAX],int row, int col);
void display(int *b[MAX],int row,int col);int main()
{
clrscr();
int *a[MAX],*b[MAX];int row1,row2,col1,col2;
coutrow1>>col1;
coutrow2>>col2;
if(col1==row2)
{
cout
-
8/6/2019 C Programs With Index
24/51
Design and Analysis of Algorithms
for(int j=0;j
-
8/6/2019 C Programs With Index
25/51
Design and Analysis of Algorithms
14. Program for Matrix Chain Multiplication
#include#include
int p[100],q;
long f(int i,int j) /*i stands for position of first matrix,j stands of last matrix*/{ long m;
int k,q;
if(i==j)return 0;
m=32099007;
for(k=i;k
-
8/6/2019 C Programs With Index
26/51
Design and Analysis of Algorithms
15. Program for returning change for given amt using Greedy Strategy
#include#include
int coins[]={100,25,10,5,2,1};
int s[25];int* make_change(int amt);
int i=0;
void main(){
clrscr();
int amt;
coutamt;
int *sol=make_change(amt);
cout
-
8/6/2019 C Programs With Index
27/51
Design and Analysis of Algorithms
else{
if(n>=1)
{ s[i++]=1;n=n-1;
}
}
}
}
}
}
return s;}
SCS DAVV, INDORE 27
-
8/6/2019 C Programs With Index
28/51
Design and Analysis of Algorithms
16. Program for finding minimum number of coins required for returning given amount
using Dynamic Programming.
#include
#include
#define MAX 3
int C[4][100];
int d[4]={0,1,4,6};
int f(int,int);
void main(){
clrscr();
for(int i=0;i
-
8/6/2019 C Programs With Index
29/51
Design and Analysis of Algorithms
{
C[i][j]=1+f(i,j-d[i]);
return(C[i][j]);}
if(j
-
8/6/2019 C Programs With Index
30/51
Design and Analysis of Algorithms
17. Program for Knap Sack Problem
#include#include
int n=3;
float M=20;float P[4];
float W[4];
float X[4];void knapsack();
void result();
void main()
{clrscr();
P[1]=24; P[2]=15; P[3]=25;
W[1]=15; W[2]=10; W[3]=18;
knapsack();result();
getch();}
void knapsack()
{
float cu = M;int i=1;
while(icu)
break;
X[i]=1;cu=cu-W[i];
i++;
}if(i
-
8/6/2019 C Programs With Index
31/51
-
8/6/2019 C Programs With Index
32/51
Design and Analysis of Algorithms
for( pp=1;pp
-
8/6/2019 C Programs With Index
33/51
Design and Analysis of Algorithms
j++;
goto cc;
}//ifj
-
8/6/2019 C Programs With Index
34/51
-
8/6/2019 C Programs With Index
35/51
Design and Analysis of Algorithms
19. Program for Breadth First Search Implementation
#include#include
char c[6];
int d[6],p[6],n[6],k=0,l=1,s,q[20],pos;int z=1;
void que(int);
int t[6][6]={{0,0,0,0,0,0},{0,0,1,1,0,0},
{0,1,0,0,1,0},
{0,1,0,0,1,1},
{0,0,1,1,0,1},{0,0,0,1,1,0}};
void main()
{ for(int i=1;i
-
8/6/2019 C Programs With Index
36/51
Design and Analysis of Algorithms
printf("c[%d]=%c , d[%d]=%d , p[%d]=%d\n",ii,c[ii],ii,d[ii],ii,p[ii]);
}
for( ii=1;ii
-
8/6/2019 C Programs With Index
37/51
Design and Analysis of Algorithms
20. Program for solving N-Queen problem using Backtracking
#include#include
#define MAX 4
int queen(int);int conflict(int);
int result[9];
void main(){clrscr();
if(queen(1))
{
for(int i=1;i
-
8/6/2019 C Programs With Index
38/51
Design and Analysis of Algorithms
int conflict(int level)
{int m,n,p;
m=n=p=result[level];
for(int i=level-1;i>=1;i--)
{
if((result[i]==++m)||(result[i]==--n)||(result[i]==p))return 1;
}
return 0;
}
SCS DAVV, INDORE 38
-
8/6/2019 C Programs With Index
39/51
Design and Analysis of Algorithms
21. Program for Subset Problem Using Backtracking
#include#include
#include
#define MAX 5int setResult(int);
int check(int);
int result[9];int result1[9];
int sum,sum1=0;
int set[9];
void main(){clrscr();
cout
-
8/6/2019 C Programs With Index
40/51
-
8/6/2019 C Programs With Index
41/51
Design and Analysis of Algorithms
22. Program for Solving N-Queen Problem using FIFO Branch and Bound Technique
#include#include
#define MAX 50
#define M 5class state
{
public:int result[M];
int level;
};
class queue{
int front,rear;
state a[MAX];
public:queue()
{front=-1;
rear=0;
}
void add(state x){
if(front==-1)
front=0;if(rear==MAX-1)
cout
-
8/6/2019 C Programs With Index
42/51
Design and Analysis of Algorithms
front=-1;
rear=0;
}return x;
}
};
state queen();
int conflict(state x);void main()
{
clrscr();
state x=queen();if(x.level==-1)
cout
-
8/6/2019 C Programs With Index
43/51
Design and Analysis of Algorithms
}
a.level=-1;
return a;}
int conflict(state x){
int m,n,p;
m=n=p=x.result[x.level];
for(int i=x.level-1;i>=1;i--)
{
if((x.result[i]==++m)||(x.result[i]==--n)||(x.result[i]==p))return 1;
}
return 0;
}
SCS DAVV, INDORE 43
-
8/6/2019 C Programs With Index
44/51
Design and Analysis of Algorithms
23. Program for calculating Factorial
#include#include
int fact(int);
int main(){
clrscr();coutn;
cout
-
8/6/2019 C Programs With Index
45/51
Design and Analysis of Algorithms
24. Program for calculating Power(X,N)
#include#include
int exp(int,int);
int main()
{clrscr();
coutx;coutn;
cout
-
8/6/2019 C Programs With Index
46/51
Design and Analysis of Algorithms
25. Program for finding GCD of two numbers using Euclid Algorithm
#include#include
void gcdByEuclid(int,int);void main()
{
clrscr();
int x,y;
printf("\n Enter the first Number :\n");
scanf("%d",&x);printf("\n Enter the second number :\n");
scanf("%d",&y);
gcdByEuclid(x,y);
getch();}
void gcdByEuclid(int x, int y)
{
int remainder, dividend, divisor;
dividend =x,divisor = y;
remainder = dividend % divisor;
while (remainder > 0){
dividend=divisor;
divisor =remainder;remainder =dividend % divisor;
}
printf("\n The GCD of input is :");printf("%d",divisor);
}
SCS DAVV, INDORE 46
-
8/6/2019 C Programs With Index
47/51
-
8/6/2019 C Programs With Index
48/51
Design and Analysis of Algorithms
27. Program for solving Sub Set Problem using FIFO Branch and Bound Technique
#include#include
#include
#define MAX 100#define M 6
class state
{public:
state();
int result[M];
int level;};
state::state()
{
for(int i=1;i
-
8/6/2019 C Programs With Index
49/51
Design and Analysis of Algorithms
state remove()
{state x;
x=a[front++];
if(rear==front){
front=-1;
rear=0;}
return x;
}
};
state SetResult();
int conflict(state x);
int result1[9];int sum,sum1=0;
int set[9];void main(){
clrscr();
cout
-
8/6/2019 C Programs With Index
50/51
Design and Analysis of Algorithms
queue b;
b.add(a);
while(!(b.empty())){
x=b.remove();
x.level++;i=1;
while(i
-
8/6/2019 C Programs With Index
51/51
Design and Analysis of Algorithms
for(i=1;i sum)
return 1;
return 0;
}
****************************