ADVANCED DATA STRUCTURES AND ALGORITHMS LAB PROGRAMS
ADVANCED DATA STRUCTURES AND ALGORITHMS LAB PROGRAMS
1.Binary Search :-
#include<stdio.h>
void Bsearch(int A[],int s,int e,int ele);
int main()
{
int i,s,e,n;
printf("Enter the size of array : ");
scanf("%d",&n);
int A[n];
printf("Enter the array : ");
for(i=0;i<n;i++)
{scanf("%d",&A[i]);}
printf("Enter the element to search : ");
int ele;
scanf("%d",&ele);
s=0,e=n-1;
Bsearch(A,s,e,ele);
return 0;
}
void Bsearch(int A[],int s,int e,int ele)
{
if(s==e)
{
if(A[s]==ele)
{
printf("Element found at index %d.\n",e);
}
else
{
printf("Element not found.\n");
}
}
int mid = (s+e)/2;
if(ele<A[mid])
{
Bsearch(A,s,mid,ele);
}
else
{
Bsearch(A,mid+1,e,ele);
}
}
2.Merge Sort :-
#include<stdio.h>
int n;
void Merge(int a[],int S,int M,int E)
{
int b[n];
int i=S,j=M+1,k=S,x;
int main(){
int i;
printf("Enter the size of array : ");
scanf("%d",&n);
int a[n];
printf("Enter the array elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Original array: ");
for(i=0;i<n;i++){printf("%d ",a[i]);}
MergeSort(a,0,n-1);
printf("\nSorted array: ");
for(i=0;i<n;i++){printf("%d ",a[i]);}
}
3.Quick Sort :-
#include<stdio.h>
int n;
int swap(int *x,int *y)
{ int temp=*x;
*x=*y;
*y=temp;}
int partition(int a[],int S,int E)
{
int p=a[S];
int i=S,j=E;
while(i<j)
{while(a[i]<=p && i<=E){i++;}
4.Min-max :-
#include<stdio.h>
int min,max;
void minmax(int a[],int S,int E)
{
if(S==E)
{
min=max=a[S];
}
else if(S==E-1)
{
min=a[S]<a[E]?a[S]:a[E];
max=a[S]>a[E]?a[S]:a[E];
}
else
{
int M=(S+E)/2;
minmax(a,S,M);
int min1=min;
int max1=max;
minmax(a,M+1,E);
if(min1<min)
{
min=min1;
}
if(max<max1)
{
max=max1;
}
}
}
void main()
{
int n,i;
printf("Enter the size of array : ");
scanf("%d",&n);
int a[n];
printf("Enter the array elements : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
minmax(a,0,n-1);
printf("Minimum in the array is %d.\nMaximum in the array is %d.\n",min,max);
}
totalValue+=items[bestIndex].value*((float)remainingCapacity/items[bestIndex].weight);
weights[bestIndex]=remainingCapacity;
remainingCapacity=0;
}
}
printf("The selected weights are :\n");
for(i=0;i<n;i++)
{
printf("obj %d :- %.2f ",i+1,weights[i]);
}
printf("\nThe maximum profit is %.2f\n",totalValue);
}
NOTE :-
Remaining programs :-
1.Kruskal’s Algorithm
2.Strassian’s Matrix multiplication.