Module 2 UQ
Module 2 UQ
Module 2 UQ
1. Before pushing the element to the stack, we check if the stack is full .
2. If the stack is full (top == capacity-1) , then Stack Overflows and we cannot insert the
element to the stack.
3. Otherwise, we increment the value of top by 1 (top = top + 1) and the new value is
inserted at top position .
4. The elements can be pushed into the stack till we reach the capacity of the stack.
1. Before popping the element from the stack, we check if the stack is empty .
2. If the stack is empty (top == -1), then Stack Underflows and we cannot remove
any element from the stack.
3. Otherwise, we store the value at top, decrement the value of top by 1 (top = top
– 1) and return the stored top value.
If rear != max - 1, then rear will be incremented to mod(maxsize) and the new
value will be inserted at the rear end of the queue.
If front != 0 and rear = max - 1, it means that queue is not full, then set the value
of rear to 0 and insert the new element there.
There are two cases in which the element cannot be inserted:
When front ==0 && rear = max-1, which means that front is at the first position
of the Queue and rear is at the last position of the Queue.
front== rear + 1;
Step 4: EXIT
5. Write an algorithm to search an element using binary search. Discuss its
time complexity.
Step 1 - Read the search element from the user.
Step 2 - Find the middle element in the sorted list.
Step 3 - Compare the search element with the middle element in the sorted list.
Step 4 - If both are matched, then display "Given element is found!!!" and
terminate the function.
Step 5 - If both are not matched, then check whether the search element is
smaller or larger than the middle element.
Step 6 - If the search element is smaller than middle element, repeat steps 2, 3,
4 and 5 for the left sublist of the middle element.
Step 7 - If the search element is larger than middle element, repeat steps 2, 3, 4
and 5 for the right sublist of the middle element.
Step 8 - Repeat the same process until we find the search element in the list or
until sublist contains only one element.
Step 9 - If that element also doesn't match with the search element, then display
"Element is not found in the list!!!" and terminate the function.
Time Complexity of Binary Search Algorithm
The binary search algorithm works in logarithmic time complexity with constant space. Check
the binary search algorithm’s best, average and worst time complexity below.
The best-case complexity happens when our target element is found in the first
iteration, i,e. It is the first middle element. The best time complexity of the binary
search algorithm is O(1).
The average time complexity of the binary search algorithm is O(logn).
The worst time complexity occurs when our target element is the last. We keep
dividing until the last element is left, our target element. The worst time complexity of
the binary search algorithm is O(logn).
In 2D array representation of sparse matrix, there are three fields used that are named as -
o Row - It is the index of a row where a non-zero element is located in the matrix.
o Column - It is the index of the column where a non-zero element is located in the matrix.
o Value - It is the value of the non-zero element that is located at the index (row, column).
We can observe a 10x10 sparse matrix containing 12 non-zero elements. The above matrix
occupies 10x10 = 100 memory space. Therefore total number of nonzero elements=100-12=88
memory space.
In tuple form
number of rows=Nonzero element+1=12+1=13
number of columns =3
therefore space occupied in tuple=13x3=39 memory space
𝟏𝟎 𝟓 𝟒 𝟖 𝟐𝟓
[ 𝟕 𝟔 𝟒 𝟐 𝟎 ]
𝟕 𝟓 𝟐 𝟎 𝟎
10. Convert the expression A+B*C-D/E*H to postfix form. Show each step in
the conversion including the stack contents.
11. What is DEQUEUE
The dequeue() is a data manipulation operation that is used to remove elements from the
queue.
14. Write the algorithms for linear and binary search and compare their
performances.
Linear search Algorithm
int linear_search(int array[], int n, int x)
{
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
a) Fixed size
The size of a circular queue is determined when it's initialized and remains
fixed.
b) Circular nature
When the end of the array is reached, the queue wraps around to the beginning,
making use of all available space.
c) Efficient space utilization
Circular queues reuse vacated spaces to avoid wasting memory.
d) Two pointers
Circular queues use two pointers, front and rear, to keep track of the positions
for dequeuing and enqueuing operations.
e) First In First Out (FIFO) principle
Circular queues follow the FIFO principle, where items are added to the rear
and deleted from the front.
f) Circular link
The last node of a circular queue is connected to the first node by forming a
circular link, which is why it's also called a Ring Buffer.
g) Insertion and deletion
In a circular queue, insertion takes place at the rear pointer and deletion takes
place at the front pointer.
Condition for checking Circular Queue is full
if (rear + 1) % size == front
Condition for checking Circular Queue is empty
if ((rear ==-1)&&(front==-1))
18. Write an algorithm for deleting a node from a specified position in a
circular queue.
1. First, find the length of the list. That is, the number of nodes in the list.
2. Take two pointers previous and current to traverse the list. Such that
previous is one position behind the current node.
3. Take a variable count initialized to 0 to keep track of the number of nodes
traversed.
4. Traverse the list until the given index is reached.
5. Once the given index is reached, do previous->next = current->next.
19. Write a program of binary search which tells how many comparisons it did
to search an element given as user input.
Binary search Algorithm
int binarySearch(int array[], int x, int low, int high)
{
// Repeat until the pointers low and high meet each other
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
To determine how many comparisons a binary search performs to find an element, we can
count the steps taken during the search process. Binary search works by dividing the search
range in half repeatedly, and in each step, it compares the target element with the middle
element of the current range.
In input restricted queue, insertion operation can be performed at only one end, while deletion
can be performed from both ends.
Output restricted Queue
In output restricted queue, deletion operation can be performed at only one end, while insertion
can be performed from both ends.
o Insertion at front
o Insertion at rear
o Deletion at front
o Deletion at rear
Step 2. Then pick the characters from the string one by one and put them to the
stack, so that the last character of the string comes at the top of the stack.
Step 3. pop the stack and put the popped characters back in the empty string.
23. What is sparse matrix? Write an algorithm to add two sparse matrices.
A sparse matrix is a matrix in which most of the elements are zero.
24. Discuss an algorithm to convert an infix expression to a postfix expression.
25. Write an algorithm to find the transpose of a matrix represented in tuple
form.