0% found this document useful (0 votes)
73 views13 pages

Insertion Sort Program

The document contains code snippets and algorithms for insertion sort, Prim's algorithm, Kruskal's algorithm, and Dijkstra's algorithm. It includes C code to implement insertion sort to sort an array of integers in ascending order. It also includes pseudocode and C code implementations of Prim's, Kruskal's and Dijkstra's algorithms for finding minimum spanning trees and shortest paths in graphs.

Uploaded by

Ekansh Gupta
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)
73 views13 pages

Insertion Sort Program

The document contains code snippets and algorithms for insertion sort, Prim's algorithm, Kruskal's algorithm, and Dijkstra's algorithm. It includes C code to implement insertion sort to sort an array of integers in ascending order. It also includes pseudocode and C code implementations of Prim's, Kruskal's and Dijkstra's algorithms for finding minimum spanning trees and shortest paths in graphs.

Uploaded by

Ekansh Gupta
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/ 13

Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………

INSERTION SORT PROGRAM


#include <stdio.h>

int main()

int n, array[1000], c, d, t;

printf("Enter number of elements\n");

scanf("%d", &n);

printf("Enter %d integers\n", n);

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

scanf("%d", &array[c]);

for (c = 1 ; c <= n - 1; c++) {

d = c;

while ( d > 0 && array[d] < array[d-1]) {

t = array[d];

array[d] = array[d-1];

array[d-1] = t;

d--;

printf("Sorted list in ascending order:\n");

for (c = 0; c <= n - 1; c++) {

printf("%d\n", array[c]);

return 0;

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………

INSERTION SORT ALGORITHM


1. insertionSort(array)
2. mark first element as sorted
3. for each unsorted element X
4. 'extract' the element X
5. for j <- lastSortedIndex down to 0
6. if current element j > X
7. move sorted element to the right by 1
8. break loop and insert X here
9. end insertionSort

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………

PRIM’S ALGORITHM PROGRAM


#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()

clrscr();

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++)

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

if(cost[i][j]<min)

if(visited[i]!=0) {

min=cost[i][j];

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


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();

PRIM’S ALGORITHM
ABES Engineering College Sign of Faculty With Date
Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


Step 1: Select a starting vertex

Step 2: Repeat Steps 3 and 4 until there are fringe vertices

Step 3: Select an edge e connecting the tree vertex and fringe vertex that has minimum weight

Step 4: Add the selected edge and the vertex to the minimum spanning tree T

[END OF LOOP]

Step 5: EXIT.

KRUSKAL’S ALGORITHM

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


1. MST-KRUSKAL(G, w)
2. A ← Ø
3. for each vertex v ¬ V[G]
4. do MAKE-SET(v)
5. sort the edges of E into nondecreasing order by weight w
6. for each edge (u, v) ¬ E, taken in nondecreasing order by weight
7. do if FIND-SET(u) ≠ FIND-SET(v)
8. then A ← A ¬ {(u, v)}
9. UNION(u, v)
10. return A

KRUSKAL’S ALGORITHM PROGRAM

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


#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);

void main()

//clrscr();

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");

while(ne < n)

for(i=1,min=999;i<=n;i++)

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


{

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);

getch();

int find(int i)

while(parent[i])

i=parent[i];

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


return i;

int uni(int i,int j)

if(i!=j)

parent[j]=i;

return 1;

return 0;

DIJKSTRA’S ALGORITHM PROGRAM

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


#include<stdio.h>

#include<conio.h>

#define INFINITY 9999

#define MAX 10

void dijikstra(int G[MAX][MAX], int n, int startnode);

void main(){

int G[MAX][MAX], i, j, n, u;

// clrscr();

printf("\nEnter the no. of vertices:: ");

scanf("%d", &n);

printf("\nEnter the adjacency matrix::\n");

for(i=0;i < n;i++)

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

scanf("%d", &G[i][j]);

printf("\nEnter the starting node:: ");

scanf("%d", &u);

dijikstra(G,n,u);

getch();

void dijikstra(int G[MAX][MAX], int n, int startnode)

int cost[MAX][MAX], distance[MAX], pred[MAX];

int visited[MAX], count, mindistance, nextnode, i,j;

for(i=0;i < n;i++)

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

if(G[i][j]==0)

cost[i][j]=INFINITY;

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


else

cost[i][j]=G[i][j];

for(i=0;i< n;i++)

distance[i]=cost[startnode][i];

pred[i]=startnode;

visited[i]=0;

distance[startnode]=0;

visited[startnode]=1;

count=1;

while(count < n-1){

mindistance=INFINITY;

for(i=0;i < n;i++)

if(distance[i] < mindistance&&!visited[i])

mindistance=distance[i];

nextnode=i;

visited[nextnode]=1;

for(i=0;i < n;i++)

if(!visited[i])

if(mindistance+cost[nextnode][i] < distance[i])

distance[i]=mindistance+cost[nextnode][i];

pred[i]=nextnode;

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


count++;

for(i=0;i < n;i++)

if(i!=startnode)

printf("\nDistance of %d = %d", i, distance[i]);

printf("\nPath = %d", i);

j=i;

do

j=pred[j];

printf(" <-%d", j);

while(j!=startnode);

DIJKSTRA’S ALGORITHM

ABES Engineering College Sign of Faculty With Date


Roll No:………………………………Date:……………………….Page No:…………………..

Practical Name:............................................................ ………………........................…Practical No:……………


1. DIJKSTRA(G,s)
2. INITIALIZE-SINGLE-SOURCE(G, S)
3. S←Ø
4. Q ← V[G]
5. while Q ≠ Ø
6. do u ← EXTRACT-MIN(Q)
7. S ← S U {u}
8. for each vertex v ∈ Adj[u]
9. do if dist[v] > dist[u] + w(u,v)
10. then d[v] ←d[u] + w(u,v)

ABES Engineering College Sign of Faculty With Date

You might also like