Sunday, October 11, 2009
sorting technix
BUBBLE SORT
#include
#include
void main()
{
int i,j,n,temp;
int a[50];
clrscr();
printf("Enter the size of the array\n");
scanf("%d",&n); //input size of array
printf("Enter the array elemnts\n"); //input elements of array
for(i=0;i
printf("a[%d]=",i);
scanf("%d",&a[i]);
printf("\n");
}
//implementing bubble sort
for(i=0;i
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
printf("Array After implenting Buuble sort \n");
for(i=0;i
getch();
}
2.SELECTION SORT
#include
#include
void main()
{
int i,j,n,posi,temp,min;
int a[50];
clrscr();
printf("Enter the size of the array\t(less than 50)\n");
scanf("%d",&n); //input size of array
printf("Enter the array elemnts\n"); //input elements of array
for(i=0;i
printf("a[%d]=",i);
scanf("%d",&a[i]);
printf("\n");
}
for(i=0;i
min=a[i];
posi=i;
for(j=i+1;j
if(a[j]<=min)
{
min=a[j];
posi=j;
}
}
temp=a[i];
a[i]=a[posi];
a[posi]=temp;
}
printf("Array After implenting Selection sort \n");
for(i=0;i
getch();
}
MERGESORT
#include
#include
void mergesort(int a[50],int low,int high);
void merge(int a[55],int low, int mid, int high);
void main()
{
int a[50],i,n;
clrscr();
printf("\nEnter the no. of Elements\n");
scanf("%d",&n);
printf("\nEnter the array elements \n");
for(i=0;i
printf("\nArray Before Sorting\n");
for(i=0;i
mergesort(a,0,n-1);
printf("\nArray After Sorting\n");
for(i=0;i
getch();
}
void mergesort(int a[50],int low,int high)
{
int mid;
if(low
mid=(low+high)/2;
mergesort(a,low,mid); //recursively calling the function
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[50], int low, int mid ,int high)
{
int c[50],i,j,k;
i=low;
j=mid+1;
k=low;
while((i<=mid) && (j<=high))
{
if(a[i]<=a[j])
{
c[k]=a[i];
k=k+1;
i=i+1;
}
c[k]=a[j];
k=k+1;
j=j+1;
}
while (i<=mid)
{
c[k]=a[i];
i=i+1;
k=k+1;
}
while (j<=high)
{
c[k]=a[j];
j=j+1;
k=k+1;
}
for(i=0;i
}
INSERTION SORT
#include
#include
void main()
{
int aray[50],i,j,n,x;
clrscr();
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the Array Elemnts\n");
for(i=1;i<=n;i++)
scanf("%d",&aray[i]);
printf("The Unsorted elements are\n");
for(i=1;i<=n;i++)
printf("%d\n",aray[i]);
for(i=2;i<=n;i++)
{
x=aray[i];
j=i-1;
while (x
aray[j+1]=aray[j];
j=j-1;
}
aray[j+1]=x;
}
printf("The sorted elements are \n");
for(i=1;i<=n;i++)
printf("%d\n",aray[i]);
getch();
}
SPARSE MATRIX
#include
#include
void main()
{
int mata[10][10],matb[10][10],row,col,i,j;
clrscr();
printf("Enter the order of matrix\n");
printf("Enter the no of rows\n");
scanf("%d",&row);
printf("Enter the no of Columns\n");
scanf("%d",&col);
printf("Enter the elements of matrix\n");
for(i=0;i
for(i=0;i
printf("The given matrix was\n");
for(i=0;i
for(j=0;j
printf("\n");
}
printf("The sparse of given matrix\n");
for(i=0;i
for(j=0;j
printf("\n");
}
getch();
}
ADDING NODES TO LINKED LIST DEMONSTRATION
#include
#include
#include
struct lnkdlst
{
int info;
struct lnkdlst *link;
};
typedef struct lnkdlst node;
void creation(node *);
void display(node *);
node *insertbeg(node *);
void insertend(node *);
void insertposi(node *);
void main()
{
node *start;
start=(node*)malloc(sizeof(node));
while(1)
{
int ch;
clrscr();
printf("\n\t\tSingly Linked list insertion Operation");
printf("\n\n\t*********************************************");
printf("\n\t*\t1.Create a linked list *");
printf("\n\t*\t2.Insertion at the begning *");
printf("\n\t*\t3.Insertion at the ending *");
printf("\n\t*\t4.Insertion at the desired position *");
printf("\n\t*\t5.Display *");
printf("\n\t*\t6.Exit *\n");
printf("\t*********************************************");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: creation(start);
break;
case 2: start=insertbeg(start);
break;
case 3: insertend(start);
break;
case 4: insertposi(start);
break;
case 5:display(start);
break;
case 6: exit(0);
break;
default:printf("\nInvalid Choice");
break;
}
}
//printf("\nstart=%d\n",start);
}
//***function to create linked list***//
void creation(node *curptr)
{
char ch;
node *newnode;
while(1)
{
printf("Enter the information\n");
scanf("%d",&curptr->info);
printf("Do You Wish to continue......(y/n)\n");
fflush(stdin);
ch=getchar();
if((ch=='y') (ch=='Y'))
{
newnode=(node*)malloc(sizeof(node));
curptr->link=newnode;
curptr=newnode;
}
else
{
curptr->link=NULL;
break;
}
}
}
//***function to display linked list***//
void display(node *curptr)
{
printf("linked list elements are\n");
while(curptr!=NULL)
{
printf("%d-->",curptr->info);
curptr=curptr->link;
}
printf("null");
getch();
}
//***Function to insert a node at the begining of the list***//
node *insertbeg(node *curptr)
{
node *newnode;
newnode=(node*)malloc(sizeof(node));
printf("Enter the information\n");
scanf("%d",&newnode->info);
newnode->link=curptr;
curptr=newnode;
return(curptr);
}
//***Function to insert a node to end of the linked list***//
void insertend(node *curptr)
{
node *newnode;
while(curptr!=NULL)
curptr=curptr->link;
newnode=(node*)malloc(sizeof(node));
printf("Enter the information\n");
scanf("%d",&newnode->info);
curptr->link=newnode;
newnode->link=NULL;
}
//***Function to insert a node to a desired position***//
void insertposi(node *curptr)
{
node *newnode;
node *temp;
int i,posi;
newnode=(node*)malloc(sizeof(node));
printf("Enter the position to be inserted");
scanf("%d",&posi);
printf("Enter the information\n");
scanf("%d",&newnode->info);
for(i=1;i
temp=curptr;
curptr=curptr->link;
}
temp->link=newnode;
newnode->link=curptr;
}
PROGRAM TO DELTE A NODE FROM A LINKED LIST
#include
#include
#include
struct lnkdlst
{
int info;
struct lnkdlst *link;
};
typedef struct lnkdlst node;
void creation(node *);
void display(node *);
node *delbeg(node *);
void delend(node *);
void delposi(node *);
void main()
{
node *start;
start=(node*)malloc(sizeof(node));
while(1)
{
int ch;
clrscr();
printf("\n\t\tSingly Linked list Deletion Operation");
printf("\n\n\t*********************************************");
printf("\n\t*\t1.Create a linked list *");
printf("\n\t*\t2.Deletion at the begning *");
printf("\n\t*\t3.Deletion at the ending *");
printf("\n\t*\t4.Deletion at the desired position *");
printf("\n\t*\t5.Display *");
printf("\n\t*\t6.Exit *\n");
printf("\t*********************************************");
printf("\nEnter your choice");
scanf("%d",&ch);
switch(ch)
{
case 1: creation(start);
break;
case 2: delbeg(start);
break;
case 3: delend(start);
break;
case 4: delposi(start);
break;
case 5:display(start);
break;
case 6: exit(0);
break;
default:printf("\nInvalid Choice");
break;
}
}
//printf("\nstart=%d\n",start);
}
//***function to create linked list***//
void creation(node *curptr)
{
char ch;
node *newnode;
while(1)
{
printf("Enter the information\n");
scanf("%d",&curptr->info);
printf("Do You Wish to continue......(y/n)\n");
fflush(stdin);
ch=getchar();
if((ch=='y') (ch=='Y'))
{
newnode=(node*)malloc(sizeof(node));
curptr->link=newnode;
curptr=newnode;
}
else
{
curptr->link=NULL;
break;
}
}
}
//***function to display linked list***//
void display(node *curptr)
{
printf("linked list elements are\n");
while(curptr!=NULL)
{
printf("%d-->",curptr->info);
curptr=curptr->link;
}
printf("null");
getch();
}
//***Function to insert a node at the begining of the list***//
node *delbeg(node *curptr)
{
printf("\nLinked list before deletion\n");
display(curptr);
curptr=curptr->link;
printf("\nLinked list after deletion\n");
display(curptr);
return(curptr);
}
//***Function to insert a node to end of the linked list***//
void delend(node *curptr)
{
node *temp;
node *head=curptr;
printf("\nLinked list before deletion\n");
display(curptr);
temp=curptr;
while(curptr->link!=NULL)
{
temp=curptr;
curptr=curptr->link;
}
temp->link=NULL;
printf("\nLinked list after deletion\n");
display(head);
}
//***Function to insert a node to a desired position***//
void delposi(node *curptr)
{
node *temp;
int i,posi;
printf("\nLinked list before deletion\n");
display(curptr);
printf("Enter the position to be Deleted");
scanf("%d",&posi);
for(i=1;i
temp=curptr;
curptr=curptr->link;
}
temp->link=curptr->link;
}