DSA Secion A
DSA Secion A
Algorithms
DSA Team
Jan-June 2025
Data Structures
•A data structure is a storage that is used to store Another Example:
and organize data. If we want to store data about people we are related to, we
use a family tree as the data structure. We choose a family
•It is a way of arranging data on a computer so that tree as the data structure because we have information
it can be accessed and updated efficiently. about people we are related to and how they are related,
and we want an overview so that we can easily find a
•Depending on your requirement and project, it is specific family member, several generations back.
important to choose the right data structure for
your project.
Problem: A problem can be defined as a real-world problem It should produce at least one output.
or real-world instance problem for which you need to
develop a program or set of instructions. An algorithm is a It should take zero or more input.
set of instructions.
It should be deterministic means giving the
Algorithm: An algorithm is defined as a step-by-step
process that will be designed for a problem. same output for the same input case.
Input: After designing an algorithm, the algorithm is given Every step in the algorithm must be effective
the necessary and desired inputs.
i.e. every step should do some work.
Processing Unit: The input will be passed to the processing
unit, producing the desired output.
Output: The outcome or result of the program is referred to
as the output.
How to Develop an Algorithm
1. Understand the problem.
3. Identify inputs required by the problem and choose the associated data structure.
4. Design a logic that will produce the desired output from the given inputs.
6. Repeat steps 1 to 5 till the algorithm produces the desired results for all types of input and
rules.
Stepwise Refinement: Top-Down
Break a complex problem into smaller problems
Write a stub for each smaller problem
Fill in algorithm for each sub-step.
Possibly using stepwise refinement for the sub-step
Advantages
Bugs are easier to find
adding less code in each refinement
Functions might be reusable
Algorithm Analysis
• The analysis of algorithms is the process of evaluating the performance and
efficiency of an algorithm.
• It involves determining the computational resources (like time and space) that
an algorithm requires for execution.
• This evaluation helps to compare algorithms and choose the most appropriate
one for a specific problem.
RULE #1: Simple program statements are assumed to take a constant amount
of time which is O(1)
RULE #2: Differences in execution time of simple statements is ignored.
RULE #3: In conditional statements, the worst case is always used.
RULE #4:The running time of a sequence of steps has the order of the
running time of the largest (the sum rule).
RULE #5:If two processes are constructed such that second process is repeated a
number of times for each n in the first process, then O is equal to the
product of the orders of magnitude for both products (the product rule)
RULE #1: RULE #2:
Address of A[I][J] = B + W * ((I – LR) * N + (J – LC)) Address of A[I][J] = B + W * ((J – LC) * M + (I – LR))
End of step 2
5.Exit
Insertion in Array
•
1. [Initialize Counter] Set J=N.
2. Repeat steps 3 and 4 while J>=K.
3. Set LA[J+1] = LA[J].
4. [Decrease Counter] Set J=j-1.
5. [Inserting ITEM] Set LA[K]=ITEM.
6. [Reset Number of Elements]. Set
N=N+1.
7. Exit
Deletion from Array
Searching in an Array
• Searching is the fundamental process of locating a
specific element or item within a collection of data.
• The primary objective of searching is to determine
whether the desired element exists within the data, and if
so, to identify its precise location or retrieve it.
• Linear Search
• Binary Search
Linear Search
Arr[5] = {1,2,3,4,5}
In-place Sorting
Sorting algorithms may require some extra space for
comparison and temporary storage of few data
elements.
These algorithms do not require any extra space and
sorting is said to happen in-place, or for example,
within the array itself.
Example: Bubble Sort, Insertion Sort, Selection Sort
Out-place Sorting
In some sorting algorithms, the program requires
space which is more than or equal to the elements
being sorted. Sorting which uses equal or more space
is called Out-place sorting.
Example: Merge-sort, Radix Sort, Counting Sort,
Bucket Sort.
Stable Sorting Internal Sorting
• When two same data appear in •When all data is placed in the main
the same order in sorted data without memory or internal memory then sorting is called internal
changing their position is called stable sort. sorting. Here the problem cannot take input beyond its size.
• Example: merge sort, insertion sort, Example: heap sort, bubble sort, selection sort, quick sort,
bubble sort. shell sort, insertion sort.
Example: Merge sort, Tag sort, Polyphase sort, Four tape sort,
External radix sort, Internal merge sort, etc.
Insertion Sorting
Selection Sort
• Selection Sort is a comparison-
based sorting algorithm. It sorts an
array by repeatedly selecting
the smallest (or largest) element
from the unsorted portion and
swapping it with the first unsorted
element. This process continues until
the entire array is sorted.
A:
3 5 15 28 30 6 10 14
L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6
i=0 j=0
Merge-Sort: Merge Example
A:
3
1 5 15 28 30 6 10 14
k=0
L: R:
3
2 15
3 28
7 30
8 6
1 10
4 14
5 22
6
i=0 j=0
Merge-Sort: Merge Example
A:
1 25 15 28 30 6 10 14
k=1
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i=0 j=1
Merge-Sort: Merge Example
A:
1 2 3 28 30
15 6 10 14
k=2
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=1 j=1
Merge-Sort: Merge Example
A:
1 2 3 4 6 10 14
k=3
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=1
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 10 14
k=4
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=2
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 10 14
k=5
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=3
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 7 14
k=6
L: R:
2 3 7 8 6
1 10
4 14
5 22
6
i=2 j=4
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 7 8
14
k=7
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i=3 j=4
Merge-Sort: Merge Example
A:
1 2 3 4 5 6 7 8
k=8
L: R:
3
2 5
3 15
7 28
8 6
1 10
4 14
5 22
6
i=4 j=4
Algorithm:- Merge ( A [n], low, mid, high )
{
//A is an array of n elements, low is lower bound of
an array and high is upper bound.
1. i = low
2. j = mid + 1
3. k = low
4. Do
4.1 If( A [i] <= A [j])
4.1.1 B [k] = A [i]
4.1.2 i = i + 1
4.1.3 k = k +1
4.2 else
4.2.1 B [k] = A [j]
4.2.2 j = j +1
4.2.3 k = k +1
while (i <= mid && j <= high);
6. While ( j < = high)
6.1 B [k] = A [j]
6.2 j = j + 1
6.3 k = k +1
}
Implementing Merge Sort Algorithm
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
The first step to sort data by using merge sort is to split the list
into two parts.
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
The first step to sort data by using merge sort is to split the list
into two parts.
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
The list has odd number of elements, therefore, the left sublist is
longer than the right sublist by one entry.
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 2 3 3 4 4 5 5 6 6
arr 53 10
10 3030 7676 3 3 5757 24 24
Implementing Merge Sort Algorithm (Contd.)
0 11 22 3 3 4 45 56 6
arr 53 10
1010 3030 30
7676 76 3 357 24
57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 3 4 5 6
arr 53 10 30 76 3 57 24
Implementing Merge Sort Algorithm (Contd.)
0 11 2 2 3 3 4 5
4 65 6
arr 10
53 53 10 30 30 76 76 3 357 57
24
57 24
Implementing Merge Sort Algorithm (Contd.)
0 1 2 2 3 3 4 4 55 6 6
arr 10 30
53 5330 7676 3 3 2457 57 57
24
Implementing Merge Sort Algorithm (Contd.)
00 11 22 3 44 55 6 6
arr 10
3 30
10 53
24 76
30 533 5724 76 57
Implementing Merge Sort Algorithm (Contd.)
0 1 2 3 4 5 6
arr 3 10 24 30 53 57 76
Write an algorithm to implement Merge sort:
Merge_sort ( A[n], low, high )
{
// A is an array of n elements,low is lower bound of
an array and high is upper bound.
1. if (low = = high)
1.1 Return Recursive Call
2. else
2.1 mid = (low + high)/2
2.2 Merge_sort ( A[], low , mid )
2.3 Merge_sort (A[], mid+1 , high )
2.4 Merge ( A[], low, mid, high )
Analysis of Merge Sort
• Divide the List into two sub-list
• Sort each sub-list
• Merge the two sub-list
log n levels
n/4 n/4 n/4 n/4 4 × cn/4 = cn
n/2 × 2c = cn
2 2 2
Total: cn log n