Post on 26-Oct-2014
S:NO PROGRAM NAME PAGE NO1. ARRAYS CREATION AND OPERATIONS
2(A). STACK OPERATION
2(B). QUEUE OPERATION
3. RECURSION,INFIX TO POSTFIX CONVERSION
4. POLYNOMIAL ADDITION USING SINGLY LINKED LIST
5. DOUBLY LINKED LIST OPERATIONS
6. TREE TRAVERSALS
7. GRAPH-SHORTEST PATH
8(A). LINEAR SEARCHING
8(B). BINARY SEARCHING
9. SORTING-SHELL,QUICK,HEAP,MERGE
PRACTICAL I: PROBLEM SOLVING IN C & DATA STRUCTURES LAB
1.ARRAYS CREATION AND OPERATIONS
#include<stdio.h>#include<conio.h>int a[20],n,pos = -1;int menu() {
int x;clrscr();printf("\n\nMain Menu");printf("\n\n\t1.Insert");printf("\n\n\t2.Delete");printf("\n\n\t3.List");printf("\n\n\t4.Exit");printf("\n\nEnter Your Choice : ");scanf("%d",&x);return x;
}void insert(int x) {
a[++pos] = x;}void delete(int x) {
int i,start;for(i = 0;i <= pos;i++) {
if(a[i] == x) {start = i;while(start <= n) {
a[start] = a[start + 1];start++;
}pos--;return ;
}}printf("\n\nNumber Not Found.....");getch();
}void list() {
int i;printf("\n\nItems in Array : ");for(i = 0;i <= pos;i++) {
printf("%d ",a[i]);}getch();
}void main() {
int ch,x;
clrscr();printf("\n\nEnter Array Size : ");scanf("%d",&n);while((ch = menu()) < 5) {
switch(ch) {case 1:
if(pos < (n - 1)) {printf("\n\nEnter a Number to insert : ");scanf("%d",&x);insert(x);
}else {
printf("\n\nArray Out of Limit.....");getch();
}break;
case 2:printf("\n\nEnter the Number to Delete : ");scanf("%d",&x);delete(x);break;
case 3:list();break;
case 4:default:
exit(0);}
}getch();
}
OUTPUT
Enter Array Size : 5
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to insert : 56
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to insert : 78
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to insert : 34
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 3
Items in Array : 56 78 34
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 2
Enter the Number to Delete : 56
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 3
Items in Array : 78 34
2(A).STACK OPERATION
#include<stdio.h>#include<conio.h>int stk[20],n,tos = -1;int menu() {
int x;clrscr();printf("\n\n\t\t\t\tSTACK OPERATION\n\nMain Menu");printf("\n\n\t1.Push");printf("\n\n\t2.Pop");printf("\n\n\t3.List");printf("\n\n\t4.Exit");printf("\n\nEnter Your Choice : ");scanf("%d",&x);return x;
}void push(int a) {
stk[++tos] = a;}int pop() {
int temp;temp = stk[tos];tos--;return temp;
}void list() {
int i;clrscr();printf("\n\nStack Elements : ");for(i = tos;i >= 0;i--) {
printf("%d ",stk[i]);}getch();
}void main() {
int ch,x;clrscr();printf("\n\nEnter Stack Size : ");scanf("%d",&n);while((ch = menu()) < 5) {
switch(ch) {case 1:
if(tos < (n - 1)) {printf("\n\nEnter a Noumber to Push : ");
scanf("%d",&x);push(x);
}else {
printf("\n\nStack Overflow......");getch();
}break;
case 2:if(tos == -1)
printf("\n\nStack Underflow......");else
printf("\n\nPopped Item : %d",pop());getch();break;
case 3:if(tos == -1)
printf("\n\nStack Underflow......");else
list();getch();break;
case 4:default:
exit(0);}
}getch();
}
OUTPUT
Enter Stack Size : 5
STACK OPERATION
Main Menu
1.Push
2.Pop
3.List
4.Exit
Enter Your Choice : 1
Enter a Noumber to Push : 23
STACK OPERATION
Main Menu
1.Push
2.Pop
3.List
4.Exit
Enter Your Choice : 1
Enter a Noumber to Push : 12
STACK OPERATION
Main Menu
1.Push
2.Pop
3.List
4.Exit
Enter Your Choice : 3
Stack Elements : 12 23
STACK OPERATION
Main Menu
1.Push
2.Pop
3.List
4.Exit
Enter Your Choice : 2
Popped Item : 12
2(B).QUEUE OPERATION
#include<stdio.h>#include<conio.h>int queue[20],n,front = -1,rear = -1;int menu() {
int x;clrscr();printf("\n\n\t\t\t\tQUEUE OPERATION\n\nMain Menu");printf("\n\n\t1.Insert");printf("\n\n\t2.Delete");printf("\n\n\t3.List");printf("\n\n\t4.Exit");printf("\n\nEnter Your Choice : ");scanf("%d",&x);return x;
}void insert(int a) {
if((front == -1) && (rear == -1)) {front = rear = 0;queue[rear] = a;
}else
queue[++rear] = a;}int delete() {
int temp,i;temp = queue[front];for(i = 0;i < rear;i++) {
queue[i] = queue[i + 1];}rear--;return temp;
}void list() {
int i;clrscr();printf("\n\nQueue Elements : rear->");for(i = rear;i >= front;i--) {
printf("%d ",queue[i]);}printf("<-front");
}void main() {
int ch,x;clrscr();
printf("\n\nEnter Queue Size : ");scanf("%d",&n);while((ch = menu()) < 5) {
switch(ch) {case 1:
if(rear < (n - 1)) {printf("\n\nEnter a Number to Insert : ");scanf("%d",&x);insert(x);
}else {
printf("\n\nQueue Full......");getch();
}break;
case 2:if(front == rear)
printf("\n\nQueue Empty......");else
printf("\n\nDeleted Item : %d",delete());getch();break;
case 3:if(front == rear)
printf("\n\nQueue Empty......");else
list();getch();break;
case 4:default:
exit(0);}
}getch();
}
OUTPUT
Enter Queue Size : 5
QUEUE OPERATION
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to Insert : 30
QUEUE OPERATION
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to Insert : 97
QUEUE OPERATION
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to Insert : 55
QUEUE OPERATION
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 3
Queue Elements : rear->55 97 30 <-front
QUEUE OPERATION
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 2
Deleted Item : 30
3.RECURSION,INFIX TO POSTFIX CONVERSION
#include<stdio.h>#include<conio.h>void main() {
char infix[40],postfix[40],oper[20];int i,m = 0,k = 0;clrscr();printf("\n\nEnter the Infix Expression : ");gets(infix);for(i = 0;infix[i]!='\0';i++) {
switch(infix[i]) {case '+':case '-':case '*':case '/':case '^':case '(':
oper[++k] = infix[i];break;
case ')':while(oper[k]!='(' && k!=0) {
postfix[m++] = oper[k--];k--;
}break;
default:postfix[m++] = infix[i];
}}while(k > 0) {
postfix[m++] = oper[k--];}postfix[m] = '\0';printf("\n\nPostfix Expression : %s",postfix);getch();
}
OUTPUT
Enter the Infix Expression : (a+b)*c
Postfix Expression : ab+c*
4.POLYNOMIAL ADDITION USING SINGLY LINKED LIST
#include<stdio.h>#include<conio.h>struct apoly {
int coeff;int power;struct apoly *next;
}*afirst,*alast,*bfirst,*blast,*cfirst,*clast,*t;void create(int op) {
t=(struct apoly *)malloc(sizeof(struct apoly));printf("\n\nCoeff : ");scanf("%d",&t->coeff);printf("\n\nPower : ");scanf("%d",&t->power);t->next=NULL;if(op==1) {
if(afirst==NULL)afirst=t;elsealast->next=t;alast=t;
}else {
if(bfirst==NULL)bfirst=t;elseblast->next=t;blast=t;
}}void result(int c,int p) {
t=(struct apoly *)malloc(sizeof(struct apoly));t->coeff=c;t->power=p;t->next=NULL;if(cfirst==NULL)
cfirst=t;else
clast->next=t;clast=t;
}void addition(struct apoly *a,struct apoly *b) {
while(a!=NULL && b!= NULL) {
if(a->power>b->power) {result(a->coeff,a->power);a=a->next;
}else if(a->power<b->power) {
result(b->coeff,b->power);b=b->next;
}else {
result(a->coeff+b->coeff,a->power);a=a->next;b=b->next;
}}while(a!=NULL) {
result(a->coeff,a->power);a=a->next;
}while(b!=NULL) {
result(b->coeff,b->power);b=b->next;
}}void display(struct apoly *c) {
int p=0;while(c!=NULL) {
if(c->coeff>0&&p!=0)printf("+");printf("%d",c->coeff);
if(c->power!=1&&c->power!=0)printf("X^%d",c->power);if(c->power==1)printf("X");p=1;c=c->next;
}printf(" = 0");
}void main() {
int n,i;clrscr();printf("\n\nEnter the no of terms for polnominal A : ");scanf("%d",&n);printf("\n\nEnter Polynomial A.......");for(i=1;i<=n;i++) {
printf("\n\nTerm : %d",i);create(1);
}printf("\n\nEnter the no of terms for polynomial B : ");
scanf("%d",&n);printf("\n\nEnter Polynomial B.........");for(i=1;i<=n;i++) {
printf("\n\nTerm : %d",i);create(2);
}addition(afirst,bfirst);printf("\n\nThe given Polynomials.... ");printf("\n\nA : "); display(afirst);printf("\n\nB : "); display(bfirst);printf("\n\nThe Resultant Polynomial : ");display(cfirst);getch();
}
OUTPUT
Enter the no of terms for polynomial A : 3
Enter Polynomial A.........
Term : 1
Coeff : 3
Power : 2
Term : 2
Coeff : 6
Power : 1
Term : 3
Coeff : 12
Power : 0
Enter the no of terms for polynomial B : 3
Enter Polynomial B.........
Term : 1
Coeff : 6
Power : 2
Term : 2
Coeff : 1
Power : 1
Term : 3
Coeff : 5
Power : 0
The given Polynomials....
A : 3X^2+6X+12 = 0
B : 6X^2+1X+5 = 0
The Resultant Polynomial : 9X^2+7X+17 = 0
5.DOUBLY LINKED LIST OPERATIONS
#include<stdio.h>#include<conio.h>struct node {
int data;struct node *next,*prev;
};typedef struct node dnode;dnode *dlist = NULL,*trace;void insert(int d) {
dnode *nw = (dnode *)malloc(sizeof(dnode));nw->data = d;if(dlist == NULL) {
dlist = nw;dlist->next = NULL;dlist->prev = NULL;
}else {
nw->next = dlist;dlist->prev = nw;nw->prev = NULL;dlist = nw;
}}void delete(int d) {
if(dlist->data == d) {dlist = dlist->next;dlist->prev = NULL;printf("\n\nElement Deleted.......");return;
}trace = dlist->next;while(trace!=NULL) {
if(trace->data == d) {trace->prev->next = trace->next;printf("\n\nElement Deleted.......");return;
}trace = trace->next;
}printf("\n\nElement not Found.......");
}void list() {
clrscr();printf("\n\nElements in the List : ");trace = dlist;while(trace!=NULL) {
printf("%d ",trace->data);trace = trace->next;
}getch();
}int menu() {
int x;clrscr();printf("\n\n\t\t\t\tDOUBLY LINKED LIST\n\nMain Menu");printf("\n\n\t1.Insert");printf("\n\n\t2.Delete");printf("\n\n\t3.List");printf("\n\n\t4.Exit");printf("\n\nEnter Your Choice : ");scanf("%d",&x);return x;
}void main() {
int ch,x;clrscr();while((ch = menu()) < 5) {
switch(ch) {case 1:
printf("\n\nEnter a Number to Insert : ");scanf("%d",&x);insert(x);break;
case 2:if(dlist!=NULL) {
printf("\n\nEnter the Number to Delete : ");scanf("%d",&x);delete(x);
}else
printf("\n\nList Empty......");getch();break;
case 3:if(dlist!=NULL)
list();else
printf("\n\nList Empty......");
getch();break;
case 4:default:
exit(0);}
}getch();
}
OUTPUT
DOUBLY LINKED LIST
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to Insert : 25
DOUBLY LINKED LIST
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to Insert : 84
DOUBLY LINKED LIST
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 1
Enter a Number to Insert : 77
DOUBLY LINKED LIST
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 3
Elements in the List : 77 84 25
DOUBLY LINKED LIST
Main Menu
1.Insert
2.Delete
3.List
4.Exit
Enter Your Choice : 2
Enter the Number to Delete : 25
Element Deleted.......
6.TREE TRAVERSALS
#include<stdio.h>#include<conio.h>typedef struct bin {
int d;struct bin *left,*right;
}node;void insert(node *,node *);void inorder(node *);void preorder(node *);void postorder(node *);node *getnode();void main() {
int ch;char ans='n';node *root,*new1;root=NULL;clrscr();do {
printf("\n******************************* ");printf("\n Tree Traversal ");printf("\n******************************* ");printf("\n\n1.Insert\n\n2.Inorder\n\n3.Preorder\n\n4.Postorder\n\n5.Exit");printf("\n\nEnter your Choice : ");scanf("%d",&ch);switch(ch) {
case 1:root = NULL;do {
new1=getnode();printf("\n\nEnter the Element : ");scanf("%d",&new1->d);if(root==NULL)
root=new1;else
insert(root,new1);printf("\n\nAdd More Elements ?(y/n) : ");ans=getch();
}while(ans=='y'||ans=='Y');clrscr();break;
case 2:if(root==NULL)
printf("\n\nTree is empty.....");else
inorder(root);break;
case 3:if(root==NULL)
printf("\n\nTree is empty.....");else
preorder(root);break;
case 4:if(root==NULL)
printf("\n\nTree is empty.....");else
postorder(root);break;
case 5:exit(0);
default: printf("\n\nWrong Choice.....");
}}while(ch!=5);getch();
}node *getnode() {
node *t;t=(node *)malloc(sizeof(node));t->left=NULL;t->right=NULL;return t;
}void insert(node *root,node *new1) {
char ch;printf("\n\nWhere to Insert %d left or right : ",root->d);ch=getch();
if((ch=='r')||(ch=='R')) {if(root->right==NULL) {
root->right=new1;}else
insert(root->right,new1);}else {
if(root->left==NULL) {root->left=new1;
}else
insert(root->left,new1);
}}void inorder(node *t) {
if(t!=NULL) {inorder(t->left);printf("\n\n%d",t->d);inorder(t->right);
}}void preorder(node *t) {
if(t!=NULL) {printf("\n\n%d",t->d);preorder(t->left);preorder(t->right);
}}void postorder(node *t) {
if(t!=NULL) {postorder(t->left);postorder(t->right);printf("\n\n%d",t->d);
}}
OUTPUT
******************************* Tree Traversal*******************************
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your Choice : 1
Enter the Element : 47
Add More Elements ?(y/n) : y
Enter the Element : 23
Where to Insert 47 left or right : l
Add More Elements ?(y/n) : y
Enter the Element : 78
Where to Insert 78 left or right : r
Add More Elements ?(y/n) : n
******************************* Tree Traversal*******************************
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your Choice : 2
23
47
78
******************************* Tree Traversal*******************************
1.Insert
2.Inorder
3.Preorder
4.Postorder
5.Exit
Enter your Choice : 4
23
78
47
7.GRAPH-SHORTEST PATH
#include<stdio.h>#include<conio.h>#define MAX 1000int dist[10][10],n;void showAdjacency();void calcPath();void showShortPath();void main() {
int x,i,j;clrscr();printf("\n\nEnter No.of Nodes in the Graph : ");scanf("%d",&n);printf("\n\nEnter Weights for the Following Path...........\n\n");for(i = 1;i <= n;i++) {
for(j = 1;j <= n;j++) {if(i!=j) {
printf("\n\n%d -> %d : ",i,j);scanf("%d",&x);dist[i][j] = x;
}else {
dist[i][j] = MAX;}
}}showAdjacency();calcPath();showShortPath();getch();
}void showAdjacency() {
int i,j;printf("\n\nAdjacency Matrix........\n\n");for(i = 1;i <= n;i++) {
for(j = 1;j <= n;j++) {printf("%6d",dist[i][j]);
}printf("\n\n");
}}void calcPath() {
int i,j,k;for(k = 1;k <= n;k++) {
for(i = 1;i <= n;i++) {for(j = 1;j <= n;j++) {
if(dist[i][j] > (dist[i][k] + dist[k][j])) {dist[i][j] = dist[i][k] + dist[k][j];
}}
}}
}void showShortPath() {
int i,s;printf("\n\nEnter the Source Node : ");scanf("%d",&s);printf("\n\nShortest Path.......");for(i = 2;i <= n;i++) {
printf("\n\n%d -> %d : %d",s,i,dist[s][i]);}
}
OUTPUT:
Enter No.of Nodes in the Graph : 3
Enter Weights for the Following Path...........
1 -> 2 : 2
1 -> 3 : 5
2 -> 1 : 3
2 -> 3 : 5
3 -> 1 : 6
3 -> 2 : 3
Adjacency Matrix........
1000 2 5
3 1000 5
6 3 1000
Enter the Source Node : 1
Shortest Path.......
1 -> 2 : 2
1 -> 3 : 5
8(A).LINEAR SEARCHING
#include<stdio.h>#include<conio.h>void main() {
int a[20],key,i,n;void linSearch(int [],int,int);clrscr();printf("\n\nEnter No.of Elements : ");scanf("%d",&n);printf("\n\nEnter %d Numbers : ",n);for(i = 0;i < n;i++) {
scanf("%d",&a[i]);}printf("\n\nEnter the Element to be Searched : ");scanf("%d",&key);linSearch(a,n,key);getch();
}void linSearch(int a[],int n,int k) {
int i;for(i = 0;i < n;i++) {
if(a[i] == k) {printf("\n\nElement Found at Position %d.....",(i + 1));return;
}}printf("\n\nElement Not Found......");
}
OUTPUT:
Enter No.of Elements : 6
Enter 6 Numbers : 246839
Enter the Element to be Searched : 8
Element Found at Position 4.....
8(B).BINARY SEARCHING
#include<stdio.h>#include<conio.h>int a[20],i,n;void main() {
int key;void binSearch(int);void sort();clrscr();printf("\n\nEnter No.of Elements : ");scanf("%d",&n);printf("\n\nEnter %d Numbers : ",n);for(i = 0;i < n;i++) {
scanf("%d",&a[i]);}printf("\n\nEnter the Element to be Searched : ");scanf("%d",&key);sort();binSearch(key);getch();
}void sort() {
int i,j,temp;for(i = 0;i < n;i++) {
for(j = i + 1;j < n;j++) {if(a[i] > a[j]) {
temp = a[i];a[i] = a[j];a[j] = temp;
}}
}}void binSearch(int k) {
int lower,upper,mid;lower = 0,upper = (n - 1);while(lower <= upper) {
mid = (lower + upper) / 2;
if(a[mid] == k) {printf("\n\nValue found at Position : %d.......",(mid + 1));return;
}else if(a[mid] > k) {
upper = mid - 1;}else if(a[mid] < k) {
lower = mid + 1;}
}printf("\n\nElement not Found.......");
}
OUTPUT:
Enter No.of Elements : 4
Enter 4 Numbers : 23673456
Enter the Element to be Searched : 3
Element not Found.......
9(A).SHELL SORTING
#include<stdio.h>#include<conio.h>int a[20],n;void main() {
int i;void shellSort();clrscr();printf("\n\nEnter Number of Elements : ");scanf("%d",&n);printf("\n\nEnter %d Numbers : ",n);for(i = 0;i < n;i++)
scanf("%d",&a[i]);shellSort();printf("\n\nSorted Numbers : ");for(i = 0;i < n;i++)
printf("%d ",a[i]);getch();
}void shellSort() {
int i,j,incr,temp;incr = 3;while(incr > 0) {
for(i = 0;i < n;i++) {j = i;temp = a[i];while((j >= incr) && (a[j - incr] > temp)) {
a[j] = a[j - incr];j = j - incr;
}a[j] = temp;
}if((incr / 2)!=0) incr = incr / 2;else if(incr == 1) incr = 0;else incr = 1;
}}
OUTPUT:
Enter Number of Elements : 4
Enter 4 Numbers : 35783
Sorted Numbers : 3 3 5 78
9(B).QUICK SORT
#include<stdio.h>#include<conio.h>int a[20],n;void main() {
int i;void quickSort(int, int);clrscr();printf("\n\nEnter Number of Elements : ");scanf("%d",&n);printf("\n\nEnter %d Numbers : ",n);for(i = 1;i <= n;i++)
scanf("%d",&a[i]);quickSort(1, n);printf("\n\nSorted Numbers : ");for(i = 1;i <= n;i++)
printf("%d ",a[i]);getch();
}void quickSort(int left, int right) {
int pivot,t,i,j;i = left;j = right;pivot = a[left];do {
while((a[i] < pivot) && (i < right))i++;
while((a[j] > pivot) && (j > left))j--;
if(i <= j) {t = a[i];a[i] = a[j];a[j] = t;i++;j--;
}}while(i <= j);if(i < right)
quickSort(i,right);if(left < j)
quickSort(left,j);}
OUTPUT
Enter Number of Elements : 5
Enter 5 Numbers : 56 12 78 33 9
Sorted Numbers : 9 12 33 56 78
9(C).HEAP SORT
/* Heap Sort */
#include<stdio.h>#include<conio.h>#include<alloc.h>
void main(){ int *x,i,n; int temp; void heap(int *,int); clrscr(); fflush(stdin); printf("Heap Sort"); printf("Enter How many Numbers : "); scanf("%d",&n); x = (int *)malloc(n * sizeof(int)); for(i=0;i<n;i++) {fflush(stdin); scanf("%d",&x[i]); } heap(x,n);
for(i=n-1;i>=1;i--) { temp = x[i]; x[i] = x[0]; x[0] = temp; heap(x,i-1); }
printf("Resultant Array "); for(i=0;i<n;i++) printf("%d ",x[i]); free(x); getch();}
void heap(int *a,int n){ int i,temp; for(i=n/2;i>=0;i--) {
if(a[(2*i)+1] < a[(2*i)+2] && (2*i+1)<=n && (2*i+2)<=n) { temp = a[(2*i)+1]; a[(2*i)+1] = a[(2*i)+2]; a[(2*i)+2] = temp; } if(a[(2*i)+1] > a[i] && (2*i+1)<=n && i<=n) { temp = a[(2*i)+1]; a[(2*i)+1] = a[i]; a[i] = temp; } }}
OUTPUT:
Heap SortEnter How many Numbers : 53445122365Resultant Array 12 23 34 45 65
9(D).MERGE SORT
#include<stdio.h>#include<conio.h>int a[20],b[20],n;void main() {
int i;void mergeSort(int, int);clrscr();printf("\n\nEnter Number of Elements : ");scanf("%d",&n);printf("\n\nEnter %d Numbers : ",n);for(i = 0;i < n;i++)
scanf("%d",&a[i]);mergeSort(0, n - 1);printf("\n\nSorted Numbers : ");for(i = 0;i < n;i++)
printf("%d ",a[i]);getch();
}void mergeSort(int lb, int ub) {
int m;void merge(int, int, int);if(lb < ub) {
m = (lb + ub) / 2;mergeSort(lb, m);mergeSort(m + 1, ub);merge(lb, m, ub);
}}void merge(int lb, int mid, int ub) {
int i,j,k,x;i = x = lb;j = mid + 1;while((x <= mid) && (j <= ub)) {
if(a[x] < a[j])b[i++] = a[x++];
elseb[i++] = a[j++];
}if(x > mid) {
for(k = j;k <= mid;k++)b[i++] = a[k];
}else {
for(k = x;k <= mid;k++)
b[i++] = a[k];}for(k = lb;k < i;k++)
a[k] = b[k];}
OUTPUT:
Enter Number of Elements : 5
Enter 5 Numbers : 2345344567
Sorted Numbers : 23 34 45 45 67