0% found this document useful (0 votes)
3 views56 pages

DSA Notes Unit3 5

The document contains multiple C programs demonstrating various algorithms for searching and sorting, including linear search, binary search, bubble sort, selection sort, insertion sort, counting sort, quicksort, and heapsort. Additionally, it provides examples of Kruskal's and Prim's algorithms for finding the minimum spanning tree in a graph. Each program includes user input for an array and outputs the sorted array or the location of the searched item.

Uploaded by

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

DSA Notes Unit3 5

The document contains multiple C programs demonstrating various algorithms for searching and sorting, including linear search, binary search, bubble sort, selection sort, insertion sort, counting sort, quicksort, and heapsort. Additionally, it provides examples of Kruskal's and Prim's algorithms for finding the minimum spanning tree in a graph. Each program includes user input for an array and outputs the sorted array or the location of the searched item.

Uploaded by

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

for i = 0 to N-1

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;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

printf("\nEnter an item to be searched in the array\n");


scanf("%d",&item);

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;

printf("Enter an sorted array in ascending order of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

printf("\nEnter an item to be searched in the array\n");


scanf("%d",&item);
while(f<=l)
{
m = (f+l)/2;

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;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

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

printf("\nSorted array after bubble sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for selection sort
#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,temp,min;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

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

printf("\nSorted array after selection sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
//program for insertion sort

#include<stdio.h>

#define N 5

int main()
{
int arr[N];
int i,j,key;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

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

printf("\nSorted array after insertion sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

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

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

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];
}
}

int count[k+1]; //create count array of size k+1 range = [0...k]

//for turbo compiler create dynamically like int * count = (int *) malloc((k+1)*sizeof(int));

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


{
count[i] = 0; // Initialize count array with all zeros
}

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];
}

for(i = 0; i<N; i++) //copy output array to original array


{
arr[i] = b[i];
}

printf("\nSorted array after counting sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}
// C Program for quicksort

#include<stdio.h>

#define N 10

void quicksort(int [], int , int);


int partition(int [], int, int);

int main()
{
int arr[N];
int i;

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

quicksort(arr,0,N-1);

printf("\nSorted array after quick sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}

return(0);
}

void quicksort(int arr[], int p, int r)


{
int q;
if(p<r)
{
q = partition(arr,p,r);
quicksort(arr,p,q-1);
quicksort(arr,q+1,r);
}
}

int partition(int arr[], int p, int r)


{
int x,i,j,temp;
x = arr[r];
i =p;
for(j=p;j<r;j++)
{
if(arr[j]<=x)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
}

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

printf("Enter an array of size %d\n", N);


for(i = 0; i<N; i++)
{
scanf("%d",&arr[i]);
}

heapsort(arr,N);

printf("\nSorted array after heap sort\n\n");


for(i = 0; i<N; i++)
{
printf("%d\t",arr[i]);
}
return(0);
}

void heapsort(int arr[], int n)


{
int i,temp;

build_max_heap(arr, n);

for(i = n-1; i>=1; i--)


{
temp=arr[0];
arr[0]=arr[i];
arr[i]=temp;
heap_size--;
max_heapify(arr,0);
}
}

void build_max_heap(int arr[], int n)


{
int i;
heap_size = n;
for(i=(n-2)/2; i>=0; i--)
{
max_heapify(arr,i);
}
}

void max_heapify(int arr[], int i)


{
int l, r, largest, temp;

l = 2*i + 1;
r = 2*i + 2;
if(l<heap_size && arr[l] > arr[i])
{
largest = l;
}
else
{
largest = i;
}

if(r<heap_size && arr[r] > arr[largest])


{
largest = r;
}

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.

Minimum cost(weight) of the MST obtained above = 1+2+2+4+4+7+8+9 = 37


If edge weights are not dis nct, we can have more than 1 MST. It depends upon
which edge we picked to avoid cycle.
Scanned by CamScanner
Example of Prims’s Algorithm to nd minimum spanning tree from a
given graph

can take
this edge
also
which
will give
another
MST
of weight
37

Shaded edges of
graph in fig (i) gives
MST

Minimum cost(weight) of the MST obtained above = 4+8+2+4+2+1+7+9 = 37


Figure 2 The execu on of Prim’s algorithm on the graph from Fig. The root vertex is a.
Shaded edges are in the tree being grown, and the ver ces in the tree are shown in black. At each
step of the algorithm, the ver ces in the tree determine a cut of the graph, and a light edge crossing
the cut is added to the tree. In the second step, for example, the algorithm has a choice of adding
either edge (b, c) or edge ( a , h ) to the tree since both are light edges crossing the cut
or Searching

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

You might also like