Home >> Sem 2 >> Programming Skills II(DS)
GUJARAT TECHNOLOGICAL UNIVERSITY
Subject Name : Programming Skills II(DS)
Subject Code : 620002
25. Write a program to create an inordered threaded binary tree.... GUJARAT TECHNOLOGICAL UNIVERSITY
Subject Name : Programming Skills II(DS)
Subject Code : 620002
Last Update : 03/June/2010
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
Shared By :your name Show/Hide Program
26. Write a program to create a graph in a adjacency matrix...
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
Shared By :your name Show/Hide Program
27. Write a program to create a graph in a adjacency,..
Shared By :mayank dudhatra (sun shine collage - rajkot ) Show/Hide Program
/* DEPTH FIRST SEARCH TECHNIQUE */
/* DFS.C */
# include<stdio.h>
# define size 20
# define T 1
# define F 0
struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertex_no;
char info;
int path_length;
struct Edge *Edge_Ptr;
};
void Table(int , int matrix [size][size], struct Vertex vert[size]);
struct Edge *Insert_Vertex (int , struct Edge *);
void DFS ( int , int *dist, struct Vertex vert [size]);
void Input(int, int a [size][size]);
void Output(int, int a [size][size]);
struct Edge * Insert_Vertex (int vertex_no, struct Edge *first)
{
struct Edge *new1, *current;
new1 = (struct Edge *) malloc(sizeof(struct Edge));
new1->terminal = vertex_no;
new1->next = NULL;
if (!first)
return (new1);
for (current = first; current->next; current = current->next);
current->next = new1;
return (first);
}
/* Initializing entries */
void Table(int vertex_num, int matrix [size][size],
struct Vertex vert[size])
{
int i, j;
for (i = 0; i < vertex_num; i++)
{
vert [i].visit = F;
vert [i].vertex_no = i+1;
vert [i].info = 'A'+ i;
vert [i].path_length = 0;
vert [i].Edge_Ptr = NULL;
}
for (i =0; i < vertex_num ; i++)
for (j =0; j < vertex_num ; j++)
if (matrix [i][j] > 0)
vert [i].Edge_Ptr = Insert_Vertex (j, vert [i].Edge_Ptr);
}
/* Computing path length */
void DFS ( int index, int *dist,
struct Vertex vert [size])
{
struct Edge *Link;
vert [index].visit = T;
vert [index].path_length = *dist;
*dist += 1;
for ( Link = vert [index].Edge_Ptr; Link; Link = Link->next)
if (vert [Link->terminal].visit == F)
DFS (Link->terminal, dist, vert);
}
/* Input function to read adjacency matrix */
void Input(int number, int a [size][size])
{
int i, j;
printf("\n Input the adjacency matrix \n");
for (i =0; i < number; i++)
{
for (j=0; j < number; j ++)
{
scanf("%d", &a [i][j]);
}
printf("\n");
}
}
/* Output function */
void Output(int number, int a [size][size])
{
int i, j;
printf("\n Adjacency matrix \n");
for (i = 0; i < number; i++)
{
for (j = 0; j < number; j ++)
{
printf(" %d", a [i][j]);
}
printf("\n");
}
}
/* Function main */
void main()
{
int i;
int number, index, dist;
int a [size][size];
struct Vertex vert [size];
struct Edge *List;
printf("\n Input the number of vertices in the graph: ");
scanf("%d", &number);
Input(number, a);
Output(number, a);
Table(number, a, vert);
printf("\n Input the starting vertex 0- %d:", number-1);
scanf("%d", &index);
dist = 0;
DFS (index, &dist, vert);
printf("\n Path length of the vertex from %c", vert[index].info);
printf("\n Vertex Length Vertex Connectivity \n ");
for (i = 0; i < number; i++)
{
printf("\n %c %d ", vert[i].info, vert[i].path_length);
for (List= vert[i].Edge_Ptr; List; List = List->next)
{
printf(" ");
putchar(List->terminal+'A');
}
}
}
Shared By :your name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
28. Write a program to create a graph in a adjacency...
Shared By :mayank dudhatra (sun shine collage - rajkot )Show/Hide Program
/* BREADTH FIRST SEARCH TECHNIQUE */
/* BFS.C */
# include<stdio.h>
# define size 20
# define T 1
# define F 0
struct Edge
{
int terminal;
struct Edge *next;
};
struct Vertex
{
int visit;
int vertex_no;
char info;
int path_length;
struct Edge *Edge_Ptr;
};
struct Q
{
int info;
struct Q *next;
};
void Table(int , int matrix [size][size], struct Vertex vert[size]);
struct Edge *Insert_Vertex (int , struct Edge *);
void BFS ( int , struct Vertex vert [size]);
void Input(int, int mat [size][size]);
void Output(int number, int mat [size][size]);
struct Q *Insert_Queue(int vertex_no, struct Q *first);
struct Q *Delete_Queue(int *vertex_no, struct Q *first);
/* Insert vertex into connectivity list */
struct Edge * Insert_Vertex (int vertex_no, struct Edge
*first) {
struct Edge *new1, *current;
new1 = (struct Edge *) malloc(sizeof(struct Edge));
new1->terminal = vertex_no;
new1->next = NULL;
if (!first)
return (new1);
for (current = first; current->next; current = current->next);
current->next = new1;
return (first);
}
/* Insert vertices into queue */
struct Q * Insert_Queue(int vertex_no, struct Q *first)
{
struct Q *new1, *current;
new1 =(struct Q *) malloc(sizeof(struct Q));
new1->info = vertex_no;
new1->next = NULL;
if (!first)
return (new1);
for (current = first; current->next; current = current->next);
current->next = new1;
return (first);
}
struct Q * Delete_Queue(int *vertex_no, struct Q *first)
{
struct Q *previous;
if (!first)
return (NULL);
*vertex_no = first->info;
previous = first;
first = first->next;
free(previous);
return (first);
}
/* Initializing entries */
void Table(int vertex_num, int matrix [size][size],
struct Vertex vert[size])
{
int i, j;
for (i = 0; i < vertex_num; i++)
{
vert [i].visit = F;
vert [i].vertex_no = i+1;
vert [i].info = 'A'+ i;
vert [i].path_length = 0;
vert [i].Edge_Ptr = NULL;
}
for (i =0; i < vertex_num ; i++)
for (j =0; j < vertex_num ; j++)
if (matrix [i][j] > 0 )
vert [i].Edge_Ptr = Insert_Vertex (j, vert [i].Edge_Ptr);
}
/* Computing path length */
void BFS ( int index, struct Vertex vert [size])
{
struct Q *queue = NULL;
struct Edge *Link;
vert [index].visit = T;
queue = Insert_Queue(index, queue);
while(queue)
{
queue = Delete_Queue(&index, queue);
for ( Link = vert [index].Edge_Ptr; Link; Link = Link->next)
{
if (vert [Link->terminal].visit == F)
{
vert[Link->terminal].visit = T;
vert[Link->terminal].path_length=vert[index].path_length+1;
queue = Insert_Queue(Link->terminal, queue);
}
}
}
}
/* Input function to read adjacency matrix */
void Input(int number, int mat [size][size])
{
int i, j;
printf("\n Input the adjacency matrix \n");
for (i =0; i < number; i++)
{
for (j=0; j < number; j ++)
{
scanf("%d", &mat [i][j]);
}
printf("\n");
}
}
/* Output function to display adjacency matrix */
void Output(int number, int mat [size][size])
{
int i, j;
printf("\n Adjacency matrix \n");
for (i =0; i < number; i++)
{
for (j=0; j < number; j ++)
{
printf(" %d", mat [i][j]);
}
printf("\n");
}
}
/* Function main */
void main()
{
int i, number, index;
int mat [size][size];
struct Vertex vert [size];
struct Edge *List;
printf("\n Input the number of vertices in the graph: ");
scanf("%d", &number);
Input(number, mat);
Output(number, mat);
Table(number, mat, vert);
printf("\n Input the starting vertex 0- %d :",number-1);
scanf("%d", &index);
BFS (index, vert);
printf("\n Path length of the vertex from %c", vert[index].info)
;
printf("\n Vertex Length Vertex Connectivity \n ");
for (i = 0; i < number; i++)
{
printf("\n %c %d ",vert[i].info, vert[i].path_length);
for (List= vert[i].Edge_Ptr; List; List = List->next)
{
printf(" ");
putchar(List->terminal+'A');
}
}
}
Shared By :your nameShow/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
29. Write a program to implement find shortest path using...
Shared By :mayank dudhatra (sun shine collage - rajkot ) Show/Hide Program
/* SHORTEST PATH */
/* SPATH.C */
# include<stdio.h>
# define size 10
# define infinity 9999
int a[size][size];
int m[size][size];
int i,k,j;
int n;
void Input ();
void Short ();
void Output ();
/* Input function */
void Input()
{
printf("\n Input the number of vetices: ");
scanf("%d", &n);
printf("\n Input adjacency matrix\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &a[i][j]);
}
printf("\n");
}
printf("\n Adjacency matrix \n");
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
printf(" %i", a[i][j]);
}
printf("\n");
}
}
/* Output function */
void Output()
{
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
printf(" %d", m[i][j]);
}
printf("\n");
}
}
/* Shortest path function */
void Short ()
{
/* Initialization of matrix m */
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
if (a[i][j] == 0)
m[i][j] = infinity;
else
m[i][j] = a[i][j];
}
}
printf("\n Adjacency matrix after replacing zeros by very large value");
Output();
/* Shortest path evaluation start from here */
for ( k = 0; k < n; k++)
{
for ( i = 0; i < n; i++)
{
for ( j = 0; j < n; j++)
{
if ( m [i][j] <= m [i][k] + m [k][j] )
m [i][j] = m [i][j];
else
m [i][j] = m[i][k] + m [k][j];
}
}
printf("\n STEP %d \n", k);
Output();
}
}
/* Function main */
void main()
{
Input();
Short();
printf("\n Shortest path matrix is as follows\n");
Output();
}
Shared By :Your Name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
30. Write a program to find minimal spanning tree for a given...
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
Shared By :Your Name Show/Hide Program
31. Write program to sort a given list using...
Shared By : Sonam Patel (SRIMCA - Tarsadi) Show/Hide Program
//To sort a given list using
//(a) Bubble sort
//(b) Selection sort
//(c) Insertion sort
//(d) Shell Sort
//(e) Quick sort
//(f) Merge sort
//(g) Radix sort
//(h) Heap sort
#include<stdio.h>
void Insert(int *a,int n)
{
int i;
for(i=0;i<=n;i++)
{
printf("Enter Value: ");
scanf("%d",(a+i));
}
printf("\nList is Created");
}
void Bubble_Sort(int a[],int n)
{
int i,j,x;
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j] > a[j+1])
{
x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
}
}
printf("\nList is Sorted using Bubble Sort");
}
void Selection_Sort(int a[],int n)
{
int i,j,min,x;
for(i=0;i<=n-1;i++)
{
min = i;
for(j=i+1;j<=n;j++)
{
if(a[j] < a[min])
min = j;
}
if(min != i)
{
x = a[min];
a[min] = a[i];
a[i] = x;
}
}
printf("\nList is Sorted using Selection Sort");
}
void Insertion_Sort(int a[],int n)
{
int i,j,x;
for(i=1;i<=n;i++)
{
x=a[i];
for(j=i-1;j>=0 && x < a[j];j--)
{
a[j+1] = a[j];
}
a[j+1]=x;
}
printf("\nList is Sorted using Insertion Sort");
}
void Shell_Sort(int a[],int n)
{
int i,j,incr,x;
incr = (n+1)/2;
while(incr != 0)
{
for(i=incr;i<=n;i++)
{
x=a[i];
j=i-incr;
while(j>=0 && x < a[j])
{
a[j+incr] = a[j];
j=j-incr;
}
a[j+incr]=x;
}
incr=incr/2;
}
printf("\nList is Sorted using Shell Sort");
}
void Quick_Sort(int a[],int LB,int UB)
{
int i,j,flag,key,x;
flag=1;
if(LB < UB)
{
i = LB;
j = UB + 1;
key = a[LB];
while(flag)
{
i = i + 1;
while(a[i] < key)
{
i = i + 1;
}
j = j - 1;
while(a[j] > key)
{
j = j - 1;
}
if(i < j)
{
x = a[i];
a[i] = a[j];
a[j] = x;
}
else
{
flag = 0;
}
}
x = a[LB];
a[LB] = a[j];
a[j] = x;
Quick_Sort(a,LB,j-1);
Quick_Sort(a,j+1,UB);
}
}
int Simple_Merge_Sort(int a[],int b[],int na,int nb,int temp[])
{
int i=0,j=0,k=-1;
Bubble_Sort(a,na);
Bubble_Sort(b,nb);
clrscr();
while(i <= na && j <= nb)
{
if(a[i] <= b[j])
{
k++;
temp[k] = a[i];
i++;
}
else
{
k++;
temp[k] = b[j];
j++;
}
}
if(i > na)
{
while(j <= nb)
{
k++;
temp[k] = b[j];
j++;
}
}
else
{
while(i <= na)
{
k++;
temp[k] = a[i];
i++;
}
}
return k;
}
void Init_Matrix(int M[10][25])
{
int i,j;
for(i=0;i<10;i++)
{
for(j=0;j<25;j++)
M[i][j] = 0;
}
}
void Radix_Sort(int a[],int n)
{
int M[10][25]={0},i,j,k,T,temp,dig,idx;
for(T=a[0],i=0;i<=n;i++)
{
if(T < a[i])
{
T = a[i];
}
}
temp=T;
T=0;
while(temp > 0 )
{
temp = temp / 10;
T++;
}
for(k=0;k<T;k++)
{
for(i=0;i<=n;i++)
{
temp=a[i];
for(j=0;j<=k;j++)
{
dig = temp % 10;
temp = temp / 10;
}
M[i][dig] = a[i];
}
idx = 0;
for(i=0;i<10;i++)
{
for(j=0;j<=n;j++)
{
if(M[j][i] != 0)
{
a[idx] = M[j][i];
idx++;
}
}
}
Init_Matrix(M);
}
printf("\nList is Sorted using Radix Sort");
}
void ReHeapUP(int *heap,int NewNode)
{
int parent,hold;
if(NewNode)
{
parent = (NewNode-1)/2;
if(heap[NewNode] > heap[parent])
{
hold = heap[parent];
heap[parent] = heap[NewNode];
heap[NewNode] = hold;
ReHeapUP(heap,parent);
}
}
return;
}
void ReHeapDown(int *heap,int rt,int n)
{
int hold,L,R,LargeIdx;
if((rt*2+1) <= n)
{
L = heap[rt*2+1];
if((rt*2+2) <= n)
{
R = heap[rt*2+2];
}
else
{
R = -1;
}
if(L > R)
{
LargeIdx = rt*2+1;
}
else
{
LargeIdx = rt*2+2;
}
if(heap[rt] < heap[LargeIdx])
{
hold = heap[rt];
heap[rt] = heap[LargeIdx];
heap[LargeIdx] = hold;
ReHeapDown(heap,LargeIdx,n);
}
}
return;
}
void Heap_Sort(int a[],int n)
{
int i,j,hold;
for(j=1;j<=n;j++)
{
ReHeapUP(a,j);
}
i=n; while(i > 0)
{
hold = a[0];
a[0] = a[i];
a[i] = hold;
i--;
ReHeapDown(a,0,i);
}
printf("\nList is Sorted using Heap Sort");
}
void View_List(int a[],int n)
{
int i;
if(n<0)
printf("\nList Is Empty");
else
{
printf("\nList is:");
for(i=0;i<=n;i++)
printf(" %d",a[i]);
}
}
void main()
{
int List[25],n=-1,ch,sch,flag,i,idx;
int List2[25],Merge[50];
do
{
do
{
clrscr();
printf("1. Create a New List\n2. Sort\n3. View List\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
n=1;
do
{
clrscr();
if(n>25 || n<=0)
printf("\nSorry!!! Out of List Size");
printf("\nHow many elements you want to Insert into List? ");
scanf("%d",&n);
}while(n<=0 || n>25);
n--;
Insert(&List[0],n);
getch();
break;
case 2:
do
{
clrscr();
printf("\n1. Bubble Sort");
printf("\n2. Selection sort");
printf("\n3. Insertion sort");
printf("\n4. Shell Sort");
printf("\n5. Quick sort");
printf("\n6. Simple Merge sort");
printf("\n7. Radix sort");
printf("\n8. Heap sort");
printf("\nChoose One Technique: ");
scanf("%d",&sch);
}while(sch<1 || sch>8);
switch(sch)
{
case 1:
Bubble_Sort(List,n);
break;
case 2:
Selection_Sort(List,n);
break;
case 3:
Insertion_Sort(List,n);
break;
case 4:
Shell_Sort(List,n);
break;
case 5:
Quick_Sort(List,0,n);
printf("\nList is Sorted using Quick Sort");
break;
case 6:
clrscr();
printf("\nYou required second List to Merge");
printf(" two List\nHow many elements you want ");
printf("to Insert into Second List? ");
scanf("%d",&i);
while(i<=0 || i>25)
{
clrscr();
if(i>25 || i<=0)
printf("\nSorry!!! Out of List Size");
printf("\nHow many elements you want to Insert into List? ");
scanf("%d",&i);
}
i--;
Insert(&List2[0],i);
getch();
idx = Simple_Merge_Sort(List,List2,n,i,Merge);
printf("\nList is Sorted using Merge Sort");
View_List(Merge,idx);
break;
case 7:
for(flag=0,i=0;i<=n;i++)
{
if(List[i] <= 0)
{
flag=1;
printf("Sorry!! The element must be");
printf(" positive for Heap Sort");
break;
}
}
if(flag==0)
Radix_Sort(List,n);
break;
case 8:
for(flag=0,i=0;i<=n;i++)
{
if(List[i] < 0)
{
flag=1;
printf("Sorry!! The element must not");
printf(" be Negative for Heap Sort");
break;
}
}
if(flag==0)
Heap_Sort(List,n);
}
getch();
break;
case 3:
View_List(List,n);
getch();
}
}while(ch != 4);
}
Shared By :Mohsin Baloch(SVICS kadi) Show/Hide Program
//Write program to sort a given list using Bubble sort
#include <conio.h>
#define N 5
void sort(int a[])
{
int exchge,i,j;
for(i=0;i<N-1;i++)
{
exchge=0;
for(j=0;j<N-1;j++)
{
if(a[j] >= a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
exchge++;
}
}
if(exchge==0)
break;
}
}
void main()
{
int a[N],i;
clrscr();
printf("\nEnter array Elements=\n");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
sort(a);
printf("\nSorted array is under=");
for(i=0;i<N;i++)
{
printf("\n%d ",a[i]);
}
getch();
}
//B..
//Write program to sort a given list using Selection sort
#include <conio.h>
#define N 5
void sort(int a[])
{
int i,j,temp;
for(i=0;i<N;i++)
{
for(j=i+1;j<N;j++)
{
if(a[j] < a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void main()
{
int a[N],i;
clrscr();
printf("\nEnter array Element=\n");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
sort(a);
printf("\nSorted array is under=");
for(i=0;i<N;i++)
{
printf("\n%d",a[i]);
}
getch();
}
//C..
////Write program to sort a given list using Instiortion sort
void main()
{
void insertion_sort(int *,int);
int n,*arr,i;
clrscr();
printf("\nHowmany elements you want sotre ? ");
scanf("%d",&n);
arr = (int *)malloc(sizeof(int)*n);
for(i=1;i<=n;i++)
{
printf("Enter %d Element : ",i);
scanf("%d",&arr[i]);
}
insertion_sort(arr,n);
printf("\nSorted Array Contains : ");
for(i=1;i<=n;i++)
{
printf("%d ",arr[i]);
}
getch();
}
void insertion_sort(int *k,int n)
{
int temp,pass,ptr;
k[0] = -32768;
for(pass=1;pass<=n;pass++)
{
temp = k[pass];
ptr = pass-1;
while(temp<k[ptr])
{
k[ptr+1] = k[ptr];
ptr -= 1;
}
k[ptr+1] = temp;
}
}
//d..
////Write program to sort a given list using Shell sort
void shell_sort(int *,int );
void main()
{
int n,i,*k;
clrscr();
printf("Enter howmany Number U Want Store ? ===> ");
scanf("%d",&n);
k = (int *)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
printf("Enter %d element= ",i+1);
scanf("%d",&k[i]);
}
shell_sort(k,n);
printf("Sorted array Contains : ");
for(i=0;i<n;i++)
printf("%d ",k[i]);
getch();
}
void shell_sort(int *a,int n)
{
int i,gap,swap,t;
gap = n/2;
while(gap>0)
{
swap = 1;
while(swap)
{
swap = 0;
for(i=0;i<n-gap;i++)
{
if(a[i]>a[i+gap])
{
swap=1;
t = a[i];
a[i] = a[i+gap];
a[i+gap] = t;
}
}
}
gap = gap / 2;
}
}
//e..
//Write program to sort a given list using Quick sort
#include <conio.h>
static int flag=0;
void display(int[],int,int);
void main()
{
int *k,n,i;
clrscr();
printf("Enter howmany digit u want to sort=");
scanf("%d",&n);
k=(int *) calloc(n,sizeof(int));
printf("\nEnter array=");
for(i=0;i<n;i++)
{
printf("\nEnter %d element= ",i+1);
scanf("%d",&k[i]);
}
printf("\nArray elements before sorting.\n");
display(k,0,n-1);
quick(k,0,n-1);
printf("\n\nSorted array.\n");
display(k,0,n-1);
getch();
}
void display(int k[],int low,int up)
{
int i;
for(i=low;i<=up;i++)
printf("%d element is= %d\n",i+1,k[i]);
}
int quick(int k[],int low,int up)
{
int l,r,temp,key;
flag=0;
l=low;
r=up;
key=low;
if(low >= up)
{
return;
}
while(flag==0)
{
//right to left
while(k[key] <= k[r] && key !=r)
r--;
if(key==r)
flag=1;
if(k[key] > k[r])
{
temp=k[key];
k[key]=k[r];
k[r]=temp;
key=r;
}
//left to right
while(k[key] >= k[l] && key !=l)
l++;
if(key==l)
flag=1;
if(k[key] < k[l])
{
temp=k[key];
k[key]=k[l];
k[l]=temp;
key=l;
}
}
quick(k,low,key-1);
quick(k,key+1,up);
}
//f..
//Write program to sort a given list using Merge sort
#include <conio.h>
#define N 5
void sort(int a[])
{
int temp,i,j;
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
if(a[i] <= a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
}
void merge(int a[],int b[],int temp[N])
{
int j,i;
for(i=0;i<N;i++)
temp[i]=a[i];
for(i=N,j=0;j<N;i++,j++)
temp[i]=b[j];
}
void main()
{
int i,a[N],b[N],c[N+N],temp[N+N];
int l,j,second,first=0,third;
clrscr();
printf("\nEnter 1st array= ");
for(i=0;i<N;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter 2nd array= ");
for(i=0;i<N;i++)
{
scanf("%d",&b[i]);
}
sort(a);
sort(b);
merge(a,b,temp);
//INITIALIZATION
second=N;
third=N+N-1;
i=first;
j=second;
l=0;
//COMPARE CORRESPONDING ELEMENTS & OUTPUT THE SMALLEST
while(i<second && j<=third)
{
if (temp[i] <= temp[j])
{
c[l]=temp[i];
l++;
i++;
}
else
{
c[l]=temp[j];
l++;
j++;
}
}
//COPY THE REMAINING UNPROCESSED ELEMENTS IN OUTPUT AREA
if(i >= second)
{
while(j <= third)
{
c[l]=temp[j];
j++;
l++;
}
}
else
{
while(i < second)
{
c[l]=temp[i];
i++;
l++;
}
}
//PRINT
printf("\nSorted array is under= ");
for(i=0;i<N+N;i++)
{
printf("\n%d",c[i]);
}
//FINISHED
getch();
}
32. Write program to search an element in a given list..
Shared By :SONAM Patel (SRIMCA - Tarsadi) Show/Hide Program
//To search an element in a given list using
//(a) Linear search
//(b) Binary search(iterative)
#include<stdio.h>
int Insert(int *a,int n)
{
if(n < 25)
{
n++;
printf("\nEnter Value: ");
scanf("%d",(a+n));
printf("\nValue is Inserted");
return n;
}
else
printf("\nList is Full");
return n;
}
void Simple_Linear_Srch(int a[],int n,int val)
{
int i;
printf("\nSimple Linear Search Result:\n");
for(i=0;i<=n;i++)
{
if(a[i]==val)
{
printf(" %d is Found in the List on Position %d",a[i],i);
return;
}
}
printf(" Value is Not Found in the List");
}
void Sentinel_Srch(int a[],int n,int val)
{
int i=0;
printf("\nSentinel Search Result:\n");
a[n+1]=val;
while(a[i] != val)
{
i++;
}
if(i<=n)
printf(" %d is Found in the List on Position %d",a[i],i);
else
printf(" Value is Not Found in the List");
}
void Probability_Srch(int a[],int n,int val)
{
int i,x;
printf("\nProbability Search Result:\n");
for(i=0;i<=n;i++)
{
if(a[i]==val)
{
if(i!=0)
{
x=a[i];
a[i]=a[i-1];
a[i-1]=x;
i--;
}
printf(" %d is Found in the List on Position %d",a[i],i);
return;
}
}
printf(" Value is Not Found in the List");
}
void Sort(int a[],int n)
{
int i,j,x;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j] > a[j+1])
{
x=a[j];
a[j]=a[j+1];
a[j+1]=x;
}
}
}
}
void Ordered_List_Srch(int a[],int n,int val)
{
int i;
Sort(a,n);
printf("\nOrdered List Search Result:\n");
for(i=0;i<=n && a[i]<=val;i++)
{
if(a[i]==val)
{
printf(" %d is Found in the List on Position %d",a[i],i);
return;
}
}
printf(" Value is Not Found in the List");
}
void Binary_Srch(int a[],int n,int val)
{
int fst=0,lst=n,mid;
Sort(a,n);
printf("\nBinary Search Result:\n");
while(fst<=lst)
{
mid=(fst+lst)/2;
if(a[mid] == val)
{
printf(" %d is Found in the List on Position %d",a[mid],mid);
return;
}
else if(a[mid] > val)
{
lst=mid-1;
}
else
{
fst=mid+1;
}
}
printf(" Value is Not Found in the List");
}
void View_List(int a[],int n)
{
int i;
if(n>=0)
{
printf("\nList is:");
for(i=0;i<=n;i++)
printf(" %d",a[i]);
}
else
printf("\nList is Empty");
}
void main()
{
int List[25],n=-1,ch,sch,sch1,val;
do
{
do
{
clrscr();
printf("1. Insert an element\n2. Search\n3. View List\n4. Exit");
printf("\nEnter Your Choice: ");
scanf("%d",&ch);
}while(ch<1 || ch>4);
switch(ch)
{
case 1:
n=Insert(&List[0],n);
getch();
break;
case 2:
printf("\nEnter Value which you want to Search: ");
scanf("%d",&val);
do
{
clrscr();
printf("\n1. Linear Search\n2. Binary Search");
printf("\nEnter Your Choice: ");
scanf("%d",&sch);
}while(sch<1 || sch>2);
switch(sch)
{
case 1:
do
{
printf("\n1. Simple Linear Search");
printf("\n2. Sentinel Search\n");
printf("3. Probability Search\n");
printf("4. Ordered List Search\n");
printf("Enter Your Choice: ");
scanf("%d",&sch1);
}while(sch1<1 || sch1>4);
switch(sch1)
{
case 1:
Simple_Linear_Srch(List,n,val);
break;
case 2:
Sentinel_Srch(List,n,val);
break;
case 3:
Probability_Srch(List,n,val);
break;
case 4:
Ordered_List_Srch(List,n,val);
}
break;
case 2:
Binary_Srch(List,n,val);
}
getch();
break;
case 3:
View_List(List,n);
getch();
}
}while(ch != 4);
}
Shared By :Mohsin Baloch(SVICS kadi) Show/Hide Program
/*Write program to search an element in a given list using Linear search */
static int flag=0;
int search(int a[],int x,int n)
{
int i;
for(i=0;i<n;i++)
{
if(a[i]==x)
{
flag=1;
return (i+1);
}
}
}
void main()
{
int *a,n,i,x,k;
clrscr();
printf("Howmany value u want to enter= ");
scanf("%d",&n);
a=(int *) malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("Enter %d value= ",i+1);
scanf("%d",&a[i]);
}
printf("\nEnter which element u want to search= ");
scanf("%d",&k);
x=search(a,k,n);
if(flag==1)
{
printf("\nAfter searching the element position of element is= %d",x);
}
else
{
printf("\nElement not found.");
}
getch();
}
//b..
/*Write program to search an element in a given list using Binary search */
static int flag=0;
int search(int a[],int x,int n)
{
int LOW=0,HIGH=n-1,MID;
while(LOW<=HIGH)
{
MID=(LOW+HIGH)/2;
if(a[MID]==x)
{
flag=1;
return (MID+1);
}
else if(a[MID] > x)
{
HIGH=MID-1;
}
else
{
LOW=MID+1;
}
}
}
void sort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
void main()
{
int *a,n,i,x,k;
clrscr();
printf("Howmany value u want to enter= ");
scanf("%d",&n);
a=(int *) malloc(n*sizeof(int));
for(i=0;i<n;i++)
{
printf("Enter %d value= ",i+1);
scanf("%d",&a[i]);
}
sort(a,n);
printf("\nEnter which element u want to search= ");
scanf("%d",&k);
x=search(a,k,n);
if(flag==1)
{
printf("\nAfter searching the element position of element is= %d",x);
}
else
{
printf("\nElement not found.");
}
getch();
}
33. Write a Program to merge two given sorted arrays...
Shared By :amar punatar,bhautik sanghvi(jvims - jamnagar) Show/Hide Program
//programm no 33. w.a.p. to merge to given sorted arrays.
#include<conio.h>
#include<stdio.h>
main()
{
int i,j,k,temp,n1,n2,a[5]={1,3,5,7,9},b[5]={2,4,6,8,10},c[10];
clrscr();
for(i=j=k=0;i<=5+5;)
{
if(a[j]<=b[k])
{
c[i++]=a[j++];
}
else
{
c[i++]=b[k++];
}
if(j==5||k==5)
{
break;
}
}
for(;j<=5-1;)
{
c[i++]=a[j++];
}
for(;k<=5-1;)
{
c[i++]=b[k++];
}
printf("\n\nAfter sorting\n\n");
for (i=0;i<10;i++)
{
printf("\n%d",c[i]);
}
getch();
}
Shared By :Your Name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z
34. Hashing and Collision Resolution...
Shared By :SAGAR PATEL(B.H.GARDI,RAJKOT) Show/Hide Program
#include<iostream>
#include<iomanip>
using namespace std;
ostream &man(ostream &temp)
{
int n;
temp<<setw(15);
temp<<setprecision(2);
temp<<setiosflags(ios::right);
temp<<setfill('+');
temp<<setiosflags(ios::showpoint);
return temp;
};
void main()
{
cout<<man;
cout<<10.5;
cin.get();
}
Shared By :Your Name Show/Hide Program
Share This Program..
send it gtumca@gmail.com
with your name - college name..
and share what you want ... at same mail id...
Thanx in advance...
Be connected...
D_i_Z