0% found this document useful (0 votes)
100 views22 pages

762 Daalab2

This lab record documents Pratik Dhanuka's algorithms lab experiments for his fifth semester of B.Tech in computer engineering at KIIT Deemed to be University. The experiments include determining if a number is prime using different algorithms, calculating greatest common divisor using Euclid's, consecutive integer checking and middle school algorithms, and measuring the time complexity of insertion sort on arrays with ascending, descending and random data. Performance results are presented in tables and graphs.

Uploaded by

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

762 Daalab2

This lab record documents Pratik Dhanuka's algorithms lab experiments for his fifth semester of B.Tech in computer engineering at KIIT Deemed to be University. The experiments include determining if a number is prime using different algorithms, calculating greatest common divisor using Euclid's, consecutive integer checking and middle school algorithms, and measuring the time complexity of insertion sort on arrays with ascending, descending and random data. Performance results are presented in tables and graphs.

Uploaded by

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

LAB RECORD

B.TECH (Fifth Semester)


Algorithms Laboratory (CS-2098)
School of Computer Engineering
KIIT, Deemed to be University

NAME- Pratik Dhanuka


Roll Number- 1905762
Section- (CS-11)
Prof. Debbrota Paul Chowdhury

Algorithms Lab Record


SCHOOL OF COMPUTER ENGINEERING
KALINGA INSTITUTE OF INDUSTRIAL TECHNOLOGY
DEEMED TO BE UNIVERSITY, BHUBANESWAR - 24
Review of Fundamentals of Data
Structure – LAB 2
2.1)Write a program to test whether a number n, entered through keyboard
is prime or not by using different algorithms you know for at least 10
inputs and note down the time complexity by step/frequency count method
for each algorithm & for each input. Finally make a comparison of time
complexities found for different inputs, plot an appropriate graph & decide
which algorithm is faster.

CODE -
#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int Algo1(int number)

int i, count = 0;

for(i=2; i<number; i++)

if(number%i == 0)

count=1;

}i

f(number == 1)

count = 1;

return count;

}
int Algo2(int number)

int i, count = 0;

for(i=2; i<number/2; i++)

if(number%i == 0)

count=1;

break;

if(number == 1)

count = 1;

return count;

int Algo3(int number, int i)

if (number <= 2)

return (number == 2) ? 1: 0;

if (number % i == 0)

return 0;

if (i * i > number)

return 1;

return Algo3(number, i + 1);


}

int main()

int number,t ;

clock_t start,end;

double cputime;

printf("Enter number: ");

scanf("%d",&number);

printf("\n");

//Algo1

start=clock();

t=Algo1(number);

if( t == 0)

printf("%d is a prime number.\n", number);

else

printf("%d is not a prime number.\n", number);

end=clock();

cputime=(double)(end-start)/CLOCKS_PER_SEC;

printf("algo1 --> total cpu time in sec:%f\n\n",cputime);

//Algo2

start=clock();

t=Algo2(number);

if( t == 0)

printf("%d is a prime number.\n", number);


else

printf("%d is not a prime number.\n", number);

end=clock();

cputime=((double)(end-start))/CLOCKS_PER_SEC;

printf("algo2 --> total cpu time in sec:%f\n\n",cputime);

//Algo3

start=clock();

if (Algo3(number,2))

printf("%d is a prime number\n",number);

else

printf("Not a prime number\n");

end=clock();

cputime=((double)(end-start))/CLOCKS_PER_SEC;

printf("algo3 --> total cpu time in sec:%f\n",cputime);

return 0;

}
OUTPUT –

GRAPH -
2.2) Write a program to find out GCD (greatest common divisor) using the
following three algorithms.

a) Euclid’s algorithm

b) Consecutive integer checking algorithm.

c) Middle school procedure which makes use of common prime factors. For
finding list of primes implement sieve of Eratosthenes algorithm.

Write a program to find out which algorithm is faster for the following data.
Estimate how many times it will be faster than the other two by
step/frequency count method in each case.

i. Find GCD of two numbers when both are very large i.e. GCD (31415,
14142) by applying each of the above algorithms.

ii. Find GCD of two numbers when one of them can be very large i.e. GCD
(56,32566) or GCD(34218, 56) by applying each of the above algorithms.

iii. Find GCD of two numbers when both are very small i.e. GCD(12,15) by
applying each of the above algorithms.

iv. Find GCD of two numbers when both are same i.e. GCD(31415, 31415)
or GCD(12, 12) by applying each of the above algorithms.

Write the above data in the following format and decide which algorithm is
faster for the particular data.

CODE –
#include <stdio.h>

#include<stdlib.h>

#include<time.h>

#define MAXFACTORS 1024

//Euclid algorithm

int Euclid(int x, int y)

if (y == 0)

return x;
else if (x >= y && y > 0)

return Euclid(y, (x % y));

//Consecutive Integer Checking algorithm

int CIC(int m, int n)

int t;

if (m < n)

t = m;

else

t = n;

while (t != 1)

if (m%t==0 && n%t==0)

return t;

t = t - 1;

//Middle School Procedure algorithm

typedef struct{

int size;

int factor[MAXFACTORS + 1];

int exponent[MAXFACTORS + 1];

} FACTORIZATION;
void FindFactorization(int x, FACTORIZATION* factorization)

int i, j = 1;

int n = x, c = 0;

int k = 1;

factorization->factor[0] = 1;

factorization->exponent[0] = 1;

for (i = 2; i <= n; i++) {

c = 0;

while (n % i == 0) {

c++;

n = n / i;

}i

f (c > 0) {

factorization->exponent[k] = c;

factorization->factor[k] = i;

k++;

} factorization->size =

1;

}i

nt MSP(int m, int n)
{

FACTORIZATION mFactorization, nFactorization;

int r, mi, ni, i, k, x = 1, j;

FindFactorization(m, &mFactorization);

FindFactorization(n, &nFactorization);

int min;

i = 1;

j = 1;

while (i <= mFactorization.size && j <= nFactorization.size) {

if (mFactorization.factor[i] < nFactorization.factor[j])

i++;

else if (nFactorization.factor[j] < mFactorization.factor[i])

j++;

else{

min = mFactorization.exponent[i] > nFactorization.exponent[j] ?

nFactorization.exponent[j] : mFactorization.exponent[i];

x = x * mFactorization.factor[i] * min;

i++;

j++;

return x;

int main()
{i

nt a,b;

clock_t start,end;

double total_cputime;

printf("\nEnter number 1: ");

scanf("%d",&a);

printf("\nEnter number 2: ");

scanf("%d",&b);

//Euclid

printf("\nFOR EUCLID ALGORITHM");

start=clock();

int gcd1=Euclid(a,b);

printf("\nGcd:%d\n",gcd1);

end=clock();

printf("Start Time = %ld\n",start);

printf("End Time = %ld\n",end);

total_cputime=(double)(end-start);

printf("Total CPU Time = %f\n",total_cputime);

total_cputime=((double)(end-start))/CLOCKS_PER_SEC;

printf("Total CPU Time in Sec. = %f\n",total_cputime);

printf("\n\n");

//Consecutive Integer Checking

printf("\nFOR CONSECUTIVE INTEGER CHECKING


ALGORITHM");
start=clock();

int gcd2=CIC(a,b);

printf("\nGcd:%d\n",gcd2);

end=clock();

printf("Start Time = %ld\n",start);

printf("End Time = %ld\n",end);

total_cputime=(double)(end-start);

printf("Total CPU Time = %f\n",total_cputime);

total_cputime=((double)(end-start))/CLOCKS_PER_SEC;

printf("Total CPU Time in Sec. = %f\n",total_cputime);

printf("\n\n");

//Middle School Method

printf("\nFOR MIDDLE SCHOOL METHOD ALGORITHM");

start=clock();

int gcd3=MSP(a,b);

printf("\nGcd:%d\n",gcd3);

end=clock();

printf("Start Time = %ld\n",start);

printf("End Time = %ld\n",end);

total_cputime=(double)(end-start);

printf("Total CPU Time = %f\n",total_cputime);

total_cputime=((double)(end-start))/CLOCKS_PER_SEC;

printf("Total CPU Time in Sec. = %f\n",total_cputime);

printf("\n\n");
return 0;

OUTPUT –
TABLE –
HOME EXERCISE -

2.3)Write a menu driven program as given below, to sort an array of n


integers in ascending order by insertion sort algorithm and determine the
time required (in terms of step/frequency count) to sort the elements.
Repeat the experiment for different values of n and different nature of data
(i.e. apply insertion sort algorithm on the data of array that are already
sorted, reversely sorted and random data). Finally plot a graph of the time
taken versus n for each type of data. The elements can be read from a file
or can be generated using the random number generator.

CODE –
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int ch,n=5000;

clock_t start,end;

void insertionSort(int arr[], int n){

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

while (j >= 0 && arr[j] > key) {

arr[j + 1] = arr[j];

j = j - 1;

arr[j + 1] = key;

}
void decresing(int arr[],int n){

int a;

for (int i = 0; i < n; ++i)

for (int j = i + 1; j < n; ++j)

if (arr[i] < arr[j])

a = arr[i];

arr[i] = arr[j];

arr[j] = a;

double timeascen(int arr[]){

insertionSort(arr, n);

start=clock();

insertionSort(arr, n);

end=clock();

return (double)(end-start)/CLOCKS_PER_SEC;

double timedescen(int arr[]){

decresing(arr, n);
start=clock();

insertionSort(arr, n);

end=clock();

return (double)(end-start)/CLOCKS_PER_SEC;

double timerand(int arr[]){

start=clock();

insertionSort(arr, n);

end=clock();

return (double)(end-start)/CLOCKS_PER_SEC;

}i

nt main()

int arr[n],arr1[n];

double totalcpu;

while(1){

printf("Enter your choice: ");

scanf("%d",&ch);

switch(ch){

case 0:{

exit(0);

case 1:{

srand(time(NULL));
for(int i=0;i<n;i++)

arr[i]=rand()%n;

printf("Array is generated for %d\n",n);

break;

case 2:{

printf("The elements are: \n");

for(int i=0;i<n;i++)

printf("%d\n",arr[i]);

break;

case 3:{

insertionSort(arr, n);

printf("The sorted elements are: \n");

for(int i=0;i<n;i++)

printf("%d\n",arr[i]);

break;

case 4:{

decresing(arr, n);

printf("The sorted elements are: \n");

for(int i=0;i<n;i++)

printf("%d\n",arr[i]);

break;
}

case 5:{

totalcpu=timerand(arr);

printf("Total CPU Time in Sec.(Random Data) = %f\n",totalcpu);

break;

case 6:{

totalcpu=timeascen(arr);

printf("Total CPU Time in Sec.(Ascending Sorted Data) =


%f\n",totalcpu);

break;

case 7:{

totalcpu=timedescen(arr);

printf("Total CPU Time in Sec.(Descending Sorted Data) =


%f\n",totalcpu);

break;

case 8:{

int

inputcase[10]={5000,10000,15000,20000,25000,30000,35000,4
0000,45000,

50000};

printf("n Randomdata In Descending In Ascending\n");

for(int i=0;i<10;i++){
printf("%d ",inputcase[i]);

//Random data

totalcpu=timerand(arr1);

printf("%f ",totalcpu);

//Descending data

totalcpu=timedescen(arr);

printf("%f ",totalcpu);

//Ascending data

totalcpu=timeascen(arr);

printf("%f ",totalcpu);

printf("\n");

break;

}}

return 0;

}
OUTPUT –

GRAPH -

TABLE -
Sl Value of n Time Complexity Time Complexity Time Complexity
No. (Data in Ascending) (Data in descending) (Random Data)
1 5000 0.000024 0.041080 0.021093

2 10000 0.000046 0.162470 0.082199

3 15000 0.000125 0.483918 0.236109

4 20000 0.000090 0.669699 0.326679

5 25000 0.000128 1.067751 0.534011

6 30000 0.000134 1.451958 0.732054


7 35000 0.000159 1.996108 0.997238

8 40000 0.000246 2.591937 1.283326

9 45000 0.000249 3.527495 2.105550

10 50000 0.000255 4.071260 2.037928

You might also like