Experiment - 1: Write A Program To Implement Insertion Sort
Experiment - 1: Write A Program To Implement Insertion Sort
2100271540049
Experiment -1
Write a program to implement insertion sort.
#include <stdio.h>int
main()
int i, j, k, n;
scanf("%d",&n);
int a[n];
scanf("%d",&a[i]);
for(i=1;i<n;i++)
k=a[i];j=i-
1;
a[j+1]=a[j]; j=j-
1;
a[j+1]=k;
1
for(i=0; i<n; i++)
printf("%d\t",a[i]);
return 0;
Output:
12 45 10 8 46 33
8 10 12 33 45 46
2
Sarthak Srivastava
2100271540049
Experiment - 2
Write a program to implement merge sort.
#include <stdio.h>
#include <stdlib.h>
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {if
(L[i] <= R[j]) {
arr[k] = L[i];i++;
}
else {
arr[k] = R[j];j++;
} k++;
}
3
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
Output:
Given array is
12 11 13 5 6 7
Sorted array is
5 6 7 11 12 13
4
Sarthak Srivastava
2100271540049
Experiment - 3
Write a program in C to implement Quick sort.
#include<stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int n;
printf("Enter the total no. of elements in array: ");scanf("%d",
&n);
int a[n];
printf("Enter array elements: ");
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Array before sorting: [ ");
5
for(int i = 0; i < n; i++)
{
printf("%d", a[i]);if(i
!= n-1)
printf(", ");
}
printf("]");
quicksort(a, 0, n - 1);
printf("\nArray after sorting: [ ");
for(int i = 0; i < n; i++)
{
printf("%d", a[i]);if(i
!= n-1)
printf(", ");
}
printf("]");
return 0;
}
Output:
Enter the total no. of elements in array: 6
Enter array elements: 12 45 34 11 6 38
Array before sorting: [ 12, 45, 34, 11, 6, 38]
Array after sorting: [ 6, 11, 12, 34, 38, 45]
6
Experiment – 4
Write a program to implement Heap Sort.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
if(largest != i)
{
swap(&a[i], &a[largest]);
heapify(a, n, largest);
}
}
int main()
{
int n;
printf("Enter the total no. of elements in array: ");
scanf("%d", &n);
7
int a[n];
printf("Enter array elements: ");
for(int i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
printf("Array before sorting: [ ");
for(int i = 0; i < n; i++)
{
printf("%d",
a[i]);if(i != n-1)
printf(", ");
}
printf("]");
heapsort(a, n);
printf("\nArray after sorting: [ ");
for(int i = 0; i < n; i++)
{
printf("%d",
a[i]);if(i != n-1)
printf(", ");
}
printf("]");
}
Output:
Enter the total no. of elements in array:
6Enter array elements: 12 46 34 10 6
25
Array before sorting: [ 12, 46, 34, 10, 6, 25]
Array after sorting: [ 6, 10, 12, 25, 34, 46]
8
Sarthak Srivastava
2100271540049
Experiment - 5
Write a program to implement counting sort.
#include <stdio.h>int
main()
int i, j, n;
scanf("%d", &n);
int input[n];
scanf("%d", &input[i]);
= 1; i < n; i++)
max=input[i];
c = 0;
9
{
if(input[j] == i)
c++;
count[i] = c;
int output[n];
output[count[input[i]] - 1] = input[i];
count[input[i]] = count[input[i]] - 1;
Output:
10
Sarthak Srivastava
2100271540049
Experiment - 6
Write a program to implement shell sort.
#include<stdio.h>
tmp = a[i];
a[j-increment];
else
break;
a[j] = tmp;
int main()
11
int i, n, a[10];
scanf("%d",&n);
= 0; i < n; i++)
scanf("%d",&a[i]);
ShellSort(a,n);
= 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
Output:
elements : 12 34 78 6 33 27
12
Sarthak Srivastava
Experiment-7 2100271540049
#include <limits.h>
#include <stdio.h>
if (i == j)
return 0;
min = count;
return min;
int main()
int arr[] = { 1, 2, 3, 4, 3 };
return 0;
Output:
13
Sarthak Srivastava
Experiment-8 2100271540049
Write a program in C to implement Kruskal’s minimum spanning tree algorithm.
#include<stdio.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()
scanf("%d",&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;
while(ne < n)
for(i=1,min=999;i<=n;i++)
min=cost[i][j];
a=u=i;
b=v=j;
u=find(u);
v=find(v);
if(uni(u,v))
mincost +=min;
cost[a][b]=cost[b][a]=999;
int find(int i)
while(parent[i])
i=parent[i];
return i;
if(i!=j)
parent[j]=i;
return 1;
15
}
return 0;
Output:
123050
269404
207412
375000
315703
007080
1 edge (3,5) =1
2 edge (5,2) =1
3 edge (1,2) =2
4 edge (3,6) =2
5 edge (4,1) =3
Minimum cost = 9
16