Untitled Document

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Prg1

#include<stdio.h>
#include<time.h>
int main()
{
int a[10000],i,j,temp,num;
float clock_t,st,et;
printf("Enter the size of the elements \n");
scanf("%d",&num);
for(i=0;i<num;i++)
{
a[i]=rand()%10000;
}
printf("Elements before Sorting \n");
for(i=0;i<num;i++)
{
printf("%d",a[i]);
}
st=clock();
for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
et=clock();
printf("\nElements after sorting\n");
for(i=0;i<num;i++)
{
printf("a[%d]=\t%d\t",i,a[i]);

}
printf("\nTIME TAKEN : %f",(et-st)/CLOCKS_PER_SEC);
return 0;
}

Prg2

#include<stdio.h>
#include<time.h>
int i,j,temp,num,p;
int partition(int a[],int start,int end)
{
int pivot=a[end];
i=(start-1);
for(j=start;j<=end;j++)
{
if(a[j]<pivot)
{
i=i+1;
temp =a[i];
a[i]=a[j];
a[j]=temp;

}
}
temp=a[i+1];
a[i+1]=a[end];
a[end]=temp;

return (i+1);
}

void quicksort(int a[],int start,int end)


{
if(start<end)
{
p=partition(a,start,end);
quicksort(a,start,p-1);
quicksort(a,p+1,end);
}

void printArray(int a[],int n)


{
int i;
for(i=0;i<n;i++)
printf("%d ",a[i]);
}

int main()
{
int a[20000],n,i;
float clock_t,st,et;
float t;
printf("Enter the number of elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
a[i]=rand()%20000;

printf("Elements before sorting \n");


printArray(a,n);
st=clock();

quicksort(a,0,n-1);
et=clock();
printf("\nElements after sorting \n");
t=((double)(et-st))/CLOCKS_PER_SEC;
printArray(a,n);
printf("TIME TAKEN = %f",t);
return 0;

Prg3

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void merge(int a[],int beg,int mid, int end)
{
int i,j,k;
int n1,n2;
int L[n1],R[n2];
n1=mid-beg+1;
n2=end-mid;
for(i=0;i<n2;i++)
{
L[i]=a[beg+i];

}
for(j=0;j<n2;j++)
{
R[n2]=a[mid+1+j];

}
i=0;
j=0;
k=beg;
while(i<n1 && j<n2)
{
if(L[i]<=R[j])
{
a[k]=L[i];
i++;
}
else
{
a[k]=R[j];
j++;

}
k++;

}
}
void mergeSort(int a[],int beg,int end)
{
if(beg<end)
{
int mid=(beg+end)/2;
mergeSort(a,beg,mid);
mergeSort(a,mid+1,end);
merge(a,beg,mid,end);
}
}
int main()
{
int i,n;
clock_t st,et;
double time_taken;
printf("Enter the number of elements to sort(>5000):");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
arr[i]=rand()%10000;
}
printf("Elements before Sorting\n ");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
st=clock();
mergeSort(arr,0,n-1);

et=clock();
printf("\nElements after merge Sorting\n");
for(i=0;i<n;i++)
{
printf("a[%d]=\t%d\t",i,arr[i]);

}
time_taken=((double)(et-st))/CLOCKS_PER_SEC;
printf("\nTime taken to sort %d elements is %lf seconds ",n,time_taken);

return 0;

Prg4

#include<stdio.h>
#include<stdlib.h>
int s[10],d[10];
void dijkstra(int cost[10][10],int n,int src)
{
int i, min, ct, u;
for(i=1;i<=n;i++)
{
s[i]=0;
d[i]=cost[src][i];
}
s[src]=1;
ct=1;
while(ct<=n)
{
min=999;
for(i=1;i<=n;i++)
{
if(d[i]<min && s[i]==0)
{
min=d[i];
u=i;
}
}
s[u]=1;
ct++;
for(i=1;i<=n;i++)
{

if((d[u]+cost[u][i]<d[i]) && !s[i])


d[i]=d[u]+cost[u][i];
}
}
}
int main()
{
int j,i,n;
int a[10][10];
int src;
printf("Enter the Size of the matrix:");
scanf("%d",&n);
printf("\nEnter the cost of adjacency matrix:\n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&a[i][j]);
if(a[i][j]==0)
a[i][j]=999;
}
}
printf("\nEnter the starting vertex:");
scanf("%d",&src);
dijkstra(a,n,src);
printf("\nShortest Path from :\n");
for(j=1;j<=n;j++)
{ if(j!=src)
printf("%d to %d : %d\n",src,j,d[j]);
}
return 0;
}

You might also like