Dsa Module 1
Dsa Module 1
Data may be organized in many different ways. The logical or mathematical model of a
particular organization of data is called a data structure.
The choice of a particular data model depends on the two considerations
1. It must be rich enough in structure to mirror the actual relationships of the data in the real
world. 2. The structure should be simple enough that one can effectively process the data
whenever necessary.
Basic Terminology: Elementary Data Organization:
Data: Data are simply values or sets of values.
Data items: Data items refers to a single unit of values. Data items that are divided into sub-
items are called Group items.
Ex: An Employee Name may be divided into three sub items- first name, middle name, and
last name.
Data items that are not able to divide into sub-items are called Elementary items.
Ex: SSN Entity: An entity is something that has certain attributes or properties which may be
assigned values. The values may be either numeric or non-numeric.
Ex: Attributes- Names, Age, Sex, SSN Values- Rohland Gail, 34, F, 134-34-5533
Entities with similar attributes form an entity set. Each attribute of an entity set has a range of
values, the set of all possible values that could be assigned to the particular attribute.
The term “information” is sometimes used for data with given attributes, of, in other words
meaningful or processed data.
Each record in a file may contain many field items but the value in a certain field may uniquely
determine the record in the file. Such a field K is called a primary key and the values k1, k2,
….. in such a field are called keys or key values.
Records may also be classified according to length. A file can have fixed-length records or
variable-length records.
• In fixed-length records, all the records contain the same data items with the same amount of
space assigned to each data item.
Example: Student records have variable lengths, since different students take different numbers
of courses.
1
Module 1- Introduction to Data Structures and Algorithms
The above organization of data into fields, records and files may not be complex enough to
maintain and efficiently process certain collections of data. For this reason, data are also
organized into more complex types of structures. The study of complex data structures includes
the following three
3. Quantitative analysis of the structure, which includes determining the amount of memory
needed to store the structure and the time required to process the structure.
2. Non- Primitive data Structures: Non-primitive data structures are those data
structures which are created using primitive data structures. Examples of non-primitive data
structures is the processing of complex numbers, linked lists, stacks, trees, and graphs.
Non-linear data structures in C store the data in a non-sequential manner. The data is
stored in multiple levels. The implementation of non-linear data structures is more complex
than linear data structures. Example - graphs, trees.
2
Module 1- Introduction to Data Structures and Algorithms
Based on the structure and arrangement of data, non-primitive data structures is further
classified into 1. Linear Data Structure 2. Non-linear Data Structure
1. Linear Data Structure: A data structure is said to be linear if its elements form a sequence or
a linear list. There are basically two ways of representing such linear structure in memory. 1.
One way is to have the linear relationships between the elements represented by means of
sequential memory location. These linear structures are called arrays. 2. The other way is to
have the linear relationship between the elements represented by means of pointers or links.
These linear structures are called linked lists. The common examples of linear data structure
are Arrays, Queues, Stacks, Linked lists
2. Non-linear Data Structure: A data structure is said to be non-linear if the data are not arranged
in sequence or a linear. The insertion and deletion of data is not possible in linear fashion. This
structure is mainly used to represent data containing a hierarchical relationship between
elements. Trees and graphs are the examples of non-linear data structure.
On the basis of size, the data structures in C can also be classified as:
The static nature of data structures is exhibited in the memory allocated to them. The size of
such data structures is fixed as the memory is allocated to them during the compile time.
However, the values of the elements stored are not static and can be modified at any time.
Example - Array.
The dynamic data structures in C are capable of resizing themselves during the run time of the
program. The memory space allocated to such data structures can be modified (increased or
decreased), thus providing more flexibility to manipulate the data stored in them. Example -
Linked List.
Arrays: The simplest type of data structure is a linear (or one dimensional) array. A list of a
finite number n of similar data referenced respectively by a set of n consecutive numbers,
usually 1, 2, 3 . . . . . . . n. if A is chosen the name for the array, then the elements of A are
denoted by subscript notation a1, a2, a3….. an or by the parenthesis notation A (1), A (2), A
(3) . . . . . . A (n) or by the bracket notation A [1], A [2], A [3] . . . . . . A [n]
Data Structures and Applications Linear arrays are called one-dimensional arrays because each
element in such an array is referenced by one subscript. A two-dimensional array is a collection
of similar data elements where each element is referenced by two subscripts.
Trees Data frequently contain a hierarchical relationship between various elements. The data
structure which reflects this relationship is called a rooted tree graph or a tree. Some of the
basic properties of tree are explained by means of examples
3
Module 1- Introduction to Data Structures and Algorithms
1. Stack: A stack, also called a fast-in first-out (LIFO) system, is a linear list in which insertions
and deletions can take place only at one end, called the top. This structure is similar in its
operation to a stack of dishes on a spring system as shown in fig. Note that new 4 dishes are
inserted only at the top of the stack and dishes can be deleted only from the top of the Stack.
2. Queue: A queue, also called a first-in first-out (FIFO) system, is a linear list in which
deletions can take place only at one end of the list, the "from'' of the list, and insertions can
take place only at the other end of the list, the “rear” of the list. This structure operates in much
the same way as a line of people waiting at a bus stop, as pictured in Fig. the first person in line
is the first person to board the bus. Another analogy is with automobiles waiting to pass through
an intersection the first car in line is the first car through.
3. Graph: Data sometimes contain a relationship between pairs of elements which is not
necessarily hierarchical in nature. For example, suppose an airline flies only between the cities
connected by lines in Fig. The data structure which reflects this type of relationship is called a
graph.
DATA STRUCTURES OPERATIONS The data appearing in data structures are processed
by means of certain operations.
1. Traversing: accessing each record/node exactly once so that certain items in the record may
be processed. (This accessing and processing is sometimes called “visiting” the record.)
2. Searching: Finding the location of the desired node with a given key value, or finding the
locations of all such nodes which satisfy one or more conditions.
1. Sorting: Arranging the records in some logical order (e.g., alphabetically according to some
NAME key, or in numerical order according to some NUMBER key, such as social security
number or account number)
2. Merging: Combining the records in two different sorted files into a single sorted file
4
Module 1- Introduction to Data Structures and Algorithms
5
Module 1- Introduction to Data Structures and Algorithms
6
Module 1- Introduction to Data Structures and Algorithms
Advantage:
Recall that abstraction is the idea of separating what something is from how it works, by
separating interface from implementation. Previously, we saw procedural abstraction, which
applies abstraction to computational processes. With procedural abstraction, we use functions
based on their signature and documentation without having to know details about their
definition.
The concept of abstraction can be applied to data as well. An abstract data type
(ADT) separates the interface of a data type from its implementation, and it encompasses both
the data itself as well as functionality on the data. An example of an ADT is the string type in
C++, used in the following code:
This code creates two strings and initializes them to represent different values, prints out one
of them, and compares the lengths of both – all without needing to any details about the
implementation of string . Rather, it relies solely on the interface provided by
the string abstraction
Algorithm
An algorithm is a finite set of instructions, those if followed, accomplishes a particular task. It
is not language specific, we can use any language and symbols to represent instructions.
The criteria of an algorithm
• Input: Zero or more inputs are externally supplied to the algorithm.
• Output: At least one output is produced by an algorithm.
• Definiteness: Each instruction is clear and unambiguous.
7
Module 1- Introduction to Data Structures and Algorithms
• Finiteness: In an algorithm, it will be terminated after a finite number of steps for all
different cases.
• Effectiveness: Each instruction must be very basic, so the purpose of those instructions
must be very clear to us.
Complexities of an Algorithm
The complexity of an algorithm computes the amount of time and spaces required by an
algorithm for an input of size (n). The complexity of an algorithm can be divided into two
types. The time complexity and the space complexity.
Time Complexity of an Algorithm
The time complexity is defined as the process of determining a formula for total time required
towards the execution of that algorithm. This calculation is totally independent of
implementation and programming language.
Space Complexity of an Algorithm
Space complexity is defining as the process of defining a formula for prediction of how much
memory space is required for the successful execution of the algorithm. The memory space is
generally considered as the primary memory.
8
Module 1- Introduction to Data Structures and Algorithms
9
Module 1- Introduction to Data Structures and Algorithms
10
Module 1- Introduction to Data Structures and Algorithms
11
Module 1- Introduction to Data Structures and Algorithms
1)#include <stdio.h>
• void main()
• {
• int i, n = 5;
• for (i = 1; i <= n; i++) {
• printf("FACE Prep n");
• }}
complexity is O(n)
2) #include <stdio.h>
int main()
{
int a = 4;
int b = 6;
int c;
c = a + b;
printf(%d, c);
}
Time Complexity Calculation: The time complexity of the above-given program is O(1), as
this program consists of only assignment, arithmetic operations and all those will be executed
only once.
12
Module 1- Introduction to Data Structures and Algorithms
4) int i,j, n = 8;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
printf("FACE Prep");
}}}
Time Complexity Calculation: In the above snippet, the first & the second for loops get
executed n times individually. So the time complexity accounts to n*n = O(n2)
5) while(low<=high)
{
mid=(low+high)/2;
if(n<arr[mid])
high=mid-1;
elseif(n>arr[mid])
low=mid+1;
elsebreak;
}
Time Complexity Calculation: This is the algorithm of binary search. It breaks the given set
of elements into two halves and then searches for a particular element. Further, it keeps dividing
these two halves into further halves until each individual element is a set. All such algorithms
which work on the principle of recursive division of elements into halves will have a
O(Log n) complexity
13
Module 1- Introduction to Data Structures and Algorithms
Asymptotic Notations:
Theta notation
14
Module 1- Introduction to Data Structures and Algorithms
Big-O notation represents the upper bound of the running time of an algorithm. Therefore, it
gives the worst-case complexity of an algorithm.
.It is the most widely used notation for Asymptotic analysis.
.It specifies the upper bound of a function.
.The maximum time required by an algorithm or the worst-case time complexity.
.It returns the highest possible output value(big-O) for a given input.
.Big-O(Worst Case) It is defined as the condition that allows an algorithm to complete
statement execution in the longest amount of time possible.
If f(n) describes the running time of an algorithm, f(n) is O(g(n)) if there exist a positive
constant C and n0 such that, 0 ≤ f(n) ≤ cg(n) for all n ≥ n0
Omega notation represents the lower bound of the running time of an algorithm. Thus, it
provides the best case complexity of an algorithm.
The execution time serves as a lower bound on the algorithm's time complexity.
Let g and f be the function from the set of natural numbers to itself. The function f is said to be
Ω(g), if there is a constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0
15
Module 1- Introduction to Data Structures and Algorithms
Arrays
Arrays is a linear data structure where all elements are arranged sequentially. It is a collection
of elements of same data type stored at contiguous memory locations. An array in C is a
fixed-size collection of similar data items stored in contiguous memory locations. It can be
used to store the collection of primitive data types such as int, char, float, etc., as well as derived
and user-defined data types such as pointers, structures, etc.
1. Array Declaration
Array declaration is the process of specifying the type, name, and size of the array. In C, we
have to declare the array like any other variable before using it.
data_type array_name[size];
16
Module 1- Introduction to Data Structures and Algorithms
The above statements create an array with the name array_name, and it can store a specified
number of elements of the same data type.
Example:
// Creates array arr to store 5 integer values.
int arr[5];
When we declare an array in C, the compiler allocates the memory block of the specified size
to the array name.
2. Array Initialization
Initialization in C is the process to assign some initial value to the variable. When the array is
declared or allocated memory, the elements of the array contain some garbage value. So, we
need to initialize the array to some meaningful values.
The above statement creates an array arr and assigns the values {2, 4, 8, 12, 16} at the time of
declaration.
Array in C provides random access to its elements, which means that we can access any element
of the array by providing the position of the element, called the index.
Syntax:
The index values start from 0 and goes up to array_size-1. We pass the index inside square
brackets [] with the name of the array.
array_name [index];
#include <stdio.h>
int main() {
int arr[100], size, i, pos, value;
// Initial input
printf("Enter number of elements: ");
scanf("%d", &size);
17
Module 1- Introduction to Data Structures and Algorithms
// Traversal
printf("\nArray elements are:\n");
for(i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
// Insertion
printf("\n\nEnter position to insert (0 to %d): ", size);
scanf("%d", &pos);
printf("Enter value to insert: ");
scanf("%d", &value);
// Deletion
printf("\n\nEnter position to delete (0 to %d): ", size - 1);
scanf("%d", &pos);
return 0;
}
Multi-dimensional array in C
A multi-dimensional array in C can be defined as an array that has more than one dimension.
Having more than one dimension means that it can grow in multiple directions. Some popular
multidimensional arrays include 2D arrays which grows in two dimensions, and 3D arrays
which grows in three dimensions.
18
Module 1- Introduction to Data Structures and Algorithms
Syntax
type arrName[size1][size2]....[sizeN];
Examples:
// Two-dimensional array
int two_d[10][20];
// Three-dimensional array:
int three_d[10][20][30];
Types of Multidimensional Arrays
In C, there can be many types of arrays depending on their dimensions but two of them are
most commonly used:
1. 2D Array - Two Dimensional
2. 3D Array - Three Dimensional
2D Arrays in C
A two-dimensional array or 2D array is the simplest form of the multidimensional array.
We can visualize a two-dimensional array as one-dimensional arrays stacked vertically
forming a table with 'm' rows and 'n' columns. In C, arrays are 0-indexed, so the row number
ranges from 0 to (m-1) and the column number ranges from 0 to (n-1).
Declaration of 2D Array
type arr_name[m][n];
19
Module 1- Introduction to Data Structures and Algorithms
For example, we can declare a two-dimensional integer array with name 'arr' with 10 rows
and 20 columns as:
int arr[10][20];
Initialization of 2D Arrays
We can initialize a 2D array by using a list of values enclosed inside '{ }' and separated by a
comma as shown in the example below:
#include <stdio.h>
int main() {
arr[0][0]: 0 arr[0][1]: 1
20
Module 1- Introduction to Data Structures and Algorithms
arr[1][0]: 2 arr[1][1]: 3
arr[2][0]: 4 arr[2][1]: 5
3D Array in C
Declaration of 3D Array in C
We can declare a 3D array with x 2D arrays each having m rows and n columns using the
syntax shown below:
type arr_name[x][m][n];
For example, we can declare 3d array, which is made by 2-2D array and each 2D array have 2
rows and 2 columns:
int arr[2][2][2];
Initialization of 3D Array in C
Initialization in a 3D array is the same as that of 2D arrays. The difference is as the number
of dimensions increases so the number of nested braces will also increase.
or
int arr[2][3][2] = { { { 1, 1 }, { 2, 3 }, { 4, 5 } },
{ { 6, 7 }, { 8, 9 }, { 10, 11 } } };
#include <stdio.h>
int main() {
21
Module 1- Introduction to Data Structures and Algorithms
• Storing and accessing data: Arrays store elements in a specific order and allow
constant-time O(1) access to any element.
• Searching: If data in array is sorted, we can search an item in O(log n) time. We can
also find floor(), ceiling(), kth smallest, kth largest, etc efficiently.
• Matrices: Two-dimensional arrays are used for matrices in computations like graph
algorithms and image processing.
• Implementing other data structures: Arrays are used as the underlying data structure
for implementing stacks and queues.
• Dynamic programming: Dynamic programming algorithms often use arrays to store
intermediate results of subproblems in order to solve a larger problem.
• Data Buffers: Arrays serve as data buffers and queues, temporarily storing incoming
data like network packets, file streams, and database results before processing.
Linked List
• A linked list is a linear data structure where each element (called a node) is
connected to the next one using pointers. Unlike array, elements of linked list are
stored in random memory locations.
• In this article, we will learn about the linked list, its types, representation of the
linked list in C, and discuss what link list offers as compared to the similar data
structures.
A linked list is a sequence of nodes in which each node is made up of two parts:
22
Module 1- Introduction to Data Structures and Algorithms
• Pointer: A reference to the next node in the sequence. (There can be multiple pointers
for different kind of linked list.)
A linked list is represented as a pointer to the first node where each node contains:
23
Module 1- Introduction to Data Structures and Algorithms
A doubly linked list is a bit more complex than singly linked list. In it, each node contains
three parts: the data, a pointer to the next node, and one extra pointer which points to the
previous node. This allows for traversal in both directions, making it more versatile than a
singly linked list.
A doubly linked list is represented as a pointer to the first node (head), where each node
contains:
24
Module 1- Introduction to Data Structures and Algorithms
A circular linked list is represented as a pointer to the first node, where each node contains:
• Next: A pointer that links to the next node, with the last node pointing back to the first
node.
25