Divide and Conquer is a programming technique which makes the program more efficient to write. And this technique work on the concept of recursion to solve a problem step by step. Generally this technique work in three parts:- Divide:- Divide the problem into some subproblem. Conquer:- Conquer the subproblem by calling recursively until subproblem solved. Combine:- (Optional Step) Combine the subproblem solution. So, that we will get the final problem Solution. When the subproblems are large enough to solve recursively, we call the recursive case. Once the subproblem becomes small enough that we no longer recursive, we say that the recursion "bottom out" and that we have gotten down to the base case. Application of Divide and Conquer Quick Sort Strassen's algorithm for matrix multiplication Merge Sort Counting inversions Binary Search Finding Min and max Divide and Conquer Abstract Algorithm DAC(a,i,j) {
/*BUBBLE SORTING WITH GRAPHICAL REPRESENTATION*/
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void object(int,int,int);
void bubble(int);
void flow(int,int);
void mixing(int,int);
int a[10];
void main()
{
int n,i,gd=0,gm=0;
clrscr();
printf("Enter the No of Elements : " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d Element : ",i+1);
scanf("%d",&a[i]);
}
initgraph(&gd,&gm,"c://tc//bgi");
settextstyle(3,0,1);
outtextxy(250,50,"GIVEN NUMBER");
for(i=0;i<n;i++)
{
object(100+i*50,150,a[i]);
getch();
}
bubble(n);
/* for(i=0;i<n;i++)
{
object(100+i*50,350,a[i]);
getch();
}*/delay(500);
settextstyle(3,0,2);
outtextxy(400,400,"prepared by rishabh");
getch();
}
void bubble(int n)
{
int i,j,temp;
for(i=0;i<n;i++)
for(j=i;j<n-1;j++)
if(a[i]>a[j+1])
{
flow(i,j+1);
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
}
}
void object(int x,int y,int no)
{
char s[8];
sprintf(s,"%d",no);
circle(x,y,15);
settextstyle(2,0,6);
outtextxy(x-3,y-10,s);
}
void flow(int f,int s)
{
int i;
for(i=0;i<50;i++)
{
setcolor(WHITE);
object(100+f*50,150+i*4,a[f]);
delay(15);
setcolor(0);
object(100+f*50,150+i*4,a[f]);
}
setcolor(WHITE);
object(100+f*50,150+i*4,a[f]);
for(i=0;i<50;i++)
{
setcolor(WHITE);
object(100+s*50,150+i*4,a[s]);
delay(10);
setcolor(0);
object(100+s*50,150+i*4,a[s]);
}
setcolor(WHITE);
object(100+s*50,150+i*4,a[s]);
mixing(f,s);
for(i=50;i>0;i--)
{
setcolor(WHITE);
object(100+f*50,150+i*4,a[s]);
delay(10);
setcolor(0);
object(100+f*50,150+i*4,a[s]);
}
setcolor(WHITE);
object(100+f*50,150+i*4,a[s]);
for(i=50;i>0;i--)
{
setcolor(WHITE);
object(100+s*50,150+i*4,a[f]);
delay(15);
setcolor(0);
object(100+s*50,150+i*4,a[f]);
}
setcolor(WHITE);
object(100+s*50,150+i*4,a[f]);
}
void mixing(int f,int s)
{
int i;
for(i=0;i<(s-f)*50;i++)
{
setcolor(WHITE);
object(100+f*50+i,350,a[f]);
object(100+s*50-i,350,a[s]);
delay(20);
setcolor(0);
object(100+f*50+i,350,a[f]);
object(100+s*50-i,350,a[s]);
}
setcolor(WHITE);
object(100+f*50+i,350,a[f]);
object(100+s*50-i,350,a[s]);
}
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void object(int,int,int);
void bubble(int);
void flow(int,int);
void mixing(int,int);
int a[10];
void main()
{
int n,i,gd=0,gm=0;
clrscr();
printf("Enter the No of Elements : " );
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d Element : ",i+1);
scanf("%d",&a[i]);
}
initgraph(&gd,&gm,"c://tc//bgi");
settextstyle(3,0,1);
outtextxy(250,50,"GIVEN NUMBER");
for(i=0;i<n;i++)
{
object(100+i*50,150,a[i]);
getch();
}
bubble(n);
/* for(i=0;i<n;i++)
{
object(100+i*50,350,a[i]);
getch();
}*/delay(500);
settextstyle(3,0,2);
outtextxy(400,400,"prepared by rishabh");
getch();
}
void bubble(int n)
{
int i,j,temp;
for(i=0;i<n;i++)
for(j=i;j<n-1;j++)
if(a[i]>a[j+1])
{
flow(i,j+1);
temp=a[i];
a[i]=a[j+1];
a[j+1]=temp;
}
}
void object(int x,int y,int no)
{
char s[8];
sprintf(s,"%d",no);
circle(x,y,15);
settextstyle(2,0,6);
outtextxy(x-3,y-10,s);
}
void flow(int f,int s)
{
int i;
for(i=0;i<50;i++)
{
setcolor(WHITE);
object(100+f*50,150+i*4,a[f]);
delay(15);
setcolor(0);
object(100+f*50,150+i*4,a[f]);
}
setcolor(WHITE);
object(100+f*50,150+i*4,a[f]);
for(i=0;i<50;i++)
{
setcolor(WHITE);
object(100+s*50,150+i*4,a[s]);
delay(10);
setcolor(0);
object(100+s*50,150+i*4,a[s]);
}
setcolor(WHITE);
object(100+s*50,150+i*4,a[s]);
mixing(f,s);
for(i=50;i>0;i--)
{
setcolor(WHITE);
object(100+f*50,150+i*4,a[s]);
delay(10);
setcolor(0);
object(100+f*50,150+i*4,a[s]);
}
setcolor(WHITE);
object(100+f*50,150+i*4,a[s]);
for(i=50;i>0;i--)
{
setcolor(WHITE);
object(100+s*50,150+i*4,a[f]);
delay(15);
setcolor(0);
object(100+s*50,150+i*4,a[f]);
}
setcolor(WHITE);
object(100+s*50,150+i*4,a[f]);
}
void mixing(int f,int s)
{
int i;
for(i=0;i<(s-f)*50;i++)
{
setcolor(WHITE);
object(100+f*50+i,350,a[f]);
object(100+s*50-i,350,a[s]);
delay(20);
setcolor(0);
object(100+f*50+i,350,a[f]);
object(100+s*50-i,350,a[s]);
}
setcolor(WHITE);
object(100+f*50+i,350,a[f]);
object(100+s*50-i,350,a[s]);
}
Comments
Post a Comment