Dsa (SRG)
Dsa (SRG)
PUSH Algorithm:
1. Create a new node with the given value.
2. Set the new node's next pointer to point to the top of the stack.
3. Update the top pointer to the new node.
POP Algorithm:
1. If top is NULL, stack is empty (Underflow condition).
2. Store the value of the current top node.
3. Update the top pointer to point to the next node.
4. Delete the current top node.
_______________________________________________________________________
4.Why is Stack called LIFO?
Stack is called LIFO (Last In First Out) because the last element added to the stack is
the first one to be removed, like stacking and removing plates.
5.Convert infix to postfix:
Infix Expression: A + (B * C-(D/E%* F) * G)*H
Postfix Expression: ABC*DEF*/G*-H*+
________________________________________________________________________
6.Algorithm to convert infix to postfix:
output.
Push the current operator to the stack.
If it’s (, push it to the stack.
If it’s ), pop operators to the output until ( is found; discard (.
4. Pop all remaining operators in the stack to the output.
________________________________________________________________________
7.Bubble Sort Algorithm:
Bubble_Sort(arr):
fori = 0 to n-1:
for j = 0 to n-i-2:
if arr[j] > arr[j+1]:
Swap(arr[j], arr[j+1])
Insertion_Sort(arr):
fori = 1ton-1:
key = arr[i]
j=i-1
arr[j+1] = key
1. Start from the head node and traverse to find the node at the specified position.
2. If the node to delete is the head:
Update the last node's next pointer to point to head.next.
3. Otherwise:
Traverse to the previous node of the target node.
Update its next pointer to skip the target node.
4. Free the memory of the target node.
________________________________________________________________________
16. Searching and Quick Sort
(a) Why do we need searching algorithms?
Searching algorithms help locate specific data within large datasets quickly and
efficiently. They are crucial in:
def quick_sort(arr):
if len(arr) <= 1:
return arr # Base case: already sorted
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
(c) Finding the First Matching Node in a Circular Header Linked List
Algorithm:
1. Initialize a pointer at the header node.
2. Traverse the list while comparing each node's value with ITEM.
3. If found, return the position/index.
4. If the traversal reaches the header again, the item is not in the list.
c) Radix Sort
A non-comparative sorting algorithm that processes each digit of the numbers
sequentially, starting from the least
significant digit to the most significant digit, using a stable sort like counting sort as a
subroutine.
d) Hash Function
A function that converts input data into a fixed-size numerical value (hash code),
typically used in hash tables for fast data
retrieval.
e) Linear Search
A search algorithm that traverses each element in a list sequentially until the desired
element is found or the list ends.
Open Addressing: Probes for the next available slot using methods like linear
probing, quadratic probing, or double
hashing.
h) Binary Search
A search algorithm that repeatedly divides a sorted array into halves to locate a target
element, reducing the search range
by half each time.
i) Merge Sort
A divide-and-conquer sorting algorithm that divides the array into smaller subarrays,
sorts them recursively, and merges
them back together.
j) Sparse Matrix
A matrix with a majority of its elements as zero, typically represented using efficient
data structures like a coordinate list
or compressed sparse row (CSR).