Saturday, 3 August 2013

Program to Implement Bubble Sort

#include<stdio.h>
#include<conio.h>
void main()
{           
int a[10],i,j,n,temp;
clrscr();
printf(“Enter the size of arrary\n”);
scanf(“%d”,&n);
printf(“Enter %d elements \n”);
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
for(i=1;i<=n;i++)
{
for(j=1;j>i;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
printf(“The sorted Array is\n”);
for(j=1;j<=n;j++){
printf(“%d”,a[j]);
}
getch();

}

Program to Implement Binary Search

#include<stdio.h>
#include<conio.h>
void main()
{
int beg, mid, end,a[20],item,loc,i,n;
clrscr();
printf(“Enter the size\n”);
scanf(%d”,&n);
printf(“Enter the elements\n”);
for(i=1;i<=n;i++)
{
scanf(“%d”,&a[i]);
}
beg=1;
end=n;
mid=(beg+end)/2;
printf(“Enter the item to be searched\n”);
scanf(“%d”,&item);
while((beg<=end)&&(a[mid]!=item))
{
if(item<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
printf(“The item is at mid = %d”,mid);
if(beg>end)
{
loc=0;
}
else
{
loc=mid;
}
printf(“The location of the item\n”);
printf(“%d”, loc);
getch();

Program to Implement the Link List

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct Node
{
int info;
struct Node *link;
};
struct Node *first;
void main()
{
create();
traverse();
insert();
}
void create()
{
struct Node *ptr, *cpt;
char ch;
ptr=(struct Node *) malloc(size of(struct Node));
printf(“Enter the first Node”);
scanf(“%d”,&ptr->info);
first=ptr;
}
do
{
cpt=(struct Node*) malloc(size of(struct Node));
printf(“Enter the next node”);
scanf(“%d”,&cpt->info);
ptr->link=cpt;
ptr=cpt;
printf(“Enter Y to continue”);
scanf(“%c”,&ch);
}
while(ch==’Y’);
ptr->link=first;
}
void traverse()
{
struct Node *ptr;
printf(“Traverse is”);
ptr=first;
while(ptr!=first);
{
printf(“%d”,ptr->info);
ptr=ptr->link;
}
}
void insert()
{
struct Node *ptr;
ptr=(struct Node*)malloc(size of(struct Node));
if(ptr==NULL)
}
printf(“Overflow”);
return;
}
printf(“Enter the info into new”);
scanf(“%d”,&ptr->info);
ptr->link=start;
start=ptr;

}