DSA unit 1
DSA unit 1
refers to a single set of item values. The data items are then classified into sub-items,
which is the group of items that are not known as the simple primary form of the item.
Let us consider an example where an employee name can be broken down into three
sub-items: First, Middle, and Last. However, an ID assigned to an employee will generally
be considered a single item.
In the example mentioned above, the items such as ID, Age, Gender, First, Middle, Last,
Street, Locality, etc., are elementary data items. In contrast, the Name and the Address are
group data items.
Some examples of Data Structures are Arrays, Linked Lists, Stack, Queue, Trees, etc. Data
Structures are widely used in almost every aspect of Computer Science, i.e., Compiler
Design, Operating Systems, Graphics, Artificial Intelligence, and many more.
Data Structures are the main part of many Computer Science Algorithms as they allow the
programmers to manage the data in an effective way. It plays a crucial role in improving
the performance of a program or software, as the main objective of the software is to
store and retrieve the user's data as fast as possible.
Understanding the Need for Data Structures
As applications are becoming more complex and the amount of data is increasing every
day, which may lead to problems with data searching, processing speed, multiple
requests handling, and many more. Data Structures support different methods to organize,
manage, and store data efficiently. With the help of Data Structures, we can easily
traverse the data items. Data Structures provide Efficiency, Reusability, and Abstraction.
Based on memory allocation, the Linear Data Structures are further classified into two
types:
1. Static Data Structures: The data structures having a fixed size are known as Static
Data Structures. The memory for these data structures is allocated at the compiler
time, and their size cannot be changed by the user after being compiled; however,
the data stored in them can be altered.
The Array is the best example of the Static Data Structure as they have a fixed size,
and its data can be modified later.
2. Dynamic Data Structures: The data structures having a dynamic size are known as
Dynamic Data Structures. The memory of these data structures is allocated at the
run time, and their size varies during the run time of the code. Moreover, the user
can change the size as well as the data elements stored in these data structures at
the run time of the code.
Linked Lists, Stacks, and Queues are common examples of dynamic data structures
An Array is a data structure used to collect multiple data elements of the same data type
into one variable. Instead of storing multiple values of the same data types in separate
variable names, we could store all of them together into one variable. This statement
doesn't imply that we will have to unite all the values of the same data type in any
program into one array of that data type. But there will often be times when some specific
variables of the same data types are all related to one another in a way appropriate for an
array.
An Array is a list of elements where each element has a unique place in the list. The data
elements of the array share the same variable name; however, each carries a different
index number called a subscript. We can access any data element from the list with the
help of its location in the list. Thus, the key feature of the arrays to understand is that the
data is stored in contiguous memory locations, making it possible for the users to traverse
through the data elements of the array using their respective indexes.
Figure 3. An Array
1. One-Dimensional Array: An Array with only one row of data elements is known as
a One-Dimensional Array. It is stored in ascending storage location.
2. Two-Dimensional Array: An Array consisting of multiple rows and columns of data
elements is called a Two-Dimensional Array. It is also known as a Matrix.
3. Multidimensional Array: We can define Multidimensional Array as an Array of
Arrays. Multidimensional Arrays are not bounded to two indices or two dimensions
as they can include as many indices are per the need.
2. Linked Lists
A Linked List is another example of a linear data structure used to store a collection of data
elements dynamically. Data elements in this data structure are represented by the Nodes
connected using links or pointers. Each node contains two fields, the information field
consists of the actual data, and the pointer field consists of the address of the subsequent
nodes in the list. The pointer of the last node of the linked list consists of a null pointer, as it
points to nothing. Unlike the Arrays, the user can dynamically adjust the size of a Linked
List as per the requirements.
Figure 4 A Linked Lists
● Singly Linked List: A Singly Linked List is the most common type of Linked List. Each
node has data and a pointer field containing an address to the next node.
● Doubly Linked List: A Doubly Linked List consists of an information field and two
pointer fields. The information field contains the data. The first pointer field contains
an address of the previous node, whereas another pointer field contains a
reference to the next node. Thus, we can go in both directions (backward as well as
forward).
● Circular Linked List: The Circular Linked List is similar to the Singly Linked List. The
only key difference is that the last node contains the address of the first node,
forming a circular loop in the Circular Linked List.
3. Stacks
A Stack is a Linear Data Structure that follows the LIFO (Last In, First Out) principle that allows
operations like insertion and deletion from one end of the Stack, i.e., Top. Stacks can be
implemented with the help of contiguous memory, an Array, and non-contiguous memory, a
Linked List. Real-life examples of Stacks are piles of books, a deck of cards, piles of money, and
many more.
The above figure represents the real-life example of a Stack where the operations are
performed from one end only, like the insertion and removal of new books from the top of
the Stack. It implies that the insertion and deletion in the Stack can be done only from the
top of the Stack. We can access only the Stack's tops at any given time.
4. Queues
A Queue is a linear data structure similar to a Stack with some limitations on the
insertion and deletion of the elements. The insertion of an element in a Queue is
done at one end, and the removal is done at another or opposite end. Thus, we
can conclude that the Queue data structure follows FIFO (First In, First Out)
principle to manipulate the data elements. Implementation of Queues can be
done using Arrays, Linked Lists, or Stacks. Some real-life examples of Queues
are a line at the ticket counter, an escalator, a car wash, and many more.
The above image is a real-life illustration of a movie ticket counter that can help us
understand the Queue where the customer who comes first is always served first. The
customer arriving last will undoubtedly be served last. Both ends of the Queue are open
and can execute different operations. Another example is a food court line where the
customer is inserted from the rear end while the customer is removed at the front end
after providing the service they asked for.
● Enqueue: The insertion or Addition of some data elements to the Queue is called
Enqueue. The element insertion is always done with the help of the rear pointer.
● Dequeue: Deleting or removing data elements from the Queue is termed Dequeue.
The deletion of the element is always done with the help of the front pointer.
2. Graphs
A Graph is another example of a Non-Linear Data Structure comprising a finite
number of nodes or vertices and the edges connecting them. The Graphs are
utilized to address problems of the real world in which it denotes the problem area
as a network such as social networks, circuit networks, and telephone networks.
For instance, the nodes or vertices of a Graph can represent a single user in a
telephone network, while the edges represent the link between them via telephone.
The Graph data structure, G is considered a mathematical structure comprised of a
set of vertices, V and a set of edges, E as shown below:
G = (V,E)
. Compile-time
a. Run-time
For example, the malloc() function is used in C Language to create data structure.
2. Selection: Selection means selecting a particular data from the available data. We can
select any particular data by specifying conditions inside the loop.
3. Update: The Update operation allows us to update or modify the data in the data structure.
We can also update any particular data by specifying some conditions inside the loop, like
the Selection operation.
4. Splitting: The Splitting operation allows us to divide data into various subparts decreasing
the overall process completion time.
In other words, we can say that abstract data types are the entities that are definitions of data and
operations but do not have implementation details. In this case, we know the data that we are
storing and the operations that can be performed on the data, but we don't know about the
implementation details. The reason for not having implementation details is that every
programming language has a different implementation strategy for example; a C data structure is
implemented using structures while a C++ data structure is implemented using objects and classes.
For example, a List is an abstract data type that is implemented using a dynamic array and
linked list. A queue is implemented using linked list-based queue, array-based queue. A
Map is implemented using Tree map, hash map, or hash table.
o 4 GB RAM
o Snapdragon 2.2ghz processor
o 5 inch LCD screen
o Dual camera
o Android 8.0
The above specifications of the Smartphone are the data, and we can also perform the
following operations on the Smartphone:
The Smartphone is an entity whose data or specifications and operations are given above.
The abstract/logical view and operations are the abstract or logical views of a
Smartphone.
class Smartphone
{
private:
int ramSize;
string processorName;
float screenSize;
int cameraCount;
string androidVersion;
public:
void call();
void text();
void photo();
void video();
}
The above code is the implementation of the specifications and operations that can be performed on
the Smartphone. The implementation view can differ because the syntax of programming languages is
different, but the abstract/logical view of the data structure would remain the same. Therefore, we can
say that the abstract/logical view is independent of the implementation view.
Note: We know the operations that can be performed on the predefined data types such as
int, float, char, etc., but we don't know the implementation details of the data types.
Therefore, we can say that the abstract data type is considered as the hidden box that hides
all the internal details of the data type.
2. Insertion:
3. Deletion:
Description: Removing an element from the data structure.
Examples: Deleting a node from a linked list, removing an element from an
array.
4. Traversal:
5. Sorting:
6. Merging:
7. Splitting:
Cost Estimation:
Time Complexity:
Examples:
Array:
Linked List:
Hash Table:
Representation of an array
As per the above illustration, there are some of the following important points -
As stated above, all the data elements of an array are stored at contiguous locations in the
main memory. The name of the array represents the base address or the address of the
first element in the main memory. Each element of the array is represented by proper
indexing.
In the above image, we have shown the memory allocation of an array arr of size 5. The array
follows a 0-based indexing approach. The base address of the array is 100 bytes. It is the
address of arr[0]. Here, the size of the data type used is 4 bytes; therefore, each element will
take 4 bytes in the memory.
We required the information given below to access any random element from the array -
● Base Address of the array.
● Size of an element in bytes.
● Type of indexing, array follows.
Here, size represents the memory taken by the primitive data types. As an instance, int takes
2 bytes, float takes 4 bytes of memory space in C programming.
Suppose an array, A[-10 ..... +2 ] having Base address (BA) = 999 and size of an element = 2
bytes, find the location of A[-1].
= 999 + 18
= 1017
Basic operations
Search - It is used to search an element using the given index or by the value.
Traversal operation
This operation is performed to traverse through the array elements. It prints all array
elements one after another. We can understand it with the below program -
#include <stdio.h>
void main() {
int Arr[5] = {18, 30, 15, 70, 12};
int i;
printf("Elements of the array are:\n");
for(i = 0; i<5; i++) {
printf("Arr[%d] = %d, ", i, Arr[i]);
}
}
Insertion operation
This operation is performed to insert one or more elements into the array. As per the
requirements, an element can be added at the beginning, end, or at any index of the array.
Now, let's see the implementation of inserting an element into the array.
#include <stdio.h>
int main()
{
int arr[20] = { 18, 30, 15, 70, 12 };
int i, x, pos, n = 5;
printf("Array elements before insertion\n");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
x = 50; // element to be inserted
pos = 4;
n++;
Deletion operation
As the name implies, this operation removes an element from the array and then
reorganizes all of the array elements.
#include <stdio.h>
void main() {
int arr[] = {18, 30, 15, 70, 12};
int k = 30, n = 5;
int i, j;
j = k;
while( j < n) {
arr[j-1] = arr[j];
j = j + 1;
}
n = n -1;
Search operation
This operation is performed to search an element in the array based on the value or index.
#include <stdio.h>
void main() {
int arr[5] = {18, 30, 15, 70, 12};
int item = 70, i, j=0 ;
j = j + 1;
}
Update operation
This operation is performed to update an existing array element located at the given index.
#include <stdio.h>
void main() {
int arr[5] = {18, 30, 15, 70, 12};
int item = 50, i, pos = 3;
arr[pos-1] = item;
printf("\nArray elements after updation :\n");
Time Complexity
Space Complexity
Advantages of Array
○ Array provides the single name for the group of variables of the same type.
Therefore, it is easy to remember the name of all the elements of an array.
○ Traversing an array is a very simple process; we just need to increment the base
address of the array in order to visit each element one by one.
○ Any element in the array can be directly accessed by using the index.
Disadvantages of Array
○ Array is homogenous. It means that the elements with similar data type can be
stored in it.
○ In array, there is static memory allocation that is size of an array cannot be
altered.
○ There will be wastage of memory if we store less number of elements than the
declared size.