m.tech DSA
-
Upload
sumeet-kaur -
Category
Documents
-
view
235 -
download
0
Transcript of m.tech DSA
-
7/31/2019 m.tech DSA
1/78
1
Program 1: WAP to implement array as an ADT.
#include
#include
void main()
{
clrscr();
int a[100] ,i, num, pos, item, size;
coutsize;
cout
-
7/31/2019 m.tech DSA
2/78
2
cout
-
7/31/2019 m.tech DSA
3/78
3
Output:
enter the no. of elements: 3
enter the elements of array:
4
3
2
Now array is:
Element at position1 is: 4
Element at position2 is: 3
Element at position3 is: 2
Press any key to insert element
Enter another element: 1
Enter position of element: 4
After insertion array becomes:
Element at position1 is: 4
Element at position2 is: 3
Element at position3 is: 2
Element at position4 is: 1
Press any key to delete element from array
Enter the position number of the element to be deleted: 1
Deleted element: 4
New array:
Element at position1 is: 3
Element at position2 is: 2
Element at position3 is: 1
-
7/31/2019 m.tech DSA
4/78
-
7/31/2019 m.tech DSA
5/78
5
else {
coutele;
top=top+1;
stack[top]=ele; } }
void pop() {
if(top==-1) {
cout
-
7/31/2019 m.tech DSA
6/78
6
Output:
enter ur choice:
1.Push
2.Pop
3.display
4.exit
1
Enter the element to insert
2
Enter ur choice:
1.Push
2.Pop
3.display
4.exit
1
Enter the element to insert
1
Enter ur choice:
1.Push
2.Pop
3.display
4.exit
3
Elements in stack are:
1
2
-
7/31/2019 m.tech DSA
7/78
7
Program 3: WAP to implement stack using pointer implementation.
#include
#include
#include
#include
struct stack {
int info;
struct stack *next ;
}; struct stack *top=NULL,*temp;
void main() {
int choice,ele;
clrscr();
while(1) {
cout
-
7/31/2019 m.tech DSA
8/78
8
while(temp!=NULL) {
cout
-
7/31/2019 m.tech DSA
9/78
9
Output:
enter ur choice:
1.Push
2.Pop
3.display
4.exit
1
Enter the element to insert
2
Enter ur choice:
1.Push
2.Pop
3.display
4.exit
1
Enter the element to insert
1
Enter ur choice:
1.Push
2.Pop
3.display
4.exit
3
Elements in stack are:
1 2
-
7/31/2019 m.tech DSA
10/78
10
Program 4: WAP to implement linear queue using array implementation.
#include
#include
#include
#define MAX 5
void enqueue(int *,int,int *,int *);
void dequeue(int *,int *,int *);
void qdisplay(int *,int *,int *);
void main()
{
clrscr();
int queue[MAX];
int front =-1;
int rear=-1;
int value,n;
int choice,i;
while(1) {
cout
-
7/31/2019 m.tech DSA
11/78
11
exit(0);
break;
}
getch();
} }
void enqueue(int *queue,int value,int *front,int *rear)
{
if(*rear==MAX-1) {
cout
-
7/31/2019 m.tech DSA
12/78
12
for(i=*front;i
-
7/31/2019 m.tech DSA
13/78
13
Program 5: WAP to implement linear queue using pointer implementation.
#include
#include
#include
#include
struct queue
{
int info;
struct queue *next;
};
struct queue *front=NULL,*rear=NULL,*p,*temp;
void enqueue();
void display();
void dequeue();
void main()
{
clrscr();
while(1) {
int choice;
cout
-
7/31/2019 m.tech DSA
14/78
14
getch();
} }
void enqueue()
{
int item;
coutnext=NULL;
if(front==NULL && rear==NULL) {
front=p;
rear=p; }
else {
rear->next=p;
rear=p; }
}
void display()
{
if(front==NULL && rear==NULL)
cout
-
7/31/2019 m.tech DSA
15/78
15
else {
cout
-
7/31/2019 m.tech DSA
16/78
16
Program 6: WAP to implement circular queue.
#include
#include
#include
#define MAX 10
void cenqueue(int *,int,int *,int *);
void cdequeue(int *,int *,int *);
void cqdisplay(int *,int *,int *);
void main()
{
clrscr();
int i,front=-1,rear=-1;
int cqueue[MAX];
int choice,item;
while(1) {
cout
-
7/31/2019 m.tech DSA
17/78
17
else {
cout
-
7/31/2019 m.tech DSA
18/78
18
cout
-
7/31/2019 m.tech DSA
19/78
19
Program 7: WAP to implement singly linear linked list.
#include
#include
#include
#include
struct node
{
int info;
struct node *next;
}; struct node *head,*p,*temp;
void ins_at_beg();
void ins_at_end();
void ins_after_ele();
void ins_before_ele();
void delete_at_beg();
void delete_at_end();
void delete_after_ele();
void delete_before_ele();
void display();
void main()
{
clrscr();
while(1) {
int choice;
cout
-
7/31/2019 m.tech DSA
20/78
20
cout
-
7/31/2019 m.tech DSA
21/78
21
}
getch(); } }
void ins_at_beg()
{
int item;
coutnext=head;
head=p;
}
void display()
{
temp=head;
if(temp==NULL) {
cout
-
7/31/2019 m.tech DSA
22/78
22
else {
while(temp->next!=NULL) {
temp=temp->next; }
temp->next=p; }
}
void ins_after_ele()
{
int item,ele;
temp=head;
coutnext=temp->next;
temp->next=p; }
else {
cout
-
7/31/2019 m.tech DSA
23/78
23
head=p; }
else {
temp=head;
while(temp->next->info!=ele && temp!=NULL) {
temp=temp->next; }
if(temp!=NULL) {
p->next=temp->next;
temp->next=p; }
else
coutnext=NULL; }
}
void delete_after_ele()
{
int ele;
cout
-
7/31/2019 m.tech DSA
24/78
24
temp=temp->next; }
if(temp!=NULL) {
temp->next=temp->next->next; }
else
coutnext->next->info!=ele && temp!=NULL) {
temp=temp->next; }
if(temp!=NULL) {
temp->next=temp->next->next; }
else {
cout
-
7/31/2019 m.tech DSA
25/78
25
Output:
Enter ur choice
1.insertion
2.deletion
3.display
4.exit
1
Enter ur choice
1.insertion at beginning
2.insertion at end
3.insertion after given element4.insertion before given element
1
Enter the item to insert: 1
Enter ur choice
1.insertion
2.deletion
3.display
4.exit
1
Enter ur choice
1.insertion at beginning
2.insertion at end
3.insertion after given element
4.insertion before given element
2
Enter the element to insert at end: 2
The linked list is
1
2
-
7/31/2019 m.tech DSA
26/78
26
Program 8: WAP to implement singly circular linked list.
#include
#include
#include
#include
struct node
{
int info;
struct node *next;
} ;
struct node *head,*p,*temp;void display();
void ins_at_beg();
void ins_at_end();
void ins_after_ele();
void ins_before_ele();
void del_at_beg();
void del_at_end();
void del_after_ele();
void del_before_ele();
void main()
{
clrscr();
int choice;
while(1) {
cout
-
7/31/2019 m.tech DSA
27/78
27
cout
-
7/31/2019 m.tech DSA
28/78
28
cout
-
7/31/2019 m.tech DSA
29/78
29
cin>>item;
p=(struct node*)malloc(sizeof(struct node));
p->info=item;
if(head==NULL) {
head=p;
p->next=head; }
else if(head->next==head) {
head->next=p;
p->next=head; }
else {
temp=head;
while(temp->next!=head) {
temp=temp->next; }
temp->next=p;
p->next=head; } }
void ins_after_ele()
{
int item,ele;
coutnext;
while(temp->info!=ele && temp!=head) {
temp=temp->next; }
-
7/31/2019 m.tech DSA
30/78
30
if(temp!=head) {
p->next=temp->next;
temp->next=p; }
else
coutnext; }
if(temp!=head) {
p->next=temp->next;
temp->next=p; } } } }
void del_at_beg()
{
-
7/31/2019 m.tech DSA
31/78
31
if(head==NULL)
coutnext=head->next;
head=head->next; }
}
void del_at_end()
{
if(head==NULL)
coutnext; }
temp->next=temp->next->next;
temp=head->next; }
}
void del_after_ele()
{
int ele;
if(head==NULL) {
cout
-
7/31/2019 m.tech DSA
32/78
32
else {
temp=head;
while(temp->info!=ele && temp->next!=head) {
temp=temp->next; }
if(temp!=head) {
temp->next=temp ->next->next; }
else
temp->next=head->next;
head=head->next; } } }
void del_before_ele()
{
int ele;
if(head==NULL) {
coutnext;
while(temp->next->next->info!=ele && temp->next!=head) {
temp=temp->next; }
if(temp!=head) {
temp->next=temp->next->next; }
else
cout
-
7/31/2019 m.tech DSA
33/78
33
Output:
Enter ur choice:
1.insertion
2.deletion
3.display
4.exit
1
Enter your choice:
1.insertion at beginning
2.insertion at end
3.insertion after element
4.insertion before given element
2
Enter the item to insert at end
11
Enter ur choice:
1.insertion
2.deletion
3.display
4.exit
1
Enter your choice:
1.insertion at beginning
2.insertion at end
3.insertion after element
4.insertion before given element
2
Enter the item to insert at end
12
Elements in list are:
11 12
-
7/31/2019 m.tech DSA
34/78
34
Program 9: WAP to implement Doubly linear linked list.
#include
#include
struct dllist {
int number;
struct dllist *next;
struct dllist *prev;
};
struct dllist *head, *tail;
void append_node(struct dllist *lnode);
void insert_node(struct dllist *lnode, struct dllist *after);
void remove_node(struct dllist *lnode);
int main(void) {
struct dllist *lnode;
int i = 0;
/* add some numbers to the double linked list */
for(i = 0; i number = i;
append_node(lnode);
}
/* print the dll list */
for(lnode = head; lnode != NULL; lnode = lnode->next) {
printf("%d\n", lnode->number);
}
/* destroy the dll list */
while(head != NULL)
remove_node(head);
-
7/31/2019 m.tech DSA
35/78
35
return 0;
}
void append_node(struct dllist *lnode) {
if(head == NULL) {
head = lnode;
lnode->prev = NULL;
}
else
{
tail->next = lnode;
lnode->prev = tail;
}
tail = lnode;
lnode->next = NULL;
}
void insert_node(struct dllist *lnode, struct dllist *after) {
lnode->next = after->next;
lnode->prev = after;
if(after->next != NULL)
after->next->prev = lnode;
else
tail = lnode;
after->next = lnode;
}
void remove_node(struct dllist *lnode) {
if(lnode->prev == NULL)
head = lnode->next;
else
-
7/31/2019 m.tech DSA
36/78
36
lnode->prev->next = lnode->next;
if(lnode->next == NULL)
tail = lnode->prev;
else
lnode->next->prev = lnode->prev;
}
-
7/31/2019 m.tech DSA
37/78
37
Program 10: WAP to implement Doubly circular linked list.
#include
#include
struct node
{
struct node *prev,*next;
int data;
};
void create_list(struct node *list,int n)
{
struct node *n1,*n2;
int i;
n1=list;
printf("enter the data for the 1 node:");
scanf("%d",&list->data);
for(i=1;inext=(struct node *)malloc(sizeof(struct node));
if(list->next==NULL)
{
printf("malloc not done");
exit(0);
}
n2=list;
list=list->next;
list->prev=n2;
printf("enter the data for the %d node",i+1);
-
7/31/2019 m.tech DSA
38/78
38
scanf("%d",&list->data);
}
list->next=n1;
n1->prev=list;
}
void print_list(struct node *list)
{
struct node *temp;
temp=list;
if(list->next=temp)
{
printf("%d",list->data);
}
if(list->next!=temp)
{
printf("%d",list->data);
if(list->next->next==temp)
printf("%d",list->next->data);
else
print_list(list->next);
}
}
int main()
{
int n;
struct node *head;
head=(struct node *)malloc(sizeof(struct node));
if(head==NULL)
-
7/31/2019 m.tech DSA
39/78
39
{
printf("space unallocated");
exit (0);
}
printf("enter the no of nodes:");
scanf("%d",&n);
create_list(head,n);
print_list(head);
return 0;
}
-
7/31/2019 m.tech DSA
40/78
40
Program 11: WAP to implement Binary Search tree
#include
#include
#include
struct node
{
int data;
struct node *left, *right;
};
typedef struct node node;
non recursive method :
main()
{
node * root = NULL , *new = NULL *temp1 =NULL , * temp2 = NULL;
int num =1;
printf(" Enter the elements of the tree( enter 0 to exit)\n");
while(1)
{
scanf("%d", &num);
if(num==0)
break;
new = malloc(sizeof(node));
new->left = new->right = NULL;
new->data = num;
if( root == NULL)
root = new;
else
{
temp1 = root;
while(temp1!=NULL)
{
temp2 = temp1;
if( new->data > temp1->data )
temp1 = temp1->right;
else
temp1 = temp1->left;
}
if( new->data > temp2->data )
-
7/31/2019 m.tech DSA
41/78
41
temp2->right = new;
else
temp2->left = new;
}
}
}
void main()
{
node * root = NULL , *new = NULL ;
int num =1;
printf(" Enter the elements of the tree( enter 0 to exit)\n");
while(1)
{
scanf("%d", &num);
if(num==0)
break;
new = malloc(sizeof(node));
new->left = new->right = NULL;
new->data = num;
if( root == NULL)
root = new;
else
{
insert(new,root);}
}
}
void insert( node * new , node *root)
{
if( new->data > root->data)
{
if( root->right == NULL)
root->right = new;
else
insert( new,root->right);
}
if( new->data < root->data)
{
if( root->left == NULL)
root->left = new;
else
insert( new,root->left);
}
}
-
7/31/2019 m.tech DSA
42/78
42
Output:
Specify the number of items to be inserted: 5
Enter the data: 4
Enter the data: 2
Enter the data: 6
Enter the data: 4
Enter the data: 1
Inorder traversal: 12446
Preorder traversal: 42164
Postorder traversal: 12464
Binary tree before deletion: 12446
Enter the node to delete: 4
Binary tree after deletion: 1246
-
7/31/2019 m.tech DSA
43/78
43
Program 12: WAP to implement AVL tree
# include
# include
# define F 0
# define T 1
struct NODE
{
char Info;
int Flag;
struct NODE *Left_Child;
struct NODE *Right_Child;
};
struct NODE *Binary_Tree (char , struct NODE *, int *);
void Output(struct NODE *, int );
struct NODE *Balance_Right_Heavy(struct NODE *, int *);
struct NODE *Balance_Left_Heavy(struct NODE *, int *);
struct NODE *DELETE(struct NODE *, struct NODE *, int *);
struct NODE *Delete_Element(struct NODE *, char , int *);
struct NODE * Binary_Tree (char Info, struct NODE *Parent, int *H)
{
struct NODE *Node1;
struct NODE *Node2;
if(!Parent)
{
Parent = (struct NODE *) malloc(sizeof(struct NODE));
Parent->Info = Info;
Parent->Left_Child = NULL;
Parent->Right_Child = NULL;
Parent->Flag = 0;
*H = T;
return (Parent);
}
if(Info < Parent->Info)
-
7/31/2019 m.tech DSA
44/78
44
{
Parent->Left_Child = Binary_Tree(Info, Parent->Left_Child, H);
if(*H)
{
switch(Parent->Flag)
{
case 1: Parent->Flag = 0;
*H = F;
break;
case 0:
Parent->Flag = -1;
break;case -1
Node1 = Parent->Left_Child;
if(Node1->Flag == -1)
{
printf("\n Left to Left Rotation\n");
Parent->Left_Child= Node1->Right_Child;
Node1->Right_Child = Parent;
Parent->Flag = 0;
Parent = Node1;
}
else
{
printf("\n Left to right rotation\n");
Node2 = Node1->Right_Child;
Node1->Right_Child = Node2->Left_Child;
Node2->Left_Child = Node1;
Parent->Left_Child = Node2->Right_Child;
Node2->Right_Child = Parent;
if(Node2->Flag == -1)
Parent->Flag = 1;
else
Parent->Flag = 0;
if(Node2->Flag == 1)
Node1->Flag = -1;
-
7/31/2019 m.tech DSA
45/78
45
else
Node1->Flag = 0;
Parent = Node2;
}
Parent->Flag = 0;
*H = F;
}
}
}
if(Info > Parent->Info){
Parent->Right_Child = Binary_Tree(Info, Parent->Right_Child, H);
if(*H)
{
switch(Parent->Flag)
{
case -1:
Parent->Flag = 0;
*H = F;
break;
case 0: Parent->Flag = 1;
break;
case 1:
Node1 = Parent->Right_Child;
if(Node1->Flag == 1)
{
printf("\n Right to Right Rotation\n");
Parent->Right_Child= Node1->Left_Child;
Node1->Left_Child = Parent;
Parent->Flag = 0;
Parent = Node1;
}
else
-
7/31/2019 m.tech DSA
46/78
46
{
printf("\n Right to Left Rotation\n");
Node2 = Node1->Left_Child;
Node1->Left_Child = Node2->Right_Child;
Node2->Right_Child = Node1;
Parent->Right_Child = Node2->Left_Child;
Node2->Left_Child = Parent;
if(Node2->Flag == 1)
Parent->Flag = -1;
else
Parent->Flag = 0;if(Node2->Flag == -1)
Node1->Flag = 1;
else
Node1->Flag = 0;
Parent = Node2;
}
Parent->Flag = 0;
*H = F;
}
}
}
return(Parent);
}
void Output(struct NODE *Tree,int Level)
{
int i;
if (Tree)
{
Output(Tree->Right_Child, Level+1);
printf("\n");
for (i = 0; i < Level; i++)
printf(" ");
printf("%c", Tree->Info);
-
7/31/2019 m.tech DSA
47/78
47
Output(Tree->Left_Child, Level+1);
}
}
struct NODE * Balance_Right_Heavy(struct NODE *Parent, int *H)
{
struct NODE *Node1, *Node2;
switch(Parent->Flag)
{
case -1:
Parent->Flag = 0;
break;
case 0:
Parent->Flag = 1;
*H= F;
break;
case 1:
Node1 = Parent->Right_Child;
if(Node1->Flag >= 0)
{
printf("\n Right to Right Rotation\n");
Parent->Right_Child= Node1->Left_Child;
Node1->Left_Child = Parent;
if(Node1->Flag == 0)
{
Parent->Flag = 1;
Node1->Flag = -1;
*H = F;
}
else
{
Parent->Flag = Node1->Flag = 0;
}
-
7/31/2019 m.tech DSA
48/78
48
Parent = Node1;
}
else
{
printf("\n Right to Left Rotation\n");
Node2 = Node1->Left_Child;
Node1->Left_Child = Node2->Right_Child;
Node2->Right_Child = Node1;
Parent->Right_Child = Node2->Left_Child;
Node2->Left_Child = Parent;
if(Node2->Flag == 1)Parent->Flag = -1;
else
Parent->Flag = 0;
if(Node2->Flag == -1)
Node1->Flag = 1;
else
Node1->Flag = 0;
Parent = Node2;
Node2->Flag = 0;
}
}
return(Parent);
}
struct NODE * Balance_Left_Heavy(struct NODE *Parent, int *H)
{
struct NODE *Node1, *Node2;
switch(Parent->Flag)
{
case 1:
Parent->Flag = 0;
break;
-
7/31/2019 m.tech DSA
49/78
49
case 0:
Parent->Flag = -1;
*H= F;
break;
case -1:
Node1 = Parent->Left_Child;
if(Node1->Flag Left_Child= Node1->Right_Child;
Node1->Right_Child = Parent;if(Node1->Flag == 0)
{
Parent->Flag = -1;
Node1->Flag = 1;
*H = F;
}
else
{
Parent->Flag = Node1->Flag = 0;
}
Parent = Node1;
}
else
{
printf("\n Left to Right Rotation\n");
Node2 = Node1->Right_Child;
Node1->Right_Child = Node2->Left_Child;
Node2->Left_Child = Node1;
Parent->Left_Child = Node2->Right_Child;
Node2->Right_Child = Parent;
if(Node2->Flag == -1)
Parent->Flag = 1;
else
-
7/31/2019 m.tech DSA
50/78
50
Parent->Flag = 0;
if(Node2->Flag == 1)
Node1->Flag = -1;
else
Node1->Flag = 0;
Parent = Node2;
Node2->Flag = 0;
}
}
return(Parent);
}struct NODE * DELETE(struct NODE *R, struct NODE *Temp, int *H)
{
struct NODE *Dnode = R;
if( R->Right_Child != NULL)
{
R->Right_Child = DELETE(R->Right_Child, Temp, H);
if(*H)
R = Balance_Left_Heavy(R, H);
}
else
{
Dnode = R;
Temp->Info = R->Info;
R = R->Left_Child;
free(Dnode);
*H = T;
}
return(R);
}
struct NODE * Delete_Element(struct NODE *Parent, char Info, int *H)
{
struct NODE *Temp;
if(!Parent)
{
-
7/31/2019 m.tech DSA
51/78
51
printf("\n Information does not exist");
return(Parent);
}
else
{
if (Info < Parent->Info )
{
Parent->Left_Child = Delete_Element(Parent>Left_Child, Info, H);
if(*H)
Parent = Balance_Right_Heavy(Parent, H);
}
elseif(Info > Parent->Info)
{
Parent->Right_Child=Delete_Element(Parent->Right_Child, Info, H);
if(*H)
Parent =Balance_Left_Heavy(Parent, H);
}
else
{
Temp= Parent;
if(Temp->Right_Child == NULL)
{
Parent = Temp->Left_Child;
*H = T;
free(Temp);
}
else
if(Temp->Left_Child == NULL)
{
Parent = Temp>Right_Child;
*H = T;
free(Temp);
}
else
{
-
7/31/2019 m.tech DSA
52/78
52
Temp->Left_Child =DELETE(Temp->Left_Child, Temp, H);
if(*H)
Parent =Balance_Right_Heavy(Parent, H);
}
}
}
return(Parent);
}
void main()
{
int H;
char Info ;char choice;
struct NODE *Tree = (struct NODE *)malloc(sizeof(struct NODE));
Tree = NULL;
printf("\n Input choice 'b' to break:"); choice = getchar();
while(choice != 'b')
{
fflush(stdin);
printf("\n Input information of the node: ");
scanf("%c", &Info);
Tree = Binary_Tree(Info, Tree, &H);
printf("\n Tree is:\n");
Output(Tree, 1);
fflush(stdin);
printf("\n Input choice 'b' to break:");
choice = getchar();
}
fflush(stdin);
while(1)
{
printf("\n Input choice 'b' to break:");
printf("\n Input the key value want to deletedir:");
scanf("%c", &Info);
if (Info == 'b')
break;
-
7/31/2019 m.tech DSA
53/78
53
Tree = Delete_Element(Tree, Info, &H);
printf("\n Tree is:\n");
Output(Tree, 1);
}
}
Output:
Left rotation along 6
AVL tree:
5 6 10 12 13 15 20 25 27 29 32
AVL tree after deletion of a node:
5 6 10 13 15 25 27 29 32
-
7/31/2019 m.tech DSA
54/78
54
Program 13: WAP to implement B Tree
#include
#include
#include
# define MAX 4
# define MIN 2
struct btnode {
int count;
int value[MAX+1];
struct btnode *child[MAX+1];
};struct btnode *insert(int,struct btnode *);
int setval(int,struct btnode *,int *,struct btnode **);
struct btnode *search(int,struct btnode *,int *);
int searchnode(int,struct btnode *,int *);
void fillnode(int,struct btnode *,struct btnode *,int);
void split(int,struct btnode *,struct btnode *,int,int *,struct btnode **);
struct btnode *delet(int,struct btnode *);
int delheap(int,struct btnode *);
void clear(struct btnode*,int);
void copysucc(struct btnode *,int);
void restore(struct btnode *,int);
void rightshift(struct btnode *,int);
void leftshift(struct btnode *,int);
void merge(struct btnode *,int);
void display(struct btnode*);
void main() {
struct btnode *root;
root=NULL;
clrscr();
root=insert(27,root);
-
7/31/2019 m.tech DSA
55/78
55
root=insert(42,root);
root=insert(22,root);
root=insert(47,root);
root=insert(32,root);
root=insert(2 ,root);
root=insert(51,root);
root=insert(40,root);
root=insert(13,root);
coutchild[1]=c;
return n;
}
return root; }
int setval(int val,struct btnode *n,int *p,struct btnode **c) {
int k;
if(n==NULL)
{
-
7/31/2019 m.tech DSA
56/78
-
7/31/2019 m.tech DSA
57/78
57
while((valvalue[*pos])&& *pos>1)
(*pos)--;
if(val==n->value[*pos])
return 1;
else
return 0; } }
void fillnode(int val,struct btnode *c,struct btnode *n,int k) {
int i;
for(i=n->count;i>k;i--)
{
n->value[i+1]=n->value[i];
n->child[i+1]=n->child[i];
}
n->value[k+1]=val;
n->child[k+1]=c;
n->count++; }
void split(int val,struct btnode *c,struct btnode *n,int k,int *y,struct btnode **newnode)
{
int i,mid;
if(kvalue[i];
(*newnode)->child[i-mid]=n->child[i];
}
(*newnode)->count=MAX-mid;
n->count=mid;
if(k
-
7/31/2019 m.tech DSA
58/78
58
*y=n->value[n->count];
(*newnode)->child[0]=n->child[n->count];
n->count--;
}
struct btnode *delet(int val,struct btnode *root) {
struct btnode *temp;
if(!delheap(val,root))
cout
-
7/31/2019 m.tech DSA
59/78
59
if(root->child[i]->countvalue[i-1]=node->value[i];
node->child[i-1]=node->child[i];
}
node->count--; }
void copysucc(struct btnode *node,int i)
{
struct btnode *temp;
temp=node->child[i];
while(temp->child[0])
temp=temp->child[0];
node->value[i]=temp->value[1];
}
void restore(struct btnode *node,int i)
{
if(i==0) {
if(node->child[1]->count>MIN)
leftshift(node,1);
else
merge(node,1); }
else {
if(i==node->count) {
if(node->child[i-1]->count>MIN)
rightshift(node,i);
else
merge(node,i); }
else {
if(node->child[i-1]->count>MIN)
-
7/31/2019 m.tech DSA
60/78
60
rightshift(node,i);
else {
if(node->child[i+1]->count>MIN)
leftshift(node,i+1);
else
merge(node,i); } } } }
void rightshift(struct btnode *node,int k)
{
int i;
struct btnode *temp;
temp=node->child[k];
for(i=temp->count;ivalue[i+1]=temp->value[i];
temp->child[i+1]=temp->child[i];
}
temp->child[1]=temp->child[0];
temp->count++;
temp->value[1]=node->value[k];
temp=node->child[k-1];
node->value[k]=temp->value[temp->count];
node->child[k]->child[0]=temp->child[temp->count];
temp->count--; }
void leftshift(struct btnode *node,int k)
{
int i;
struct btnode *temp;
temp=node->child[k-1];
temp->count++;
temp->value[temp->count]=node->value[k];
temp->child[temp->count]=node->child[k]->child[0];
temp=node->child[k];
node->value[k]=temp->value[1];
temp->child[0]=temp->child[1];
-
7/31/2019 m.tech DSA
61/78
61
temp->count--;
for(i=1;icount;i++)
{
temp->value[i]=temp->value[i+1];
temp->child[i]=temp->child[i+1];
} }
void merge(struct btnode *node,int k)
{
int i;
struct btnode *temp1,*temp2;
temp1=node->child[k];
temp2=node->child[k-1];
temp2->count++;
temp2->value[temp2->count]=node->value[k];
temp2->child[temp2->count]=node->child[0];
for(i=1;icount;i++)
{
temp2->count++;
temp2->value[temp2->count]=temp1->value[i];
temp2->child[temp2->count]=temp1->child[i];
}
for(i=k;icount;i++)
{
node->value[i]=node->value[i+1];
node->child[i]=node->child[i+1];
}
node->count--;
free(temp1);
}
void display(struct btnode *root)
{
int i;
if(root!=NULL) {
for(i=0;icount;i++)
-
7/31/2019 m.tech DSA
62/78
62
{
display(root->child[i]);
cout
-
7/31/2019 m.tech DSA
63/78
63
Program14: WAP to sort a given list using insertion sort
#include
#include
#include
#define max 20
void main()
{
int a[max],i,j,k=0,s,n;
clrscr();
printf("\n Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i
-
7/31/2019 m.tech DSA
64/78
64
getch();
}
Output:
Enter the no. of elements:
5 4 1 7 6
Array before sorting:
5 4 1 7 6
After after sorting:
1 4 5 6 7
-
7/31/2019 m.tech DSA
65/78
65
Program 15: WAP to sort a given list using selection sort
#include
#include
void swap(int *x,int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
void selection_sort(int list[], int n)
{
int i, j, min;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i+1; j < n; j++)
{if (list[j] < list[min])
{
min = j;
}
}
swap(&list[i], &list[min]);
}
}
void printlist(int list[],int n)
{
int i;
for(i=0;i
-
7/31/2019 m.tech DSA
66/78
66
{
const int MAX_ELEMENTS = 10;
int list[MAX_ELEMENTS];
int i = 0;
// generate random numbers and fill them to the list
for(i = 0; i < MAX_ELEMENTS; i++ ){
list[i] = rand();
}
printf("The list before sorting is:\n");
printlist(list,MAX_ELEMENTS);
// sort the list
selection_sort(list,MAX_ELEMENTS);
// print the result
printf("The list after sorting:\n");
printlist(list,MAX_ELEMENT;
}
Output:
Enter the no. of elements:
5 4 1 7 6
Array before sorting:
5 4 1 7 6
After after sorting:
1 4 5 6 7
-
7/31/2019 m.tech DSA
67/78
67
Program 16: WAP to sort a given list using quick sort
#include
#include
void quicksort(int* array, int left, int right)
{
if(left >= right)
return;
int index = partition(array, left, right);
quicksort(array, left, index - 1);
quicksort(array, index + 1, right);
}int partition(int* array, int left, int right)
{
findMedianOfMedians(array, left, right);
int pivotIndex = left, pivotValue = array[pivotIndex], index = left, i;
swap(array, pivotIndex, right);
for(i = left; i < right; i++)
{
if(array[i] < pivotValue)
{
swap(array, i, index);
index += 1;
}
}
swap(array, right, index);
return index;
}
int findMedianOfMedians(int* array, int left, int right)
{
if(left == right)
-
7/31/2019 m.tech DSA
68/78
68
return array[left];
int i, shift = 1;
while(shift
-
7/31/2019 m.tech DSA
69/78
69
//Swap integer values by array indexes
void swap(int * array, int a, int b)
{
int tmp = array[a];
array[a] = array[b];
array[b] = tmp;
}
Output:
Enter size of array: 5
Enter 5 elements: 4
3
6
1
8
Sorted elements: 1 3 4 6 8
-
7/31/2019 m.tech DSA
70/78
70
Program 17: WAP to sort a given list using merge sort
#include
#include
Void mergesort(int *,int);
Void main()
{
Int x[max],n,i,j;
Char ans;
Clrscr();
Coutn;
For(i=0;i
-
7/31/2019 m.tech DSA
71/78
71
List2=list1+size;
U1=list2-1;
U2=((list2+size-1)
-
7/31/2019 m.tech DSA
72/78
72
Output:
No. of elements in first array: 4
No. of elements in second array: 4
First array:
3
2
4
1
Second array:
6
7
5
8
Array after sorting
1 2 3 4 5 6 7 8
-
7/31/2019 m.tech DSA
73/78
73
Program 18: WAP to sort a given list using radix sort
#include "stdio.h"
#define MAX 100
#define SHOWPASS
void print(int *a, int n) {
int i;
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
}
void radix_sort(int *a, int n) {int i, b[MAX], m = 0, exp = 1;
for (i = 0; i < n; i++) {
if (a[i] > m)
m = a[i];
}
while (m / exp > 0) {
int box[10] = { 0 };
for (i = 0; i < n; i++)
box[a[i] / exp % 10]++;
for (i = 1; i < 10; i++)
box[i] += box[i - 1];
for (i = n - 1; i >= 0; i--)
b[--box[a[i] / exp % 10]] = a[i];
for (i = 0; i < n; i++)
a[i] = b[i];
exp *= 10;
#ifdef SHOWPASS
printf("\n\nPASS : ");
print(a, n);
#endif
-
7/31/2019 m.tech DSA
74/78
74
}
}
int main() {
int arr[MAX];
int i, num;
printf("\nEnter total elements (num < %d) : ", MAX);
scanf("%d", &num);
printf("\nEnter %d Elements : ", num);
for (i = 0; i < num; i++)
scanf("%d", &arr[i]);
printf("\nARRAY : ");
print(&arr[0], num);
radix_sort(&arr[0], num);
printf("\n\nSORTED : ");
print(&arr[0], num);
return 0;
}
-
7/31/2019 m.tech DSA
75/78
75
Program 19: WAP to sort a given list using heap sort
#include
#include
void heapsort(int a[],int n);
void heapcreate(int a[],int n);
void adjust(int a[],int n);
int main()
{
int a[20],n,temp,i;
printf("enter number of elements :");
scanf("%d",&n);
printf("enter the elements\n");
for(i=0;i
-
7/31/2019 m.tech DSA
76/78
76
{
temp=a[0];
a[0]=a[i];
a[i]=temp;
adjust(a,i);
}
//return;
}
void heapcreate(int a[],int n)
{
int i,j,k,item;
for(k=1;k
{
item=a[k];
i=k;
j=(i-1)/2;
while(i>0&&item>a[j])
{
a[i]=a[j];
i=j;
j=(i-1)/2;
}
a[i]=item;
}
//return;
}
void adjust(int a[],int n)
{
-
7/31/2019 m.tech DSA
77/78
77
int i,j,item;
j=0;
item=a[j];
i=2*j+1;
while(i
-
7/31/2019 m.tech DSA
78/78
After heap sort
1 2 3 6 8