Data Structures-1
Data Structures-1
Syllabus
Sorting - Selection Sort, Bubble Sort, Insertion Sort, Quick Sort and
Merge Sort.
1
14-05-2025
• Data consists of raw, unprocessed facts and figures collected through • A data structure is basically a group of data elements that are put
observations, experiments, or measurements. together under one name, and which defines a particular way of
• Data that has been processed, organized, and structured to mean storing and organizing data in a computer so that it can be used
something is known as information. efficiently.
• When we add context to raw data, we transform it into information,
which makes it a lot more helpful in making decisions, understanding • Data structures are used in almost every program or software
complex situations, or building new knowledge. system.
• When selecting a data structure to solve a problem, the following steps Data : a value or set of values.
must be performed. • It specifies either the value of a variable or a constant
1. Analysis of the problem to determine the basic operations that must be • e.g., marks of students, name of an employee, address of a customer,
supported. For example, basic operation may include inserting /deleting/ value of pi, etc.).
searching a data item from the data structure.
• Elementary item: a data item that does not have subordinate data
items.
2. Quantify the resource constraints for each operation. • Group item: the one that is composed of one or more subordinate data
items.
3. Select the data structure that best meets these requirements. • For example, a student’s name may be divided into three sub-items—
first name, middle name, and last name—but his roll number would
normally be treated as a single item.
2
14-05-2025
• Sequential storage, data is stored in a contiguous block of memory locations. This is • Data is stored in nodes, where each node contains two parts:
commonly used for arrays and strings. • Data: The actual value stored in the node.
Characteristics: • Pointer/Link: A reference to the next node in the sequence (or, in the case of
• Contiguous Memory Allocation: All elements are stored one after the other in memory. doubly-linked lists, references to both the next and the previous nodes).
Characteristics:
• Indexing: Direct access using indices, making it efficient for random access.
• Dynamic Memory Allocation: Nodes are created at runtime, allowing the structure
• Fixed Size: The size of the structure is typically fixed at the time of declaration. to grow or shrink as needed.
• Efficient Traversal: Traversal is straightforward due to contiguous allocation. • Non-contiguous Memory Allocation: Nodes may be scattered across different
• Insertion/Deletion Overhead: Inserting or deleting elements may require shifting other memory locations, but they are connected via pointers.
elements, leading to a time complexity of 𝑂(𝑛) in the worst case. • Efficient Insertions/Deletions: Adding or removing nodes can be done efficiently
(𝑂(1)) if the pointer to the position is known.
3
14-05-2025
4
14-05-2025
Arrays
• In C, the array index starts from zero. This means that the array marks
will contain 10 elements in all.
• An array is a collection of similar data elements. These data elements
have the same data type.
• The elements of the array are stored in consecutive memory locations
and are referenced by an index (also known as the subscript).
• In C, arrays are declared using the following syntax:
type name[size];
• For example, int marks[10];
Linked Lists
Limitations of arrays:
• Arrays are of fixed size.
• A linked list is a very flexible, dynamic data structure in which
• Data elements are stored in contiguous memory locations which elements (called nodes) form a sequential list.
may not be always available.
• In a linked list, each node is allocated space as it is added to the list.
• Insertion and deletion of elements can be problematic because of
shifting of elements from their positions. • Every node in the list points to the next node in the list.
• Therefore, in a linked list, every node contains the following two
types of data:
• The value of the node or any other data that corresponds to that node
• A pointer or link to the next node in the list
5
14-05-2025
Stack
The last node in the list contains a NULL pointer to indicate that it
is the end or tail of the list.
• A stack is a linear data structure in which insertion and deletion of
elements are done at only one end, which is known as the top of
the stack.
• Stack is called a last-in, first-out (LIFO) structure.
• In the computer’s memory, stacks can be implemented using
arrays or linked lists.
• Advantage: Easier to insert or delete data elements
• Disadvantage: Slow search operation and requires more memory
space.
6
14-05-2025
• Every queue has front and rear variables that point to the position
from where deletions and insertions can be done, respectively. Trees
• A binary tree consists of a root node and left and • A graph is a non-linear data structure which is a collection of vertices
right sub-trees, where both sub-trees are also (also called nodes) and edges that connect these vertices.
binary trees.
• A graph is often viewed as a generalization of the tree structure, where
• Each node contains a data element, a left pointer instead of a purely parent-to-child relationship between tree nodes, any
which points to the left sub-tree, and a right
pointer which points to the right sub-tree. kind of complex relationships between the nodes can exist.
• The root element is the topmost node which is • A node in the graph may represent a city and the edges connecting the
pointed by a ‘root’ pointer. If root = NULL then the nodes can represent roads.
tree is empty. • A graph can also be used to represent a computer network where the
• Advantage: Provides quick search, insert, and nodes are workstations and the edges are the network connections.
delete operations
• Disadvantage: Complicated deletion algorithm
7
14-05-2025
• Unlike trees, graphs do not have any root node. Rather, every node in the Operations on Data Structures
graph can be connected with every another node in the graph.
• When two nodes are connected via an edge, the two nodes are known as
neighbours.
• Advantage: Best models real-world situations
• Traversing
• Disadvantage: Some algorithms are slow and very complex • Searching
• Inserting
• Deleting
• Sorting
• Merging
8
14-05-2025
Sequence Decision
• By sequence, we mean that each step of an algorithm is executed • Decision statements are used when the execution of a process depends
on the outcome of some condition.
in a specified order.
• Syntax: IF condition Then process
• A condition in this context is any statement that may evaluate to either
a true value or a false value.
• example, if x = y, then print EQUAL.
• A decision statement can also be stated in the following manner:
IF condition
Then process1
ELSE
process2
• This form is popularly known as the IF–ELSE construct
9
14-05-2025
Repetition
Tasks:
SEARCHING
• Write an algorithm to print all multiples of 5 between 1 to 500.
10
14-05-2025
• There are two popular methods for searching the array elements: • Also called as sequential search.
linear search and binary search. • It works by comparing the value to be searched with every element of
• The algorithm that should be used depends entirely on how the the array one by one in a sequence until a match is found.
values are organized in the array. • Mostly used to search an unordered list of elements.
• For example, if the elements of the array are arranged in ascending • For example, if an array A[] is declared and initialized as,
order, then binary search should be used, as it is more efficient for
sorted lists in terms of complexity. • int A[] = {10, 8, 2, 7, 3, 4, 9, 1, 6, 5};
• the value to be searched is VAL = 7, then searching means to find
whether the value ‘7’ is present in the array or not.
11
14-05-2025
• if VAL is not equal to A[MID], then the values of BEG, END, and MID will be
changed depending on whether VAL is smaller or greater than A[MID].
• (a) If VAL < A[MID], then VAL will be present in the left segment of the array. So,
END = MID – 1.
• (b) If VAL > A[MID], then VAL will be present in the right segment of the array. So,
BEG = MID + 1.
• Finally, if VAL is not present in the array, then eventually, END will be less than
BEG.
• When this happens, the algorithm will terminate and the search will be
unsuccessful.
12
14-05-2025
sort key
• Internal sorting data stored in the
computer’s memory sort key comparisons
records in the list will be moved
• External sorting data stored in files
6. Stability equivalent
elements or records retain their relative positions even after sorting is
done
1
14-05-2025
30, 29, 52, 63, 27, 19, 54, 87 29, 30, 52, 27, 19, 54, 63, 87
2
14-05-2025
27, 29, 19, 30, 52, 54, 63, 87 19, 27, 29, 30, 52, 54, 63, 87
3
14-05-2025
comparisons
f(n) = (n – 1) + (n – 2) + (n – 3) + ..... + 3 + 2 + 1
( )( ) ( )
f(n) = = //sum of natural numbers
f(n) = − = O(𝑛 )
O(𝑛 ).
𝑛 n
is the total number of elements
4
14-05-2025
5. During each iteration of the algorithm, the first element in the unsorted set
is picked up and inserted into the correct position in the sorted set.
Unsorted
5
14-05-2025
best case
O(n)
worst case
O(𝑛 )
(K–1)/2
O(𝑛 ).
6
14-05-2025
Pass 2
ARR[POS] ARR[1]
Pass N–1
ARR[POS] ARR[N–2]
7
14-05-2025
developed by C. A. R. pivot
Hoare.
partition exchange sort all elements that are
divide-and-conquer less than the pivot appear before all elements greater than the
pivot element come after it
O(n log n)
O(𝑛 ). pivot is placed in its final position
partition operation
Step 1 scan
loc = 0, left = 0, and right = n–1 from left to right
8
14-05-2025
Best-case analysis:
Worst Case:
sublists
returned by the partitioning routine is of size n − 1
pivot happens to be the smallest or largest log n
O(log n).
O(n)
O(n log n)
• This means that the call tree is a linear chain of n − 1 nested calls.
The ith call does O(n − i) work to do the partition, and , so in that
case quicksort takes O(n²) time.
9
14-05-2025
• Divide
• Conquer
• Combine
10
14-05-2025
O(n)
11