Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
3.3 kB
17
Indexable
Never
4
#include<stdio.h>

#include<time.h>

#include<stdlib.h>
void quicksort(int a[],int low,int high);
int partition(int a[],int low,int high);
void swap(int*,int*);
void quicksort(int a[],int low,int high)
{
if(low<high)
{
int pi = partition(a,low,high);
quicksort(a,low,pi-1);
quicksort(a,pi+1,high);
}
}
void swap(int *a,int *b)
{
int c=*a;
*a=*b;
*b=c;
}
int partition(int a[],int low,int high)
{
int pivot=a[high];
int i=low-1;
for(int j=low;j<=high-1;j++)
{
if(a[j]<=pivot)
{
i++;
swap(&a[i],&a[j]);
}
}
swap(&a[i+1],&a[high]); return (i+1);
}
void main()
{
srand(time(NULL));
int a[100000],n=100000;

int elements[n];

for(int i=0;i<n;i++)
 {
 elements[i]=rand()%1000;
 }
 for(int size=5000;size<=n;size+=2000)
{
int arr[size];
for(int i=0;i<size;i++)
{
arr[i]=elements[i];
}
clock_t start=clock();
quicksort(a,0,size);
clock_t end=clock();
printf("Total time taken to sort %d elements is %lf\n",size,((double)(end-
start)/CLOCKS_PER_SEC));
}
}


5
#include<stdlib.h>

#include<stdio.h>

#include<time.h>
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// Create temp arrays
int L[n1], R[n2];
// Copy data to temp array
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
// Merge the temp arrays
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++;
}
// Copy the remaining elements of L[]
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
// Copy the remaining elements of R[]
while (j < n2)

{

arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r)
{
// Finding mid element
int m = l+(r-l)/2;
// Recursively sorting both the halves
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
void main()
{
srand(time(NULL));
int arr[100000],n=100000,i,size;
int elements[n];
for(i=0;i<n;i++)
elements[i]=rand()%1000;
for(size=5000;size<n;size+=5000)
{
arr[size];
for(i=0;i<n;i++)
{
arr[i]=elements[i];
}
clock_t start=clock();
mergeSort(arr, 0,size);
clock_t end=clock();
printf("Total time taken to sort %d elements is %lf\n",size,((double)(end-
start)/CLOCKS_PER_SEC));
}


6
Program A:
#include<stdio.h>
#include<conio.h>
#define INF 999

int min(int a,int b)
{
	return(a<b)?a:b;
}

void floyd(int p[][10],int n)
{
	int i,j,k;
	for(k=1;k<=n;k++)
	for(i=1;i<=n;i++)
	for(j=1;j<=n;j++)
	p[i][j]=min(p[i][j],p[i][k]+p[k][j]);
}

void main()
{
	int a[10][10],n,i,j;
	clrscr();
	printf("\nEnter the n value:");
	scanf("%d",&n);
	printf("\nEnter the graph data:\n");
	for(i=1;i<=n;i++)
	for(j=1;j<=n;j++)
	scanf("%d",&a[i][j]);
	floyd(a,n);
	printf("\nShortest path matrix\n");
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
    printf("%d ",a[i][j]);
		printf("\n");
	}
	getch();
}


b
Program B
#include<stdio.h>
void warsh(int p[][10],int n)
{
	int i,j,k;
	for(k=1;k<=n;k++)
	for(i=1;i<=n;i++)
	for(j=1;j<=n;j++)
	p[i][j]=p[i][j] || p[i][k] && p[k][j];
 }

int main()
{
	int a[10][10],n,i,j;
	printf("\nEnter the n value:");
	scanf("%d",&n);
	printf("\nEnter the graph data:\n");
	for(i=1;i<=n;i++)
	for(j=1;j<=n;j++)
	scanf("%d",&a[i][j]);
	warsh(a,n);
	printf("\nResultant path matrix\n");
	for(i=1;i<=n;i++)
	{
		for(j=1;j<=n;j++)
		printf("%d ",a[i][j]);
		printf("\n");
	}
	return 0;
	}
Leave a Comment