0% found this document useful (0 votes)
70 views48 pages

Practical: 1: Aim: Implementation and Time Analysis of Bubble, Selection and Insertion Bubblesort

The document describes implementations and time analyses of various sorting algorithms including bubble sort, insertion sort, selection sort, quicksort, mergesort, and heapsort. Code implementations are provided for each algorithm along with sample outputs showing the sorted arrays and time taken for each algorithm. The aim is to analyze the best, average, and worst case time complexities of the sorting algorithms.

Uploaded by

SAPNA NISHAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views48 pages

Practical: 1: Aim: Implementation and Time Analysis of Bubble, Selection and Insertion Bubblesort

The document describes implementations and time analyses of various sorting algorithms including bubble sort, insertion sort, selection sort, quicksort, mergesort, and heapsort. Code implementations are provided for each algorithm along with sample outputs showing the sorted arrays and time taken for each algorithm. The aim is to analyze the best, average, and worst case time complexities of the sorting algorithms.

Uploaded by

SAPNA NISHAD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 48

PRACTICAL: 1

Aim: Implementation and Time Analysis of bubble, Selection and Insertion


sorting algorithms For Best case, Average case & Worst case
1. BubbleSort:
#include <stdio.h>
#include<time.h>
void swap(int *xp, int*yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[],
int n)
{
printf("190303108123\n");
printf("Pankaj Yadav\n");
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (arr[j] > arr[j+1])
swap(&arr[j],
&arr[j+1]);
}
void printArray(int arr[],
int size)
{
int i;

190303108068
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] =
{7,5,2,21,50,90,30,14,33,18,88,44,11,30,75,85,13,76,7,6,10,66,43,2,30,433,143
,670,40};
int n = sizeof(arr)/sizeof(arr[0]);
double time_b;
clock_t start,end;
start = clock();
bubbleSort(arr, n);
end = clock();
printf("Sorted array:\n");
printArray(arr, n);
time_b += (double)(end - start) /
CLOCKS_PER_SEC;
printf("\n time taken = %lf ",time_b);
return 0;
}

190303108068
Output:

190303108123
Pankaj Yadav

190303108068
2. Insertion Sort :
#include<stdlib.h>
#include<time.h>
#include <math.h>
#include <stdio.h>
void insertionSort(int
a[], long int n)
{
int i, t, j;
for (i = 1; i < n; i++)
{
t = a[i];
j = i - 1;
while (j >= 0 && a[j] > t)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = t;
}
}
void printArray(int a[],
int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);

190303108068
printf("\n");
}
int main()
{
int a[] ={7,5,2,21,50,90,30,14,33,18,88,44,11,30,75,85,13,76,7,6,2};
double time_b;
clock_t start,end;
int n = sizeof(a) / sizeof(a[0]);
start = clock();
insertionSort(a, n);
end = clock(); printf("\
n190303108123\n\n");
printf("\n Pankaj Yadav\n\n");
printArray(a, n);
time_b += (double)(end - start) /
CLOCKS_PER_SEC;
printf("\n time taken = %lf ",time_b);
return 0;
}

190303108068
Output:

190303108123

Pankaj Yadav

190303108068
1. Selection Sort:
#include <time.h>
#include
<stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n - 1; i++)
{
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] <
arr[min_idx])
min_idx = j;
swap(&arr[min_idx],&arr[i]);
}
}
void printArray(int arr[],int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);

190303108068
printf("\n");

}
int main()
{
int arr[] ={7,5,2,21,50,90,30,14,33,18,88,44,11,30,75,85,13,76,7};

int n = sizeof(arr) / sizeof(arr[0]);


double time_b;
clock_t stat, end;
stat = clock();
selectionSort(arr, n);
end = clock();
printf("\n190303108123\n\n");
printf("\n Pankaj Yadav\n\n");
printf("Sorted array:\n");
printArray(arr, n);
time_b+=(double)(end-stat)/CLOCKS_PER_SEC;

printf("Time Taken %lf",time_b);


return 0;
}

190303108068
Output:

190303108123

Pankaj Yadav

P R Ps Total

190303108068
PRACTICAL: 2
Aim: Implementation and Time Analysis of Quick sort algorithms For Best case,
Average case & Worst case using divide and conquer.
Code:
#include<stdio.h>
#include<time.h>
int temp;
void heapify(int arr[], int size, int i)
{
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < size && arr[left] >arr[largest])
largest = left;
if (right < size && arr[right] > arr[largest])
largest = right;
if (largest != i)
{
temp = arr[i];
arr[i]= arr[largest];
arr[largest] = temp;
heapify(arr, size, largest);
}
}
void heapSort(int arr[], int size)
{
int i;
for (i = size / 2 - 1; i >= 0; i--)

190303108068
heapify(arr, size, i);
for (i=size-1; i>=0; i--){
temp = arr[0];
arr[0]= arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void main()
{
printf("190303108123\n");
printf("Pankaj Yadav\n");
int arr[] =
{9,18,81,23,120,26,20,55,16,40,45,4,60,45};
int i;
int size = sizeof(arr)/sizeof(arr[0]);
double time_b;
clock_t start,end;
start = clock();
heapSort(arr, size);
end=clock();
time_b += (double)(end - start) / CLOCKS_PER_SEC;
printf("\n time taken = %lf\n ",time_b);
printf("printing sorted elements\n");
for (i=0; i<size; ++i)
printf("%d\n",arr[i]);
}

190303108068
Output:
P R Ps Total

190303108123
Pankaj Yadav

190303108068
PRACTICAL: 3
Aim: Implementation and Time Analysis of Merge sort algorithms For
Bestcase, Average case & Worst case.
Code:
#include <iostream>
#include<time.h>
using namespace std;
void merge(int arr[], int start, int mid, int end)
{ int len1 = mid - start + 1;
int len2 = end - mid;
int leftArr[len1], rightArr[len2];
for (int i = 0; i < len1; i++)
leftArr[i] = arr[start + i];
for (int j = 0; j < len2; j++)
rightArr[j] = arr[mid + 1 + j];
int i, j, k;
i = 0;
j = 0;
k = start;
while (i < len1 && j < len2)
{ if (leftArr[i] <= rightArr[j])
{ arr[k] = leftArr[i];
i++;
} else
{
arr[k] = rightArr[j]; j++;
} k+
+;

190303108068
}
while (i < len1)
{ arr[k] = leftArr[i];
i++;
k++;
}
while (j < len2)
{ arr[k] = rightArr[j];
j++;
k++;
}
}
void mergeSort(int arr[], int start, int end)
{
if (start < end)
{
int mid = start + (end - start) / 2;
mergeSort(arr, start, mid);
mergeSort(arr, mid + 1, end);
merge(arr, start, mid, end);
}
}
void display(int arr[], int size)
{
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
cout << end

190303108068
int main()
{
int arr[] = {6, 5, 12, 10, 9,
1,30,2478,985,463,857,232,23,43,86,898,287,8,65,34,31,45,6654,56,36,95,65,
45,25};
int size = sizeof(arr) / sizeof(arr[0]);
double time_b;
clock_t start,end;
printf("190303108123\n");
printf("Pankaj yadav\n");
cout << "Original array \n";
display(arr, size);
start=clock();
mergeSort(arr, 0, size - 1);
end=clock();
cout << "Sorted array \n";
display(arr, size);
time_b+=(double)(end-start)/CLOCKS_PER_SEC;
printf("\n time taken =%lf\n",time_b);
return 0;
}

190303108068
Output:

190303108123
Pankaj yadav

P R Ps Total

190303108068
PRACTICAL: 4
Aim: Implementation and Time analysis of Heap-Sort algorithm
Code:
#include<stdio.h>
#include<time.h>
int temp;
void heapify(int arr[], int size, int i)
{
int largest = i;
int left = 2*i + 1;
int right = 2*i + 2;
if (left < size && arr[left] >arr[largest])
largest = left;
if (right < size && arr[right] > arr[largest])
largest = right;
if (largest != i)
{
temp = arr[i];
arr[i]= arr[largest];
arr[largest] = temp;
heapify(arr, size, largest);
}
}
void heapSort(int arr[], int size)
{
int i;
for (i = size / 2 - 1; i >= 0; i--)
heapify(arr, size, i);

190303108068
for (i=size-1; i>=0; i--)
{
temp = arr[0];
arr[0]= arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
int main()
{
int arr[] = {13,4,3,16,2,5,9,12,15,18,19,10,8,1,2,7,15,29,24,23,17,20};
int i;
int size = sizeof(arr)/sizeof(arr[0]);
double time_b;
clock_t start,end;
start = clock();
heapSort(arr, size);
end=clock();
time_b += (double)(end - start) / CLOCKS_PER_SEC;
printf("190303108123\n");
printf("Pankaj Yadav\n");
printf("\n Time taken to arrange %d elements are = %lf\n ",size,time_b);
printf("sorted elements are\n");
for (i=0; i<size; ++i)
printf("%d ",arr[i]);
}

190303108068
Output:

190303108123
Pankaj Yadav

Time complexity: O(n log n)

P R Ps Total

190303108068
PRACTICAL: 5
Aim: Implementation and Time analysis of Krushkal’s Minimum spanning
Tree algorithms.
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int i,j,k,a,b,u,v,n,ne=1;
int
min,mincost=0,cost[9][9],parent[9];
int find(int);
int uni(int,int);
int main()
{
printf("190303108123\n");
printf("Pankaj Yadav\n");
printf("\n\tImplementation of Kruskal's algorithm\n");
printf("\nEnter the no. of vertices:");
scanf("%d",&n);
printf("\nEnter the cost adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
}
printf("The edges of Minimum Cost Spanning Tree are\n");

190303108068
while(ne < n)
{
for(i=1,min=999;i<=n;i++)
{
for(j=1;j <= n;j++)
{
if(cost[i][j] < min)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
}
}
u=find(u);
v=find(v);
if(uni(u,v))
{
printf("%d edge (%d,%d) =%d\n",ne++,a,b,min);
mincost +=min;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n\tMinimum cost = %d\n",mincost);

}
int find(int i)
{

190303108068
while(parent[i])
i=parent[i];
return i;
}
int uni(int i,int j)
{
if(i!=j)
{
parent[j]=i;
return 1;
}
return 0;
}

190303108068
Output:

190303108123
Pankaj Yadav

Time Complexity is O(ElogE) or O(ElogV)

P R Ps Total

190303108068
PRACTICAL: 6
Aim: Implementation and Time analysis of Prim’s Minimum spanning Tree
algorithms.
Code:
#include<stdio.h>
#include<conio.h>
int a,b,u,v,n,i,j,ne=1;
int visited[10]= {0}
,min,mincost=0,cost[10][10];
void main()
{
printf("\n190303108123\n\n");
printf("\nPankaj Yadav\n\n");
printf("\n Enter the number of nodes:");
scanf("%d",&n);
printf("\n Enter the adjacency matrix:\n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
{
scanf("%d",&cost[i][j]);
if(cost[i][j]==0)
cost[i][j]=999;
}
visited[1]=1;
printf("\n");
while(ne<n)
{
for (i=1,min=999;i<=n;i++)

190303108068
for (j=1;j<=n;j++)

190303108068
if(cost[i][j]<min)
if(visited[i]!=0)
{
min=cost[i][j];
a=u=i;
b=v=j;
}
if(visited[u]==0 || visited[v]==0)
{
printf("\n Edge %d:(%d %d) cost:%d",ne++,a,b,min);
mincost+=min;
visited[b]=1;
}
cost[a][b]=cost[b][a]=999;
}
printf("\n Minimun cost=%d",mincost);
getch();
}

190303108068
Output:

190303108123
Pankaj Yadav

Time Complexity of Prim’s algorithm is O(V^2)

P R Ps Total

190303108068
PRACTICAL: 7
Aim: Implementation and Time analysis of Depth First Search (DFS)Graph
Traversal.
Code:
#include <stdio.h>
#include <stdlib.h>
int source,V,E,time,visited[20],G[20][20];
void DFS(int i)
{
int j;
visited[i]=1;
printf(" %d->",i+1);
for(j=0;j<V;j++)
{
if(G[i][j]==1&&visited[j]==0)
DFS(j);
}
}
int main()
{
int i,j,v1,v2; printf("\t\t\
tGraphs\n");
printf("190303108068\n");
printf("Hetvi gohil");
printf("Enter the no of edges:");
scanf("%d",&E);
printf("Enter the no of vertices:");
scanf("%d",&V);

190303108068
for(i=0;i<V;i++)
{
for(j=0;j<V;j++) G[i][j]=0;
}
for(i=0;i<E;i++)
{
printf("Enter the edges (format: V1 V2) : ");
scanf("%d%d",&v1,&v2);
G[v1-1][v2-1]=1;
}
for(i=0;i<V;i++)
{
for(j=0;j<V;j++) printf(" %d ",G[i][j]);
printf("\n");
}
printf("Enter the source: ");
scanf("%d",&source);
DFS(source-1);
return 0;
}

190303108068
Output:

190303108068
Hetvi gohil

Time complexity for this is O(V + E), where V is the number of vertices and E
is the number of edges in the graph.
And Space Complexity is O(V).

P R Ps Total

190303108068
PRACTICAL: 8
Aim: Implementation and Time analysis of Breadth First Search (BFS) Graph
Traversal.
Code:
#include <stdio.h>
#include <stdlib.h>
#define SIZE 40
struct queue {
int items[SIZE];
int front;
int rear;
};
struct queue* createQueue();
void enqueue(struct queue* q, int);
int dequeue(struct queue* q);
void display(struct queue* q);
int isEmpty(struct queue* q);
void printQueue(struct queue* q);
struct node {
int vertex;
struct node* next;
};
struct node* createNode(int);
struct Graph {
int numVertices;
struct node** adjLists;
int* visited;
};

190303108068
void bfs(struct Graph* graph, int startVertex) {
struct queue* q = createQueue();
graph->visited[startVertex] = 1;
enqueue(q, startVertex);
while (!isEmpty(q)) {
printQueue(q);
int currentVertex = dequeue(q);
printf("Visited %d\n", currentVertex);
struct node* temp = graph->adjLists[currentVertex];
while (temp) {
int adjVertex = temp->vertex;
if (graph->visited[adjVertex] == 0) {
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}
struct node* createNode(int v) {
struct node* newNode = malloc(sizeof(struct node));
newNode->vertex = v;
newNode->next = NULL;
return newNode;
}
struct Graph* createGraph(int vertices) {
struct Graph* graph = malloc(sizeof(struct Graph));

190303108068
graph->numVertices = vertices;

graph->adjLists = malloc(vertices * sizeof(struct node*));


graph->visited = malloc(vertices * sizeof(int));
int i;
for (i = 0; i < vertices; i++) {
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}
return graph;
}
void addEdge(struct Graph* graph, int src, int dest) {
struct node* newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}
struct queue* createQueue() {
struct queue* q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;
return q;
}
int isEmpty(struct queue* q) {
if (q->rear == -1)
return 1;

190303108068
else
return 0;
}
void enqueue(struct queue* q, int value) {
if (q->rear == SIZE - 1)
printf("\nQueue is Full!!");
else {
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}
int dequeue(struct queue* q) {
int item;
if (isEmpty(q))
{ printf("Queue is
empty"); item = -1;
} else {
item = q->items[q->front];
q->front++;
if (q->front > q->rear)
{ printf("Resetting queue
"); q->front = q->rear = -1;
}
}
return item;

190303108068
}
void printQueue(struct queue* q) {
int i = q->front;]
if (isEmpty(q)) { printf("Queue is
empty");
} else {
printf("\nQueue contains \n");
for (i = q->front; i < q->rear + 1; i++) {
printf("%d ", q->items[i]);
}
}
}

int main() {
struct Graph* graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);
bfs(graph, 0);
return 0;
}

190303108068
Output:

The time complexity of the BFS algorithm is represented in the form


of O(V + E)
The space complexity of the algorithm is O(V)

P R Ps Total

190303108068
PRACTICAL: 9
Aim: Implement Topological Sort algorithm.

Code:
#include <stdio.h>
int main(){
int i,j,k,n,a[10][10],indeg[10],flag[10],count=0;
printf("Enter the no of vertices:\n");
scanf("%d",&n);
printf("Enter the adjacency matrix:\n");
for(i=0;i<n;i++){
printf("Enter row %d\n",i+1);
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
for(i=0;i<n;i++){
indeg[i]=0;
flag[i]=0;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
indeg[i]=indeg[i]+a[j][i];
printf("\nThe topological order is:");
while(count<n){
for(k=0;k<n;k++){
if((indeg[k]==0) && (flag[k]==0)){
printf("%d ",(k+1));
flag [k]=1;
}
for(i=0;i<n;i++){
if(a[i][k]==1)
indeg[k]--;
}
}
count++;
}
return 0;
}

190303108068
Output:

P R Ps Total

190303108068
PRACTICAL: 10
Aim: Write a program to find out whether there exist cycle in given graph or
not.
Code:
#include<iostream>
#include<set>
#define NODE 5
using namespace
std;
int graph[NODE][NODE] = {
{0, 1, 0, 0, 0},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{0, 1, 0, 0, 1},
{0, 0, 1, 1, 0}
};
bool dfs(int vertex, set<int>&visited, int parent) {
visited.insert(vertex);
for(int v = 0; v<NODE; v++) {
if(graph[vertex][v]) {
if(v == parent)
continue;
if(visited.find(v) != visited.end())
return true;
if(dfs(v, visited, vertex))
return true;
}
}

190303108068
return false;

190303108068
}
bool hasCycle()
{ set<int>
visited;
for(int v = 0; v<NODE; v++)
{ if(visited.find(v) != visited.end())
continue;
if(dfs(v, visited, -1)) {
return true;
}
}
return false;
}
int main() {
bool res;
res = hasCycle();
if(res)
cout << "The graph has cycle." << endl;
else
cout << "The graph has no cycle." << endl;
}

190303108068
Output:

P R Ps Total

190303108068
190303108068
190303108068
190303108068
190303108068
190303108068
190303108068

You might also like