Lab Manual
Lab Manual
Aim
Algorithm
Step1: Start
Step 2: Enter the no: of vertices and adjacency matrix of the graph
2. enqueue(node)
3. STATUS[node] = 2
8. Set STATUS[node] = 3
Step 4: Stop
Program
#include<stdio.h>
#define MAX 20
int G[10][10],visited[10],n,rear,front,queue[MAX];
rear=-1;front=-1;
if(rear == MAX-1)
printf("Queue Overflow\n");
else
if(front == -1)
front = 0;
rear = rear+1;
queue[rear] = vertex ;
}
int dequeue()
int delete_item;
printf("Queue Underflow\n");
exit(1);
delete_item = queue[front];
front = front+1;
return delete_item;
void main()
int i,j,n;
scanf("%d",&n);
printf("%d",n);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
breadthFirstSearch(G,n,0);
int i,status[n];
for(i=0;i<n;i++)
status[i]=1;
enqueue(pnode);
status[pnode] =2;
while(front!=-1)
{
pnode = dequeue();
status[pnode] =3;
for(i=0;i<n;i++)
enqueue(i);
status[i] = 2;
Output:
010100
001001
000011
001010
000000
000000
013254
Queue Underflow
Aim
To develop a C program to implement DFS for graph representations.
Algorithm
Step1: Start
Step 2: Enter the no: of vertices and adjacency matrix of the graph
Step 3: Call the DFS function, it works as follows
1. Set STATUS[i] = 1 for all vertices i.
2. push(node) and STATUS[node] = 2
3. Repeat step 5 to 8 while STACK is not empty
4. pnode = pop()
5. If STATUS[node] == 3, then go to step 3
6. Process the node
7. Repeat step 8 for all the adjacent vertices q of node
8. If STATUS[q] != 3, then push(q) and STATUS[q] = 2
9. Set STATUS[node] = 3
Step 4. Stop
Program
#include<stdio.h>
int G[10][10],visited[10],n,top=-1,stack[20];
void depthFirstSearch(int a[10][10],int n,int pnode);
void push(int item)
{
stack[++top] = item;
}
int pop()
{
return stack[top--];
}
void main()
{
int i,j,n;
printf("Enter number of vertices:");
scanf("%d",&n);
printf("\nEnteradjecency matrix of the graph:");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
depthFirstSearch(G,n,0);
}
void depthFirstSearch(int a[10][10],int n1,int pnode)
{
int i, status[n1];
for(i=0;i<n1;i++)
status[i]=1;
push(pnode);
status[pnode] =2;
while(top!=-1)
{
pnode = pop();
printf("%d ", pnode);
status[pnode] =3;
for(i=0;i<n1;i++)
{
if(a[pnode][i] == 1 && status[i] == 1)
{
push(i);
status[i] = 2;
}
}
}
}
Output:
Enter number of vertices:6
Enter adjacency matrix of the graph:
010000
001001
000111
000010
000000
000000
015243
IMPLEMENTATION OF SORTING ALGORITHMS – BUBBLE SORT
Aim
Algorithm
Step 1: Start
Step 2: Start at index zero, compare the element with the next one (a[0] & a[1]
Step 4: Now compare a[1] & a[2] and swap if a[1] > a[2].
Step 7: Stop
Program
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
Output
Result
Aim
To develop a C program to implement sorting algorithms – insertion sort.
Algorithm
Step 1: Start
Step 2: Insert elements and Repeat step 3 to 8 for i =1 to n-1
Step 3: Set temp = a[i]
Step 4: Set j = i-1
Step 5: Repeat step 6 and 7 while temp <a[j] and j>=0
Step 6: a[j] = a[j-1]
Step 7: j = j-1
Step 8: a[j] =temp
Step 9: Display the sorted array
Step 10: Stop
Program
#include <stdio.h>
int main()
{
int a[50],n,i,temp,min,j;
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the %delements of the array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i;j>0&&temp<a[j-1];j--)
{
a[j]=a[j-1];
}
a[j]=temp;
}
printf("\nDisplaying the sorted elements:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Output
Result
Aim
Algorithm
Step 1: Start
Step 9: Exit
Program
#include <stdio.h>
int main()
{
int a[50],n,i,temp,min,j;
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the %delements of the array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
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)
{
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
}
printf("\n Displaying the sorted elements:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
Output:
5
7
Result
Aim
To develop a C program to implement sorting algorithms – quick sort.
Algorithm
Step 1: Insert elements to be sorted into an array A
Step 2: The quick sort algorithm works as follows:
1. Select an element pivot from the array elements.
2. Rearrange the elements in the array in such a way that all elements that are less
than the pivot appear before the pivot and all elements greater than the pivot element
come after it (equal values can go either way). After such a partitioning, the pivot is
placed in its final position. This is called the partition operation.
3. Recursively sort the two sub-arrays thus obtained. One with sub-list of values
smaller than that of the pivot element and the other having higher value elements.
Step 3: If the last element is considered as the pivot (END) and the partition is done as follows.
PARTITION (A, BEG, END)
{
X=A[END] i=BEG-1
for(j=BEG TO END-1)
{
if(A[j]<=X)
{
i=i+1;
Exchange A[i] With A[j]
}
}
Exchange A[i+1] with A[END] return i+1
}
Step 4: Display the sorted elements
Step 5: Stop
Program
#include <stdio.h>
int main() {
int n, i;
int a[size];
scanf("%d", &n);
scanf("%d", &a[i]);
QUICKSORT(a, 0, n - 1);
return 0;
int X, t, i, j;
X = A[END];
i = BEG - 1;
if (A[j] <= X) {
i = i + 1;
t = A[i];
A[i] = A[j];
A[j] = t;
t = A[i + 1];
A[i + 1] = A[END];
A[END] = t;
return i + 1;
int Q;
QUICKSORT(A, Q + 1, END);
}
Output:
56
34
32
22
Result
Program to implement quick sort algorithm has been successfully executed and
output is verified.
Aim
To develop a C program to implement sorting algorithms – merge sort.
Algorithm
Step 1: Start
Step2: Insert elements to be sorted into an array Step 3: The Merge sort algorithm works as
follows:
1: If it is only one element in the list it is already sorted, return.
2: Divide the list recursively into two halves until it can no more be divided.
3: Merge the smaller lists into new list in sorted order.
Step 4:
MERGE_SORT(ARR, BEG, MID)
1. If BEG < END then do step 2 to 5
2. Set MID = (BEG + END)/2
3. Call MERGE_SORT(ARR, BEG, MID)
4. Call MERGE_SORT(ARR, MID+1, END)
5. MERGE(ARR, BEG, MID, END)
6. END
Step 5: Display the sorted elements
Step 6: Stop
Program
#include <stdio.h>
void mergesort(int A[],int lb,int nb)
{
int mid;
if(lb<nb)
{
mid=(lb+ub)/2;
mergesort(A,lb,mid);
mergesort(A,mid+1,ub);
merge(A,lb,mid,ub);
}
}
void merge(int a[],int lb,int mid,int ub)
{
int i,j,k,b[10];
i=lb;
j=mid+1;
k=lb;
while(i<=mid&&j<=ub)
{
if(a[i]<=a[j])
{
b[k]=a[i];
i++;
k++;
}
else
{
b[k]=a[j];
j++;
k++;
}
}
if(i>mid)
{
while(j<=ub)
{
b[k]=a[j];
j++;
k++;
}
}
else
{
while(i<=mid)
{
b[k]=a[i];
i++;
k++;
}
}
for(k=lb;k<=ub;k++)
{
a[k]=b[k];
}
}
int main()
{
int n,i; int a[10];
printf("Enter the number of elements");
scanf("%d",&n);
printf("\nThe elements are:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
mergesort(a,0,n-1);
printf("\nAfter sorting the elements are");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
return 0;
}
Output:
Result
Experiment No.20
Aim
Algorithm
Step 1: Start
Step 4: Delete the root element from Min/Max Heap using Heapify method.
Program
#include <stdio.h>
int temp;
void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[largest])
largest = l;
if (r < n && arr[r] > arr[largest])
largest = r;
if (largest != i)
{
temp = arr[i];
arr[i]= arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--)
{
temp = arr[0];
arr[0]= arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
printf("%d ",arr[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, n);
printf("Sorted array is \n");
printArray(arr, n);
}
Output:
Sorted array is 5 6 7 11 12 1
Result
Program to implement heap sort algorithm has been successfully executed and
output is verified.
AIM: Implementation of hash table using linear probing and searching in hash table
ALGORITHM:
Step1: Start
Step2: Enter the no: of elements to be inserted, n
Step3: Input the n elements in array A
Step4: Enter the size of hash table, m
Step5: Initialize all the entry to the hash table(H) as -1
Step6: Hash:
6.1. initialize j=0 and h=-1 evaluate the expression, h=((A[i]%m)+j)%m;
6.2.if (h<m) and if H(h) ==-1 insert the element A[i] at H[h]
else increment j and goto Hash
Step 7: Repeat the step 6 until all elements of the array is inserted into the hash table.
Step 8: Display the elements in the array
Step 9: Enter the element to be searched, item
Step10: if item is there in the array print item found else not found
Step 11: Stop
PROGRAM:
#include<stdio.h>
void main()
{
int i,h,H[100],A[50],m,n,j,e,c=0;
printf("\nEnter the number of elements to be inserted:");
scanf("%d",&n);
printf("\nEnter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
printf("\nEnter size of Hash table:");
scanf("%d",&m);
for(i=0;i<m;i++)
H[i]=-1;
for(i=0;i<n;i++)
{
j=0;
h=-1;
hash:
h=((A[i]%m)+j)%m;
if(h<m)
{
if(H[h]==-1)
H[h]=A[i];
else
{ j++;
goto hash;
}
}
}
printf("\nHash Table
display:\n");
for(i=0;i<m;i++)
{
if(H[i]==-1)
printf("
- "); else
printf("%d ",H[i]);
}
printf("\nEnter element to be searched:");
scanf("%d",&e);
for(i=0;i<m;i++)
if(H[i]==e)
{
printf("\nElement %d found in hash table \nposition :%d \nindex:%d\
n",e,i+1,i); c++;
}
if(c==0)
printf("\nElement %d not found in hash table\n",e);
}
Output:
Enter the number of
elements to be inserted:6
Enter 6 elements:
26
37
59
76
65
86
position :10
index:9
Result: The program to implement hash table using linear probing and searching is
successfully implemented and executed.