Chapter 1 - Concept of Data Type
Chapter 1 - Concept of Data Type
Concept of data type. Basic and user defined data types. Concept of data structure and its uses. Definition and use of ADT. Benefits of using ADTs.
Concept of dynamic memory allocation.
Definition of algorithm. What is a good algorithm? Different structures used in algorithms.
Time and space complexity. Big Oh (O) notation.
Data Type
● Simple data structure can be constructed with the help of primitive data structure.
● A primitive data structure used to represent the standard data types of any one of the computer languages.
● Variables, arrays, pointers, structures, unions, etc. are examples of primitive data structures.
● Compound data structure can be constructed with the help of any one of the primitive data structure and it
is having a specific functionality.
● It can be designed by user. It can be classified as
○ Linear data structure
○ Non-linear data structure
● Linear data structures can be constructed as a continuous arrangement of data elements in the memory.
● It can be constructed by using array data type.
● In the linear Data Structures the relationship of adjacency is maintained between the data elements.
Classification of Data Structures [3]
● The following list of operations applied on linear data structures
1. Add an element
2. Delete an element
3. Traverse
4. Sort the list of elements
5. Search for a data element
For example Stack, Queue, Tables, List, and Linked Lists.
Classification of Data Structures [4]
Non-linear Data Structure:
● Non-linear data structure can be constructed as a collection of randomly distributed set of data item
joined together by using a special pointer (tag).
● In non-linear Data structure the relationship of adjacency is not maintained between the data items.
● The following list of operations applied on non-linear data structures.
1. Add elements
2. Delete elements
• The elements of the array can be of any valid type- integers, characters, floating-
There are two types of operators to access members of a structure. Which are:
● 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.
Time Complexity[2]
Determined in terms of 3 types of time:
1. Worst-case running time: The worst-case running time of an algorithm is an upper bound on the
running time for any input.
Therefore, having the knowledge of worst-case running time gives us an assurance that the algorithm will
never go beyond this time limit.
2. Average-case running time: The average-case running time of an algorithm is an estimate of the running
time for an `average' input.
It specifies the expected behaviour of the algorithm when the input is randomly drawn from a given
distribution. Average-case running time assumes that all inputs of a given size are equally likely.
3. Best-case running time: The term 'best-case performance' is used to analyse an algorithm under optimal
conditions.
For example, the best case for a simple linear search on an array occurs when the desired element is the
first in the list.
=>It is always recommended to improve the average performance and the worst-case performance of an
algorithm.
Space Complexity
● Space complex of an algorithm represents the amount of memory space needed by 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:
● 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.
Asymptotic Notations
● Asymptotic analysis of an algorithm refers to defining the mathematical
bounding /framing of its run-time performance.
● Asymptotic analysis is input bound i.e., if there's no input to the algorithm, it is
concluded to work in a constant time.
● Other than the "input" all other factors are considered constant.
● Asymptotic analysis refers to computing the running time of any operation in
mathematical units of computation.
● The Running time of Algorithm is defined as : the time needed by an algorithm
in order to deliver its output when presented with legal input .
● In Asymptotic notation, algorithm is treated as a function.
● Usually, the time required by an algorithm falls under three types
○ Best Case — Minimum time required for program execution.
○ Average Case — Average time required for program execution.
○ Worst Case — Maximum time required for program execution
Types of Asymptotic Notations
5.
Lab 1
1. Write a program in C to get 2 numbers and display the max and min.
2. Write a program in C to find out the factorial of a user entered number using
iterative method.
3. Perform 2 using recursive function.
4. Write a program in C to get “n” numbers and obtain their sum, average, max and min using
the concept of dynamic memory allocation. (malloc)
5. Implement array as an ADT. Your Array ADT should implement the following functions:
a. Creating of an array
b. Inserting new element at required position
c. Deletion of any element
d. Modification of any element
e. Traversing of an array
f. Merging of arrays