Unit 1
Unit 1
and Algorithms
Unit – 1 : Session – 9 : SLO-2
SRMInstitute of ScienceandTechnology 1
Time-Space Trade-Off in Algorithms.
A tradeoff is a situation where one thing increases and another thing decreases. It is a
way to solve a problem in:
Either in less time and by using more space, or In very little space by spending a long
amount of time.
SRMInstitute of ScienceandTechnology 2
Compressed or Uncompressed data
In this case, storing only the source and rendering it as an image would
take more space but less time i.e., storing an image in the cache is faster
than re-rendering but requires more space in memory.
SRMInstitute of ScienceandTechnology 3
Smaller code or Loop Unrolling
Smaller code occupies less space in memory but it requires high computation
time that is required for jumping back to the beginning of the loop at the end
of each iteration. Loop unrolling can optimize execution speed at the cost of
increased binary size. It occupies more space in memory but requires less
computation time.
SRMInstitute of ScienceandTechnology 4
21CSC201J – Data Structures
and Algorithms
Unit – 1 : Session – 8 : SLO-2
SRMInstitute of ScienceandTechnology 1
BIG Ω NOTATION(Ω ): Ω notation provides an asymptotic lower bound.
Ω(g(n)) = { f(n): there exist positive constants c and n0 such that 0 ≤ cg(n)
≤ f(n) for all n≥ n0}
SRMInstitute of ScienceandTechnology 2
Θ NOTATION: Θ((g(n)) = {f(n): there exist positive constants c1, c2
and n0 such that 0 ≤ c1*g(n) ≤ f(n) ≤ c2*g(n) for all n≥n0}
SRMInstitute of ScienceandTechnology 3
21CSC201J – Data Structures
and Algorithms
Unit – 1 : Session – 8: SLO-1
SRMInstitute of ScienceandTechnology 1
MATHEMATICAL NOTATION
Ο Notation
Ω Notation
θ Notation
There are 3 types of asymptotic notations:
• Big O notation
• Ω notation
• θ notation
SRMInstitute of ScienceandTechnology 2
Big Oh Notation, Ο It measures the worst case time complexity or
longest amount of time an algorithm can possibly take to complete.
O(g(n)) = { f(n): there exist positive constants c and n0 such that 0 <=
f(n) <= cg(n) for all n >= n0}
SRMInstitute of ScienceandTechnology 3
21CSC201J – Data Structures
and Algorithms
Unit – 1 : Session – 9 : SLO-1
SRMInstitute of ScienceandTechnology 1
Time and Space Complexity
1. Time Complexity
2. 2. Space Complexity
SRMInstitute of ScienceandTechnology 2
Space Complexity
The amount of memory space required by the algorithm in its life cycle. A
fixed part : For example simple variables & constant used and program
size etc. A variable part : For example dynamic memory allocation,
recursion stacks space etc.
SRMInstitute of ScienceandTechnology 3
Time Complexity - T(n)
The amount of time required by the algorithm to run to completion. T(n) can
be measured as the number of steps, provided each step consumes
constant time.
ALGORITHM ANALYSIS
SRMInstitute of ScienceandTechnology 4
18CSC201J – Data Structures
and Algorithms
struct point {
int value;
};
int main()
{
struct point s;
return 0;
}
In the above code s is an instance of struct point and ptr is the struct pointer because it is storing the address of
struct point.
Accessing the Structure Member with the Help of Pointers
There are two ways to access the members of the structure with the help of a structure pointer:
With the help of (*) asterisk or indirection operator and (.) dot operator.
With the help of ( -> ) Arrow operator.
Below is the program to access the structure members using the structure pointer with the help of the dot operator.
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
int main()
{
struct Student s1; struct Student* ptr = &s1;
s1.roll_no = 27;
strcpy(s1.name, "Kamlesh Joshi");
strcpy(s1.branch, "Computer Science And Engineering");
s1.batch = 2019;
printf("Roll Number: %d\n", (*ptr).roll_no);
printf("Name: %s\n", (*ptr).name);
printf("Branch: %s\n", (*ptr).branch);
printf("Batch: %d", (*ptr).batch); return 0;
}
Output:
1
Below is the program to access the structure members using the structure pointer with the help of the Arrow operator.
In this program, we have created a Structure Student containing structure variable s. The Structure Student has
roll_no, name, branch, and batch.
#include <stdio.h>
#include <string.h>
struct Student {
int roll_no;
char name[30];
char branch[40];
int batch;
};
struct Student s, *ptr;
int main()
{
ptr = &s;
printf("Enter the Roll Number of Student\n"); scanf("%d", &ptr->roll_no);
printf("Enter Name of Student\n"); scanf("%s", &ptr->name);
printf("Enter Branch of Student\n"); scanf("%s", &ptr->branch);
printf("Enter batch of Student\n"); scanf("%d", &ptr->batch);
printf("\nStudent details are: \n");
printf("Roll No: %d\n", ptr->roll_no);
printf("Name: %s\n", ptr->name);
printf("Branch: %s\n", ptr->branch);
printf("Batch: %d\n", ptr->batch);
return 0;
}
Output:
Enter the Roll Number of Student
27
Enter Name of Student
Kamlesh_Joshi
Enter Branch of Student
Computer_Science_And_Engineering
Enter batch of Student
2019
Student details are:
Roll No: 27
Name: Kamlesh_Joshi
Branch: Computer_Science_And_Engineering
18CSC201J – Data Structures
and Algorithms
Syntax of char
Syntax of float
int main()
{
float a = 9.0f;
float b = 2.5f;
// 2x10^-4
float c = 2E-4f;
printf("%f\n", a);
printf("%f\n", b);
printf("%f", c);
Output
9.000000
return 0;
2.500000
}
0.000200
Double Data Type
A Double data type in C is used to store decimal numbers (numbers with floating point
values) with double precision. It is used to define numeric values which hold numbers
with decimal values in C.
The double data type is basically a precision sort of data type that is capable of holding
64 bits of decimal numbers or floating points. Since double has more precision as
compared to that float then it is much more obvious that it occupies twice the memory
occupied by the floating-point type. It can easily accommodate about 16 to 17 digits after
or before a decimal point.
Range: 1.7E-308 to 1.7E+308
Size: 8 bytes
Format Specifier: %lf
Syntax of Double
The variable can be declared as double precision floating point using the double
keyword:
double var_name;
// C Program to demonstrate
// use of double data type
#include <stdio.h>
int main()
{
double a = 123123123.00;
double b = 12.293123;
double c = 2312312312.123123;
printf("%lf\n", a);
printf("%lf\n", b);
printf("%lf", c);
Output
123123123.000000
return 0;
12.293123
}
2312312312.123123
Void Data Type
The void data type in C is used to specify that no value is present. It does not provide a
result value to its caller. It has no values and no operations. It is used to represent nothing.
Void is used in multiple ways as function return type, function arguments as void,
and pointers to void.
Syntax:
// function return type void
int print(void);
int main()
{
int val = 30;
void* ptr = &val;
printf("%d", *(int*)ptr);
return 0;
}Output
30
Size of Data Types in C // C Program to print size of
Types Description
Primitive data types are the most basic data types that are
Primitive Data Types used for representing simple values such as integers, float,
characters, etc.
User Defined Data Types The user-defined data types are defined by the user himself.
The data types that are derived from the primitive or built-in
Derived Types
datatypes are referred to as Derived Data Types.
The following are some main primitive data types in C:
Integer Data Type
The integer datatype in C is used to store the whole numbers without decimal values. Octal values, hexadecimal
values, and decimal values can be stored in int data type in C.
• Range: -2,147,483,648 to 2,147,483,647
• Size: 4 bytes
• Format Specifier: %d
Syntax of Integer
The queue abstract data type (ADT) follows the basic design of the stack
abstract data type.
Each node contains a void pointer to the data and the link pointer to the
next element in the queue. The program’s responsibility is to allocate
memory for storing the data.
dequeue() – Remove and return the first element of the queue, if the
queue is not empty.
Disadvantages:
Overhead
Complexity
Learning
Limited Flexibility
Cost
The program allocates memory for the data and address is passed to
the stack ADT.
The head node and the data nodes are encapsulated in the ADT. The
calling function can only see the pointer to the stack.
The stack head structure also contains a pointer to top and count of
number of entries currently in stack.
pop() – Remove and return the element at the top of the stack, if it
is not empty.
Abstract Data type (ADT) is a type (or class) for objects whose behavior is
defined by a set of values and a set of operations.
It does not specify how data will be organized in memory and what
algorithms will be used for implementing the operations.
List ADT,
Stack ADT,
Queue ADT.
Syntax of malloc() in C
ptr = (cast-type*) malloc(byte-size)
For Example:
else {
printf("Memory has been successfully "
"reallocated using realloc:\n");
printf(
"\n base address of marks are:%pc",
marks); ////print the base or
///beginning address of
///allocated memory
}
}
} while (ans == 1);
// print the marks of the students
for (i = 0; i <= index; i++) {
printf("marks of students %d are: %d\n ", i,
marks[i]);
}
free(marks);
}
return 0;
}
Syntax of malloc() in C
ptr = (cast-type*) malloc(byte-size)
For Example:
else {
printf("Memory has been successfully "
"reallocated using realloc:\n");
printf(
"\n base address of marks are:%pc",
marks); ////print the base or
///beginning address of
///allocated memory
}
}
} while (ans == 1);
// print the marks of the students
for (i = 0; i <= index; i++) {
printf("marks of students %d are: %d\n ", i,
marks[i]);
}
free(marks);
}
return 0;
}