DSA Notes Unit3 5
DSA Notes Unit3 5
index
Scanned by CamScanner
Requirements : A is a sorted array in
ascending(increasing) order
index
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
<
<
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
C Programs for searching and sorting
//program for linear search
#include<stdio.h>
#define N 5
int main()
{
int arr[N];
int item,i,loc =-1;
for(i=0;i<N;i++)
{
if(arr[i]==item)
{
loc = i;
printf("\nItem found at location = %d\n",loc);
break;
}
}
if(loc==-1)
{
printf("\nItem not found\n");
}
return(0);
}
//program for binary search
#include<stdio.h>
#define N 10
int main()
{
int arr[N];
int item,loc =-1;
int m,i, f = 0, l = N-1;
if(arr[m]==item)
{
loc = m;
printf("\nItem found at location = %d\n",loc);
break;
}
else if(item<arr[m])
{
l = m - 1;
}
else
{
f = m + 1;
}
}
if(loc==-1)
{
printf("\nItem not found\n");
}
return(0);
}
//program for bubble sort
#include<stdio.h>
#define N 5
int main()
{
int arr[N];
int i,j,temp;
for(i=0;i<N-1;i++)
{
for(j=0;j<N-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return(0);
}
//program for selection sort
#include<stdio.h>
#define N 5
int main()
{
int arr[N];
int i,j,temp,min;
for(i=0;i<N-1;i++)
{
min = i;
for(j=i+1;j<N;j++)
{
if(arr[j]<arr[min])
{
min = j;
}
}
if(min!=i)
{
temp = arr[i];
arr[i] = arr[min]; //swapping minimum element with element at index i
arr[min] = temp;
}
}
return(0);
}
//program for insertion sort
#include<stdio.h>
#define N 5
int main()
{
int arr[N];
int i,j,key;
for(i=1;i<N;i++)
{
key = arr[i];
j = i-1;
while(j>=0 && key<arr[j])
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=key;
}
return(0);
}
//program for counting sort
#include<stdio.h>
#define N 10
int main()
{
int arr[N];
int k,i;
int b[N]; //b is output array
k = arr[0];
for(i = 1; i<N; i++) //finding maximum value k = range = [0...k] = max value among the given array numbers
{
if(arr[i]>k)
{
k = arr[i];
}
}
//for turbo compiler create dynamically like int * count = (int *) malloc((k+1)*sizeof(int));
for(i=0;i<N;i++)
{
count[arr[i]]++; //count array now contains frequencies of all elements
}
for(i=1;i<=k;i++)
{
count[i] = count[i] + count[i-1]; //update count array// cumulitive frequencies
}
for(i=N-1;i>=0;i--) //create an output array
{
b[--count[arr[i]]] = arr[i];
}
return(0);
}
// C Program for quicksort
#include<stdio.h>
#define N 10
int main()
{
int arr[N];
int i;
quicksort(arr,0,N-1);
return(0);
}
}
temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
return(i);
}
//program for heapsort sort
#include<stdio.h>
#define N 10
void heapsort(int [], int);
void build_max_heap(int [], int);
void max_heapify(int [], int);
int heap_size;
int main()
{
int arr[N];
int i;
heapsort(arr,N);
build_max_heap(arr, n);
l = 2*i + 1;
r = 2*i + 2;
if(l<heap_size && arr[l] > arr[i])
{
largest = l;
}
else
{
largest = i;
}
if (largest != i)
{
temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
max_heapify(arr, largest);
}
}
Scanned by CamScanner
Scanned by CamScanner
Example of Kruskal’s Algorithm to nd minimum spanning tree from a
given graph
2
2 2
Shaded edges of
graph in fig (n)
Figure 1. The execu on of Kruskal’s algorithm on the graph from Figure. Shaded edges gives MST
belong to the forest A being grown. The edges are considered by the algorithm in sorted order by
weight. An arrow points to the edge under considera on at each step of the algorithm. If the edge
joins two dis nct trees in the forest, it is added to the forest, thereby merging the two trees.
can take
this edge
also
which
will give
another
MST
of weight
37
Shaded edges of
graph in fig (i) gives
MST
ready
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner
Scanned by CamScanner