Ds Programs
Ds Programs
DATA STRUCTURE
1.MATRIX....................................................................................................................................................2
1.1.MATRIX REPRESENTATION USING 1-D ARRAY..............................................................................2
1.2.SPARSE MATRIX.................................................................................................................................4
1.3.INVERT SPARSE MATRIX...................................................................................................................7
1.4.LONG INTEGER...................................................................................................................................9
2.STACK IMPLEMENTATION.................................................................................................................11
2.1.STACK................................................................................................................................................11
2.2.DYNAMIC STACK...............................................................................................................................12
2.3.QUEUES.............................................................................................................................................13
2.4.DYNAMIC QUEUE..............................................................................................................................15
2.5.CIRCULAR QUEUE............................................................................................................................17
2.6.PRIORITY QUEUE.............................................................................................................................18
2.7.INFIX TO POSTFIX CONVERSION...................................................................................................20
2.8.POSTFIX EVAULATION.....................................................................................................................22
3.LINKED LIST IMPLEMENTATION........................................................................................................23
3.1.SINGLY LINKED LIST........................................................................................................................23
3.2.CIRCULAR LINKED LIST...................................................................................................................26
3.3.DOUBLY LINKED LIST.......................................................................................................................28
3.4.DOUBLY CIRCULAR LINKED LIST...................................................................................................28
3.5.TREE...................................................................................................................................................30
3.6.BINARY TREE....................................................................................................................................31
3.7.THREADED BINARY TREE...............................................................................................................34
4.SORTING TECHNIQUES......................................................................................................................36
4.1.BUBBLE SORT...................................................................................................................................36
4.2.SELECTION SORT.............................................................................................................................37
4.3.INSERTION SORT..............................................................................................................................38
4.4.MERGE SORT....................................................................................................................................39
4.5.HEAP SORT.......................................................................................................................................40
4.6.QUICK SORT......................................................................................................................................41
4.7.SHELL SORT......................................................................................................................................42
4.8.RADIX SORT......................................................................................................................................43
Page no:-1
DATA STRUCTURE
1. MATRIX
1.1. MATRIX REPRESENTATION USING 1-D ARRAY
#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct data
{
int row,col;
int m[max];
}mat;
void init(mat **m1,int r,int c)
{
(*m1)->row=r;
(*m1)->col=c;
}
void putvalue(mat **m1,int r,int c,int value)
{
*((*m1)->m + r*(*m1)->col + c)=value;
}
int getvalue(mat **m1,int r,int c)
{
return(*((*m1)->m + r*(*m1)->col + c));
}
void input(mat *m1)
{
int i,j,value,row,col;
printf("\n Enter size of matrix [row][col] : ");
scanf("%d %d",&row,&col);
init(&m1,row,col);
for(i=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
printf("\n enter value at pos [%d][%d] : ",i,j);
scanf("%d",&value);
putvalue(&m1,i,j,value);
}
}
int check_add(mat *m1,mat *m2,mat *m3)
{
int value=1;
if (m1->row==m2->row && m1->col==m2->col)
init(&m3,m1->row,m1->col);
else
value=0;
return(value);
}
int check_multi(mat *m1,mat *m2,mat *m3)
{
int value=1;
if (m1->row==m2->col)
Nirender Prakash Singh
Page no:-2
init(&m3,m1->row,m2->col);
else
value=0;
return(value);
}
void addmat(mat *m1,mat *m2,mat *m3) //Addition
{
int i,j;
for(i=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
putvalue(&m3,i,j,(getvalue(&m1,i,j) + getvalue(&m2,i,j)));
}
void submat(mat *m1,mat *m2,mat *m3) //Subtraction
{
int i,j;
for(i=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
putvalue(&m3,i,j,(getvalue(&m1,i,j) - getvalue(&m2,i,j)));
}
void multimat(mat *m1,mat *m2,mat *m3) //Multiplication
{
int i,j,k,value;
for(k=0;k<m2->col;k++)
for(i=0;i<m1->row;i++)
{
for(j=0,value=0;j<m1->col;j++)
value +=((getvalue(&m1,k,j) * getvalue(&m2,j,i)));
putvalue(&m3,k,i,value);
}
}
void display(mat *m1)
{
int i,j;
printf("\n");
for(i=0;i<m1->row;i++)
{
for(j=0;j<m1->col;j++)
printf("%5d",getvalue(&m1,i,j));
printf("\n\n");
}
}
void main()
{
mat m1,m2,m3;
clrscr();
input(&m1);
input(&m2);
if(check_add(&m1,&m2,&m3))
{
clrscr();
addmat(&m1,&m2,&m3);
printf("\n 1st MATRIX\n");
display(&m1);
printf("\n 2nd MATRIX\n");
Nirender Prakash Singh
Page no:-3
display(&m2);
printf("\n ADDITION OF BOTH MATRIX\n");
display(&m3);
printf("\n SUBTRACTION OF BOTH MATRIX\n");
submat(&m1,&m2,&m3);
display(&m3);
}
else
{
printf("\n\n Matrix addition is not possible ...");
printf("\n Please enter correct matrix size\b");
}
if(check_multi(&m1,&m2,&m3))
{
printf("\n MULTIPACTION OF BOTH MATRIX\n");
multimat(&m1,&m2,&m3);
display(&m3);
}
getch();
}
Page no:-4
value=(m1)->d[i].v;
return(value);
}
void input(spmat *m1)
{
int i,row,col,value;
printf("\n Enter size of matrix [Row][Col] : ");
scanf("%d %d",&row,&col);
printf("\n Enter no. for no. of value to be enter : ");
scanf("%d",&value);
init(&m1,row,col,value);
printf("\n Enter only value i.e >0 or <0\n");
for(i=0;i<m1->no;i++)
{
printf("\n Enter position [Row][Col] : ");
scanf("%d %d",&row,&col);
printf("\n Enter value : ");
scanf("%d",&value);
putvalue(&m1,row,col,i,value);
}
}
void display(spmat *m1)
{
int i,j,row,col,count,value;
printf("\n Value of [%d] by [%d] matrix : \n\n",(m1)->row,(m1)->col);
for(i=0;i<(m1)->row;i++, printf("\n"))
{
for(j=0;j<(m1)->col;j++)
printf("%5d",(getvalue(m1,i,j)));
}
}
void addspmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,add,pos;
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(i=0,pos=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
add=getvalue(m1,i,j)+getvalue(m2,i,j);
if(add!=0)
{
putvalue(&m3,i,j,pos,add);
m3->no +=1;
pos++;
}
}
display(m3);
}
void subspmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,sub,pos;
if(m1->row==m2->row &&m1->col==m2->col)
{
Nirender Prakash Singh
Page no:-5
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(i=0,pos=0;i<m1->row;i++)
for(j=0;j<m1->col;j++)
{
sub=getvalue(m1,i,j)-getvalue(m2,i,j);
if(sub!=0)
{
putvalue(&m3,i,j,pos,sub);
m3->no +=1;
pos++;
}
}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void multispmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,k,multi,pos;
if(m1->col==m2->row)
{
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(k=0;k<m2->col;k++)
for(i=0,pos=0;i<m1->row;i++)
for(j=0,multi=0;j<m1->col;j++)
{
multi +=getvalue(m1,k,j)*getvalue(m2,j,i);
if(multi!=0)
{
putvalue(&m3,k,i,pos,multi);
m3->no +=1;
pos++;
}
}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void main()
{
spmat m1,m2,m3;
clrscr();
input(&m1);input(&m2);
display(&m1);display(&m2);
addspmat(&m1,&m2,&m3);
subspmat(&m1,&m2,&m3);
getch();
}
Nirender Prakash Singh
Page no:-6
Page no:-7
}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void multispmat(spmat *m1,spmat *m2,spmat *m3)
{
int i,j,k,multi,pos;
if(m1->col==m2->row)
{
m3->row=m1->row;
m3->col=m1->col;
m3->no=0;
for(k=0;k<m2->col;k++)
for(i=0,pos=0;i<m1->row;i++)
for(j=0,multi=0;j<m1->col;j++)
{
multi +=getvalue(m1,k,j)*getvalue(m2,j,i);
if(multi!=0)
{
putvalue(&m3,k,i,pos,multi);
m3->no +=1;
pos++;
}
}
display(m3);
}
else
printf("\n\n Subtraction is not possible ...check matrix size ");
}
void main()
{
spmat m1,im1,m2,im2,m3,im3;
clrscr();
input(&m1);input(&m2);
invert(&m1,&im1);invert(&m2,&im2);
clrscr();
display(&im1);display(&im2);
printf("\nADDITION OF TWO MATRIX \n" );
addspmat(&im1,&im2,&im3);
getch();
}
Page no:-9
Page no:-10
2. STACK IMPLEMENTATION
2.1. STACK
#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct stack
{
int top;
int data[max];
}stack;
int check_empty(stack s)
{
if(s.top==-1)
return(1);
return(0);
}
int check_full(stack s)
{
if(s.top<max-1)
return(0);
return(1);
}
void push(stack *s,int value)
{
(((s)->data[++((s)->top)]))=value;
}
int pop(stack *s)
{
return(((s)->data[((s)->top)--]));
}
void show_stack(stack s)
{
int i;
for(i=0;i<=s.top;i++)
printf("%3d",s.data[i]);
}
void main()
{
stack s;
int choice=5,value;
clrscr();
s.top=-1;
printf("\n\t\t STACK IMPLIMENT USING ARRAY");
Nirender Prakash Singh
Page no:-11
Page no:-12
s->top=0;
}
void push(stack *s,int value) //Insertion
{
node *n;
n=(node *) malloc(sizeof(node));
n->next=s->top;
n->data=value;
s->top=n;
}
void pop(stack *s)
//Deletion
{
node *n;
n=s->top;
s->top=s->top->next;
free(n);
}
void show(node **top)
{
for(;*top;top=&(*top)->next)
printf("\t%d",(*top)->data);
}
void main()
{
stack s;
init(&s);
clrscr();
push(&s,10);
push(&s,20);
push(&s,30);
push(&s,40);
printf("\n");
show(&s.top);
getch();
printf("\n");
pop(&s);
show(&s.top);
getch();
}
2.3. QUEUES
#include<stdio.h>
#include<conio.h>
#define max 100
typedef struct queu
{
int front;
int rear;
int data[max];
}queu;
int q_empty(queu q)
{
Nirender Prakash Singh
Page no:-13
if(q.rear==q.front)
return(1);
return(0);
}
int q_full(queu q)
{
if(q.rear<max-1)
return(0);
return(1);
}
void init(queu *q)
{
q->rear=-1;
q->front=-1;
}
void push(queu *q,int value)
{
q->data[++(q->rear)]=value;
}
void pop(queu *q)
{
(q->front)++;
}
void show(queu q)
{
int i;
for(i=q.front+1;i<=q.rear;i++)
printf("%3d",q.data[i]);
}
void main()
{
queu q;
int choice=5,value;
clrscr();
init(&q);
printf("\n\t\t Queu IMPLIMENT USING ARRAY");
printf("\n\t 1. Push \n\t 2. Pop \n\t 3. Show \n\t 4. Exit \n");
while(choice!=4)
{
printf("\n Enter your choice : ");
scanf("%d",&choice);
if(choice==1)
{
if(!q_full(q))
{
printf("\n Enter value : ");
scanf("%d",&value);
push(&q,value);
}
else
{
printf("\n Queu is full");
}
}
else if(choice==2)
Nirender Prakash Singh
Page no:-14
{
if(!q_empty(q))
{
pop(&q);
printf("\n Queu poped");
}
else
{
printf("\n Queu is Empty");
}
}
else if(choice==3)
{
if(!q_empty(q))
{
printf("\n See Queu value : ");
show(q);
}
else
{
printf("\n Queu is Empty");
}
}
}
}
Page no:-15
if(q->front==0)
q->rear=q->front=n;
else
q->rear=q->rear->next=n;
}
void pop(queue *q)
{
node *n;
n=q->front;
q->front=q->front->next;
free(n);
}
void show(queue q)
{
for(;q.front;q.front=q.front->next)
printf("%4d",q.front->data);
}
void main()
{
queue q;
int choice=5,value;
clrscr();
init(&q);
printf("\n\t\t\t DYNAMIC QUEUE");
printf("\n\t 1. Push \n\t 2. Pop \n\t 3. Show \n\t 4. Exit \n");
while(choice!=4)
{
printf("\n Enter your choice : ");
scanf("%d",&choice);
if(choice==1)
{
printf("\n Enter value : ");
scanf("%d",&value);
push(&q,value);
}
else if(choice==2)
{
if(!empty(q))
{
pop(&q);
printf("\n Queu poped");
}
else
printf("\n\t\tQueue is Empty...");
}
else if(choice==3)
{
if(!empty(q))
{
printf("\n See Queu value : ");
show(q);
}
else
printf("\n\t\tQueue is Empty...");
}
Nirender Prakash Singh
Page no:-16
}
}
Page no:-17
{
printf("\n Enter your choice : ");
scanf("%d",&choice);
if(choice==1)
{
if(!full(q))
{
printf("\n Enter value : ");
scanf("%d",&value);
push(&q,value);
}
else
{
printf("\n\t\t Queu is full");
}
}
else if(choice==2)
{
if(!empty(q))
{
pop(&q);
printf("\n Queu poped");
}
else
{
printf("\n\t\t Queu is Empty");
}
}
else if(choice==3)
{
if(!empty(q))
{
printf("\n See Queu value : ");
show(q);
}
else
{
printf("\n\t\t Queu is Empty");
}
}
}
}
Page no:-18
Page no:-19
char ch;
pqueue q;
int i,c=0,l;
init(&q);
while((ch=*str)!='\0'&&!full(&q))
{
str++;
insert(&q,ch);
c++;
}
for(i=0;i<c&&!full(&q);i++)
{
l=delet(&q);
insert(&q,l);
printqueue(&q);
}
}
void main()
{
clrscr();
cirprintstr("ramu");
getch();
}
Page no:-20
Page no:-21
char str1[40],str2[40];
clrscr();
init(&s);
printf("\n Enter infix expression : ");
gets(str1);
to_postfix(str1,str2);
printf("\n See postfix expression : ");
puts(str2);
getch();
}
Page no:-22
char str[50];
int op1,op2,op3,i;
clrscr();
printf("\n Enter postfix expression : ");
for(i=0;(str[i]=getchar())!='\n';i++)
{
if(isdigit(str[i]))
{
push(&s,str[i]-48);
}
else
{
op2=s.data[s.top];
pop(&s);
op1=s.data[s.top];
pop(&s);
op3=camput(op1,op2,str[i]);
push(&s,op3);
}
}
printf("\n Result : %d",op3);
getch();
}
Page no:-23
}
void ins_end(list **head,list *node)
{
for(;*head;head=&(*head)->next);
(*head)=node;
}
void del_npos_beg(list **head,int pos)
{
list **h;
for(h=head;pos>0;head=&(*head)->next,pos--);
*h=*head;
}
void del_npos_end(list **head,int pos)
{
list **h;
int c;
for(c=0,h=head;*head;head=&(*head)->next,c++);
for(c=c-pos;c>0;h=&(*h)->next,c--);
*h=*head;
}
void ins_pos(list **head,list *node,int pos)
{
list **h;
for(;pos>0&&*head;head=&(*head)->next,pos--)
h=head;
if(pos<1)
{
node->next=*h;
*h=node;
}
else
printf("\n Position is out of range");
}
void del_pos(list **head,int pos)
{
list **h;
for(;pos>0&&*head;head=&(*head)->next,pos--)
h=head;
if(pos<1)
*h=*head;
else
printf("\n Position is out of range");
}
void del_value(list **head,int value)
{
list **h;
for(;*head;head=&(*head)->next)
if((*head)->data==value)
*head=(*head)->next;
}
void reverse(list *head,list **h1)
Nirender Prakash Singh
Page no:-24
{
list **h2,**h3;
int n,c,i;
for(n=0,h2=h3=h1;*h1;h1=&(*h1)->next,n++);
for(i=1,h1=h2,h2=&(*h2)->next;n>1;n--,i++)
{
for(c=n;c>i;c--,h1=&(*h1)->next);
*h3=*h1;
h3=&(*h3)->next;
h1=h2;
}
*h3=head;
(*h3)->next=0;
}
void split(list **head,list **odd,list **even)
{
for(;*head;head=&(*head)->next)
{
if(((*head)->data)%2)
{
*odd=*head;
odd=&(*odd)->next;
}
else
{
*even=*head;
even=&(*even)->next;
}
}
if(((*head)->data)%2)
{
(*odd)->next=0;
}
else
{
(*even)->next=0;
}
(*odd)->next=0;
(*even)->next=0;
}
void main()
{
list *head=0,*odd=0,*even=0,*node;
int n,pos,value;
clrscr();
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(list *)malloc(4);
node->data=n;
node->next=0;
Nirender Prakash Singh
Page no:-25
creat(&head,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
}
display(&head);
printf("\n Enter value to be insert : ");
scanf("%d",&n);
node=(list *)malloc(4);
node->data=n;
node->next=0;
printf("\n Enter pos : ");
scanf("%d",&pos);
printf("\n Enter value : ");
scanf("%d",&value);
del_pos(&head,pos);
del_npos_beg(&head,pos);
del_npos_end(&head,pos);
del_value(&head,value);
ins_pos(&head,node,pos);
ins_beg(&head,node);
ins_end(&head,node);
reverse(head,&head);
display(&head);
/*
printf("\n Enter value to be insert : ");
scanf("%d",&n);
node=(list *)malloc(4);
node->data=n;
node->next=0;
ins_end(&head,node);
display(&head);
printf("\n Enter pos to be delete : ");
scanf("%d",&pos);
del_npos(&head,node,pos);
*/
reverse(&head);
split(&head,&odd,&even);
printf("\n");
display(&odd);
printf("\n");
display(&even);
getch();
Page no:-26
Page no:-28
#include<stdlib.h>
#include<conio.h>
typedef struct list
{
int data;
struct list *pre,*next;
}list;
void creat(list **head,list *node)
{
list *h=0,*pre=0;
for(h=pre=*head;h && (*head)->next !=h;pre=pre->next,head=&(*head)->next);
if(h)
{
(*head)->next=h->pre=node;
node->next=h;
node->pre=pre;
}
else
(*head)=node->pre=node->next=node;
}
void display(list **head)
{
list *h=0;
if(*head)
{
for(h=*head;h && (*head)->next !=h;head=&(*head)->next)
printf("\n %d\t%u\t%u",(*head)->data,(*head)->pre,(*head)->next);
printf("\n %d\t%u\t%u",(*head)->data,(*head)->pre,(*head)->next);
}
else
printf("\n Doubly Circular linked list is empty ... ");
}
void main()
{
list *head=0,*node;
int n,pos,value;
clrscr();
printf("\n DOUBLY CIRCULAR LINKED LIST ... ");
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(list *)malloc(sizeof(list));
node->data=n;
node->pre=node->next=0;
creat(&head,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
}
display(&head);
getch();
}
Nirender Prakash Singh
Page no:-29
3.5. TREE
#include<stdio.h>
#include<conio.h>
typedef struct tree
{
int data;
struct tree *left,*right;
}tree;
void init(tree *node)
{
node->left=node->right=0;
}
void maketree(tree **root,tree *node)
{
for(;*root && (*root)->data !=node->data;
root=((node->data)<((*root)->data))?&(*root)->left:&(*root)->right);
*root=node;
}
void preorder(tree **root)
{
if(*root)
{
printf("\n%d",(*root)->data);
printf("\t%d",(*root)->left);
printf("\t%d",(*root)->right);
preorder(&(*root)->left);
preorder(&(*root)->right);
}
}
void inorder(tree **root)
{
if(*root)
{
inorder(&(*root)->left);
printf("\n%d",(*root)->data);
printf("\t%d",(*root)->left);
printf("\t%d",(*root)->right);
inorder(&(*root)->right);
}
}
void postorder(tree **root)
{
if(*root)
{
postorder(&(*root)->left);
postorder(&(*root)->right);
printf("\n%d",(*root)->data);
printf("\t%d",(*root)->left);
printf("\t%d",(*root)->right);
}
}
Nirender Prakash Singh
Page no:-30
void main()
{
tree *root=0,*node;
int n;
clrscr();
printf("\n\t\t\t Tree Implementation \n ");
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
while(n!=-99)
{
node=(tree *)malloc(sizeof(tree));
init(node);
node->data=n;
maketree(&root,node);
printf("\n Enter number (-99 for exit): ");
scanf("%d",&n);
}
printf("\n\n\t\tPreorder Traversal \n");
preorder(&root);
printf("\n\n\t\tInorder Traversal \n");
inorder(&root);
printf("\n\n\t\tpostorder Traversal \n");
postorder(&root);
getch();
}
//////preorder//////
void preorder()
{
BT t,p;
t=root;
if(!root)
{
printf("Tree does not exist ");
return;
}
do
Nirender Prakash Singh
Page no:-31
{
p=NULL;
while(t!=NULL)
{
printf("%d\t",t->d);
p=t;
t=t->l;
}
if(p!=NULL)
t=p->r;
while(p->f==1&&t!=NULL)
{
p=t;
t=t->r;
}
}while(t!=NULL);
return;
}
//////inorder traverse//////
void inorder()
{
BT t,p;
t=root;
if(!root)
{
printf("\n\tTree doesn't exist");
return;
}
do
{
p=NULL;
while(t!=NULL)
{
p=t;
t=t->l;
}
if(p!=NULL)
{
printf("%d\t",p->d);
t=p->r;
while(p->f==1&&t!=NULL)
{
printf("%d\t",t->d);
p=t;
t=t->r;
}
}
}while(p!=NULL);
return;
}
Nirender Prakash Singh
Page no:-32
void addnode(int x)
{
BT t,p;
int v;
t=root;
if(!root)
{
t=root=(BT)malloc(sizeof (bt));
t->f=0;
t->d=x;
t->l=t->r=NULL;
return;
}
while(t!=NULL)
{
p=t;
if(x<=t->d)
t=t->l;
else
{
if(t->f!=0)
break;
t=t->r;
}
}
if(x<=p->d)
{
t=(BT)malloc(sizeof (bt));
t->d=x;
t->f=1;
t->l=NULL;
p->l=t;
t->r=p;
}
else
{
t=(BT)malloc(sizeof(bt));
if(p->r==NULL)
t->f=0;
else
t->f=1;
t->d=x;
t->l=NULL;
t->r=p->r;
p->r=t;
p->f=0;
}
}
///////////////////
void main()
{
int ch,v;
clrscr();
root=NULL;
Nirender Prakash Singh
Page no:-33
clrscr();
while(1)
{clrscr();
printf("1.create \n2.Inorder\n3.Preorder\n4.Postorder\n5.Mirror\n6.Delete\n7.Exit");
printf("\n\nEnter choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter value to add:");
scanf("%d",&v);
addnode(v);
break;
case 2:
inorder();
break;
case 3:
preorder(root);
break;
case 7:
exit(0);
default :
printf("INVALID choice");
}
getch();
}
}
Page no:-34
h->rt=0;
h->right=p;
}
void insert(tbt **h,tbt *p)
{
tbt *q;
int f;
if(*h==0)
{
*h=p;
p->left=p->right=0;
p->lt=p->rt=1;
}
else
{
q=*h;
f=0;
while(f!=1)
{
if(q->rt==0&&p->d>q->d)
q=q->right;
else
if(q->lt==0&&p->d<=q->d)
q=q->left;
else
f=1;
}
if(p->d>q->d)
insert_right(q,p);
else
insert_left(q,p);
}
}
tbt*in_succ(tbt *t)
{
if(t->rt==1)
return(t->right);
else
t=t->right;
while(t->lt==0)
t=t->left;
return(t);
}
void inorder(tbt *t)
{
while(t->left)
t=t->left;
while(t)
{
printf("% 5d",t->d);
t=in_succ(t);
}
}
Nirender Prakash Singh
Page no:-35
4. SORTING TECHNIQUES
4.1. BUBBLE SORT
#include<stdio.h>
#include<conio.h>
void putmax(int a[],int n)
{
int t;
if(n>1)
{
if(a[0]>a[1])
{
t=a[0];
Nirender Prakash Singh
Page no:-36
a[0]=a[1];
a[1]=t;
}
putmax(a+1,n-1);
}
}
void bubblesort(int a[],int n)
{
if(n>1)
{
putmax(a,n);
bubblesort(a,n-1);
}
}
void main()
{
int a[20],i,n,t[10],j,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubblesort(a,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
Page no:-37
putmin(a,n);
selectionsort(a+1,n-1);
}
}
void main()
{
int a[20],i,n,t[10],j,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
selectionsort(a,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
Page no:-38
Page no:-39
int a[20],i,n,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
Page no:-40
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
else
f=1;
}
}
void heapsort(int a[])
{
int i,t;
i=a[0];
do{
t=a[1];
a[1]=a[a[0]];
a[a[0]]=t;
a[0]--;
downadjust(a,1);
}while(a[0]>1);
a[0]=i;
}
void display(int a[])
{
int i;
printf("\n the sorted array:-");
for(i=1;i<=a[0];i++)
printf("%5d",a[i]);
}
void main()
{
int a[max],n;
clrscr();
printf("\n enter the size of array");
scanf("%d",&a[0]);
create_heap(a);
heapsort(a);
display(a);
getch();
}
Page no:-41
do{
do{i++;}while(a[i]<v);
do{j--;}while(a[j]>v);
if(i<j)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}while(i<j);
a[m]=a[j];
a[j]=v;
return(j);
}
void quicksort(int a[],int i,int j)
{
int p;
if(i<j)
{
p=partition(a,i,j);
quicksort(a,i,p);
quicksort(a,p+1,j);
}
}
void main()
{
int a[20],i,n,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quicksort(a,0,n);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
Page no:-42
{
v=a[l];
for(j=l-s;j>=0&&a[j]>v;j=j-s)
{
a[j+s]=a[j];
}
a[j+s]=v;
}
}
}
void main()
{
int a[20],i,n,t[10],j,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the no of steps ");
scanf("%d",&d);
printf("\n enter the steps in an array");
for(j=0;j<d;j++)
scanf("%d",&t[j]);
shellsort(a,n,t,d);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
Page no:-43
}
int empty(queue *q)
{
if(q->f==-1)
return(1);
return(0);
}
void insert(queue *q,int v)
{
if(q->f==-1)
q->f=q->r=0;
else
q->r++;
q->a[q->r]=v;
}
int delet(queue *q)
{
int v;
v=q->a[q->f];
if(q->f==q->r)
q->f=q->r=-1;
else
q->f++;
return(v);
}
void radixsort(int a[],int n,int d)
{
queue q[10];
int k=10,i,j,l,r,c;
for(i=0;i<10;i++)
init(&q[i]);
for(j=0;j<d;j++)
{
for(l=0;l<n;l++)
{
c=(a[l]%k)/(k/10);
insert(&q[c],a[l]);
}
k=k*10;
for(i=0,r=0;i<10;i++)
{
while(!empty(&q[i]))
{
a[r]=delet(&q[i]);
r++;
}
}
}
}
void main()
Nirender Prakash Singh
Page no:-44
{
int a[20],i,n,d;
clrscr();
printf("\n enter the no of elements");
scanf("%d",&n);
printf("\n enter the elements in an array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("\n enter the max no of digits ");
scanf("%d",&d);
radixsort(a,n,d);
printf("\n the sorted array:-");
for(i=0;i<n;i++)
printf("%5d",a[i]);
getch();
}
Page no:-45