The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by abhimoga971999, 2018-12-12 10:07:22

DS Practical (1)-converted

DS Practical (1)-converted

Linear And Binary Search

Binary Search

Input:
#include <stdio.h>
#include <conio.h>
#define SIZE 10

int smallest(int a[], int k, int n);
void selectionSort(int a[], int n);

void main()
{

int a[SIZE], num, i, n, beg, end, mid, found = 0;
printf("\nEnter the no. of elements in the array:");
scanf("%d", &n);
printf("\nEnter the elements->\n");
for(i = 0; i < n; i++)

scanf("%d", &a[i]);
selectionSort(a, n);
printf("\nThe Sorted array is: \n");
for(i = 0; i < n; i++)

printf("%d\t", a[i]);
printf("\nEnter the no. to be searched=");
scanf("%d", &num);
beg = 0;
end = n-1;
while(beg <= end)

{
mid = (beg + end)/2;
if(a[mid] == num)
{
printf("\n%d is present in the array at position = %d", num, mid+1);
found = 1;
break;
}
else if(a[mid] > num)
end = mid - 1;
else
beg = mid + 1;

}
if((beg < end) && (found == 0))

printf("\n%d does not exist in the array!", num);
return 0;
}

int smallest(int a[], int k, int n)
{

int pos = k, small = a[k], i;
for(i = k+1; i < n; i++)
{

if(a[i] < small)
{

small = a[i];
pos = i;
}

}
return pos;
}

void selectionSort(int a[], int n)
{

int k, pos, temp;
for(k = 0; k < n; k++)
{

pos = smallest(a, k, n);
temp = a[k];
a[k] = a[pos];
a[pos] = temp;
}
}

Linear Search

Input:
#include <stdio.h>
#include <conio.h>
#define SIZE 20

void main()
{

int a[SIZE], num, i, n, found = 0, pos = -1;
printf("\nEnter the no. of the elements in the array:");
scanf("%d", &n);

printf("\nEnter the elements in the array->\n");
for(i = 0; i < n; i++)

scanf("%d", &a[i]);
printf("\nEnter the no. that has to be searched=");
scanf("%d", &num);
for(i = 0; i < n; i++)
{

if(a[i] == num)
{

found = 1;
pos = i;
printf("\n%d is found in the array at position = %d", num, i+1);
break;
}
}
if(found == 0)
printf("\n%d does not exist in the array!", num);
getch();
}

Implementation of Graph

Depth First Search

Inputs :

#include <stdio.h>
#include <conio.h>

void DFS(int);
int G[10][10], visited[10], n;

void main()
{

int i, j;
printf("\nEnter the no of vertices=");
scanf("%d", &n);
printf("\nEnter the adjecency matrix of the graph:");
for(i = 0; i < n; i++)
{

for(j = 0; j < n; j++)
{

scanf("%d", &G[i][j]);
}
}
for(i = 0; i < n; i++)
visited[i] = 0;
DFS(0);
}

void DFS(int i)
{

int j;
printf("\n%d", i);
visited[i] = 1;
for(j = 0; j < n; j++)
{

if(!visited[j] && G[i][j] == 1)
DFS(j);

}
}

Breadth First Search

Input:
#include <stdio.h>
#include <conio.h>
#define MAX 10

typedef struct Q
{

int R, F;
int data[MAX];
}Q;

int empty(Q * P);
int full(Q * P);
void enqueue(Q * P, int x); //Function Prototype
int dequeue(Q * P);

void BFS(int);

int G[MAX][MAX];
int n;

void main()
{

int i, j, v;
printf("\nEnter no. of vertices: ");
scanf("%d", &n);
printf("\nEnter the adjecendy matrix of graph: "); //printing on the screen
for(i = 0; i < n; i++)
{

for(j = 0; j < n; j++)
{

scanf("%d", &G[i][j]);
}
}
printf("\nEnter the starting vertex for BFS:");
scanf("%d", &v);
BFS(v);
getch();
}

void BFS(int v)
{

int visited[MAX], i;
Q q;

q.R = q.F = -1;
for(i = 0; i < n; i++)
{

visited[i] = 0;
}
enqueue(&q, v);
printf("\nvisit\t%d", v);
visited[v] = 1;
while(!empty(&q))
{

v = dequeue(&q);
for(i = 0; i < n; i++)
{

if(visited[i] == 0 && G[v][i] != 0)
{

enqueue(&q, i);
visited[i] = 1;
printf("\nvisit\t%d", i);
}
}
}
}

int empty(Q * P)
{

if(P->R == -1)
return 1;

return 0;

}

int full(Q * P)
{

if(P->R == MAX-1)
return 1;

return 0;
}

//Using queue logic
void enqueue(Q * P, int x)
{

if(P->R == -1)
{

P->R = P->F = 0;
P->data[P->R] = x;
}
else
{
P->R = P->R + 1;
P->data[P->R] = x;
}
}

int dequeue(Q * P)
{

int x;
x = P->data[P->F];

if(P->R == P->F)
{

P->R = -1;
P->F = -1;
}
else
P->F = P->F + 1;
return x;
}


Click to View FlipBook Version