You are on page 1of 21

MAHILA ENGINEERING COLLEGE ,AJMER

ANALYSIS OF ALGORITHM

ASSIGNMENT

Batch: A2

Submitted by: Submitted To:


Name: Dipansha Choudhary Kuldeep Goswami
Class: CSE-A Assistant Professor
RollNo.:20EEMCS029
Year: 3rd (V Sem)
Q1. Explain asymptotic notations and its usefulness in Algorithms.
Ans. Asymptotic Notations
Asymptotic notations are used to represent the complexities of algorithms for asymptotic
analysis. These notations are mathematical tools to represent the complexities. There are
three notations that are commonly used.
Big Oh Notation
Big-Oh (O) notation gives an upper bound for a function f(n) to within a constant factor.

We write f(n) = O(g(n)), If there are positive constantsn0 and c such that, to the right of n0
the f(n) always lies on or below c*g(n).
O(g(n)) = { f(n) : There exist positive constant c and n0 such that 0 ≤ f(n) ≤ c g(n), for all n ≥
n0}
Big Omega Notation
Big-Omega (Ω) notation gives a lower bound for a function f(n) to within a constant factor.

We write f(n) = Ω(g(n)), If there are positive constantsn0 and c such that, to the right of n0
the f(n) always lies on or above c*g(n).
Ω(g(n)) = { f(n) : There exist positive constant c and n0 such that 0 ≤ c g(n) ≤ f(n), for all n ≥
n0}
Big Theta Notation
Big-Theta(Θ) notation gives bound for a function f(n) to within a constant factor.
We write f(n) = Θ(g(n)), If there are positive constantsn0 and c1 and c2 such that, to the
right of n0 the f(n) always lies between c1*g(n) and c2*g(n) inclusive.
Θ(g(n)) = {f(n) : There exist positive constant c1, c2 and n0 such that 0 ≤ c1 g(n) ≤ f(n) ≤ c2
g(n), for all n ≥ n0}

Asymptotic notation describes the runtime of an algorithm based on the increasing input size
of the algorithm. Asymptotic notation is important in computer science, as it helps engineers
gauge the efficiency of the algorithms they write.

Q2. What do you understand by efficiency of algorithm ? How can it be defined as time
and space complexity with respect to ‘n’ i.e , no. of inputs.
Ans. Algorithmic efficiency is a property of an algorithm which relates to the amount
of computational resources used by the algorithm. An algorithm must be analyzed to
determine its resource usage, and the efficiency of an algorithm can be measured based on the
usage of different resources. Algorithmic efficiency can be thought of as analogous to
engineering productivity for a repeating or continuous process.
For maximum efficiency it is desirable to minimize resource usage. However, different
resources such as time and space complexity cannot be compared directly, so which of two
algorithms is considered to be more efficient often depends on which measure of efficiency is
considered most important.
For example, bubble sort and time sort are both algorithms to sort a list of items from
smallest to largest. Bubble sort sorts the list in time proportional to the number of elements
squared (, see Big O notation), but only requires a small amount of extra memory which is
constant with respect to the length of the list (). Time sort sorts the list in time linear
eithmic (proportional to a quantity times its logarithm) in the list's length (), but has a space
requirement linear in the length of the list (). If large lists must be sorted at high speed for a
given application, time sort is a better choice; however, if minimizing the memory footprint
of the sorting is more important, bubble sort is a better choice.
Space Complexity
Space complexity of an algorithm represents the amount of memory space needed the
algorithm in its life cycle.
Space needed by an algorithm is equal to the sum of the following two components
A fixed part that is a space required to store certain data and variables (i.e. simple variables
and constants, program size etc.), that are not dependent of the size of the problem.
A variable part is a space required by variables, whose size is totally dependent on the size of
the problem. For example, recursion stack space, dynamic memory allocation etc.
Space complexity S(p) of any algorithm p is S(p) = A + Sp(I) Where A is treated as the fixed
part and S(I) is treated as the variable part of the algorithm which depends on instance
characteristic I. Following is a simple example that tries to explain the concept
Algorithm
SUM(P, Q)
Step 1 - START
Step 2 - R ← P + Q + 10
Step 3 - Stop
Here we have three variables P, Q and R and one constant. Hence S(p) = 1+3. Now space is
dependent on data types of given constant types and variables and it will be multiplied
accordingly.
Time Complexity
Time Complexity of an algorithm is the representation of the amount of time required by the
algorithm to execute to completion. Time requirements can be denoted or defined as a
numerical function t(N), where t(N) can be measured as the number of steps, provided each
step takes constant time.
For example, in case of addition of two n-bit integers, N steps are taken. Consequently, the
total computational time is t(N) = c*n, where c is the time consumed for addition of two bits.
Here, we observe that t(N) grows linearly as input size increases.

Q3. What do you understand by static and dynamic memory allocation? How can it be
implemented in C. Swap two number using call by value and call by reference.
Ans. Static Memory Allocation
In static memory allocation whenever the program executes it fixes the size that the
program is going to take, and it can’t be changed further. So, the exact memory
requirements must be known before. Allocation and deallocation of memory will be done
by the compiler automatically. When everything is done at compile time (or) before run
time, it is called static memory allocation.
Key Features:
 Allocation and deallocation are done by the compiler.
 It uses a data structures stack for static memory allocation.
 Variables get allocated permanently.
 No reusability.
 Execution is faster than dynamic memory allocation.
 Memory is allocated before runtime.
 It is less efficient.
#include <stdio.h>
#include <stdlib.h>
int main()
{ int size;
printf("Enter limit of the text: \n");
scanf("%d", &size);
char str[size];
printf("Enter some text: \n");
scanf(" ");
gets(str);
printf("Inputted text is: %s\n", str);
return 0;
}

Dynamic Memory Allocation


In Dynamic memory allocation size initialization and allocation are done by the
programmer. It is managed and served with pointers that point to the newly allocated
memory space in an area which we call the heap. Heap memory is unorganized and it is
treated as a resource when you require the use of it if not release it. When everything is
done during run time or execution time it is known as Dynamic memory allocation.
Key Features:
 Dynamic allocated at runtime
 We can also reallocate memory size if needed.
 Dynamic Allocation is done at run time.
 No memory wastage

#include <stdio.h>
#include <stdlib.h>
int main()
{ int size, resize;
char* str = NULL;
printf("Enter limit of the "
"text: \n");
scanf("%d", &size);
str = (char*)malloc(size * sizeof(char));
if (str != NULL)
{ printf("Enter some text: \n");
scanf(" ");
gets(str);
printf("Inputted text by allocating"
"memory using malloc() is: "
"%s\n",
str);
}
free(str);
str = (char*)calloc(50, sizeof(char));
if (str != NULL)
{ printf("Enter some text: \n");
scanf(" ");
gets(str);
printf("Inputted text by allocating "
"memory using calloc() is: " "%s\n",str);
}
printf("Enter the new size: \n");
scanf("%d", &resize);
str = (char*)realloc(str, resize * sizeof(char));
printf("Memory is successfully "
"reallocated by using "
"realloc() \n");
if (str != NULL)
{ printf("Enter some text: \n");
scanf(" ");
gets(str);
printf("Inputted text by reallocating"
" memory using realloc()is: " "%s\n", str);
}
free(str);
str = NULL;
return 0;
}

Swap two number using call by reference.


#include <stdio.h>
void swap(int * num1, int * num2);
int main()
{
int num1, num2;

printf("Enter two numbers: ");


scanf("%d%d", &num1, &num2);
printf("Before swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

swap(&num1, &num2);
printf("After swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

return 0;
}

void swap(int * num1, int * num2)


{
int temp;
temp = *num1;
*num1= *num2;
*num2= temp;

printf("After swapping in swap function n");


printf("Value of num1 = %d \n", *num1);
printf("Value of num2 = %d \n\n", *num2);
}

C program to swap two numbers using call by value

#include<stdio.h>

void swap(int,int);

void main( )

{ int n1,n2;

printf("Enter the two numbers to be swapped\n");

scanf("%d%d",&n1,&n2);
printf("\nThe values of n1 and n2 in the main function before calling the swap function are
n1=%d n2=%d",n1,n2);

swap(n1,n2);

printf("\nThe values of n1 and n2 in the main function after calling the swap function are
n1=%d n2=%d",n1,n2);

void swap(int n1,int n2)

{ int temp;

temp=n1;

n1=n2;

n2=temp;

printf("\nThe values of n1 and n2 in the swap function after swapping are n1=%d n2=
%d",n1,n2);

Q4. Write a program to implement scope of variables.


Ans. Scope of Variable in C
Global variable

#include<stdio.h>

int num1, num2 ;

void main()
{ void addition() ;

void subtraction() ;

void multiplication() ;

num1 = 30 ;

num2 = 60 ;

printf("num1 = %d, num2 = %d", num1, num2) ;

addition() ;

subtraction() ;

multiplication() ;

void addition()

{ int result ;

result = num1 + num2 ;

printf("\naddition = %d", result) ;

void subtraction()

{ int result ;

result = num1 - num2 ;

printf("\nsubtraction = %d", result) ;

void multiplication()

{ int result ;

result = num1 * num2 ;

printf("\nmultiplication = %d", result) ;

}
Local variable
#include<stdio.h>
void main()
{ void addition() ;
int num1, num2 ;
num1 = 30 ;
num2 = 50 ;
printf("num1 = %d, num2 = %d", num1, num2) ;
addition() ;
}
void addition()
{ int sumresult ; int num1; int num2;
sumresult = num1 + num2 ;
printf("\n addition = %d", sumresult) ;
}

Q5.Write a program to observe size of datatype with their range (min. and max. value).
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <float.h>
int main( void )
{
printf( "Ranges for integer data types in C \n" );
printf( "--------------\n");
printf( "int8_t %20d %20d\n" , SCHAR_MIN , SCHAR_MAX );
printf( "int16_t %20d %20d\n" , SHRT_MIN , SHRT_MAX );
printf( "int32_t %20d %20d\n" , INT_MIN , INT_MAX );
printf( "int64_t %20lld %20lld\n" , LLONG_MIN , LLONG_MAX );
printf( "uint8_t %20d %20d\n" ,0 , UCHAR_MAX );
printf( "uint16_t %20d %20d\n" ,0 , USHRT_MAX );
printf( "uint32_t %20d %20u\n" ,0 , UINT_MAX );
printf( "uint64_t %20d %20llu\n" , 0 , ULLONG_MAX );
printf( "\n" );
printf( "===============\n");
printf( "Ranges for real number data types in C\n\n" );
printf( "---------------\n");
printf( "float %14.7g %14.7g\n" , FLT_MIN , FLT_MAX );
printf( "double %14.7g %14.7g\n" , DBL_MIN , DBL_MAX );
printf( "long double %14.7Lg %14.7Lg\n" , LDBL_MIN , LDBL_MAX );
printf( "\n" );
return 0;
}
Q6. Write a program to find the mean and the median of the numbers stored in an
array.
Ans.
#include <stdio.h>
void Array_sort(int *array , int n)
{ int i=0 , j=0 , temp=0;
for(i=0 ; i<n ; i++)
{
for(j=0 ; j<n-1 ; j++)
{
if(array[j]>array[j+1])
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
printf("\nThe array after sorting is..\n");
for(i=0 ; i<n ; i++)
{
printf("\narray_1[%d] : %d",i,array[i]);
}
}
float Find_median(int array[] , int n)
{ float median=0;
if(n%2 == 0)
median = (array[(n-1)/2] + array[n/2])/2.0;
else
median = array[n/2];
return median;
}
int main()
{ int array_1[30] = {0};
int i=0 ,n=0;
float median=0;
printf("\nEnter the number of elements for the array : ");
scanf("%d",&n);
printf("\nEnter the elements for array_1..\n");
for(i=0 ; i<n ; i++)
{ printf("array_1[%d] : ",i);
scanf("%d",&array_1[i]);
}
Array_sort(array_1 , n);
median = Find_median(array_1 , n);
printf("\n\nThe median is : %f\n",median);
return 0;
}
C Program to to find the mean of n numbers using array
#include<stdio.h>
int main( )
{ int a[25], n, i ,avg;
float mean = 0, sum = 0 ;
printf(" Enter the Numbers of terms: ") ;
scanf("%d ",& n) ;
printf("\n Enter the Numbers : \n") ;
for (i = 1 ; i <= n ; i++)
{
scanf("%d ",& a[i]) ;
}
for(i = 1 ; i <= n ; i++)
{ sum = sum + a[i] ;
avg = sum / n ;}
printf("\n Mean of entered Numbers are : %f ",mean) ;
return ( 0 ) ;
}

Q7. Write a program to insert one element in an array and delete an element from an –
array.
Ans.
#include <stdio.h>
#include <stdlib.h>
int main()
{ int a[100];
int element,i,loc,size,n,j,choice;
printf("C Program to Insert and Delete an Element in an Array using switch case\n");
printf("1. Inserting an Element in an Array\n");
printf("2. Deleting an Element in an Array\n");
printf("Select your choice : ");
scanf("%d",&choice);
switch(choice)
{ case 1:
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter %d array elements\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("List before Insertion: ");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to insert\n");
scanf("%d",&element);
printf("Enter a position to insert an element %d\n",element);
scanf("%d",&loc);
loc--;
for(i=size-1;i>=loc;i--)
{
a[i+1]=a[i];
}
a[loc]=element;
printf("List after Insertion: ");
for(i=0;i<size+1;i++)
{
printf("%d ",a[i]);
}
break;
case 2:
printf("Enter the size of an array\n");
scanf("%d",&size);
printf("Enter elements\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("List before deletion\n");
for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\nEnter an element to delete\n");
scanf("%d",&n);
for(i=0;i<size;i++)
{
if(a[i]==n)
{
for(j=i;j<(size-1);j++)
{
a[j]=a[j+1];
}
break;
}
}
printf("List after deletion\n");
for(i=0;i<(size-1);i++)
{
printf("%d ",a[i]);
}
break;
default:
printf("Wrong choice, Please try again later");
}
return 0;
}
Q8. Write a program to implement recursion with single call by itself.
Ans.
#include <stdio.h>
int sum(int n);
int main()
{ int number, result;
printf("Enter a positive integer: ");
scanf("%d", &number);
result = sum(number);
printf("sum = %d", result);
return 0;
}
int sum(int n)
{ if (n != 0)
return n + sum(n-1);
else
return n;
}
Q9. Write a program to implement recursion with multiple call by itself.
Ans.
#include <stdio.h>
int mystery (int a, int b)
{ if (b == 0)
return 0;
if (b % 2 == 0)
return mystery (a+a, b/2);
return mystery (a+a, b/2) + a;
}
int main()
{ int i = 12, j = 11;
printf("mystery of %d and %d is: %d\n", i, j, mystery (12, 11));
}

You might also like