DSU Super 25 by Rajan SirUpdated V2V
DSU Super 25 by Rajan SirUpdated V2V
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
3. Describe working of linear search with an example.
Ans.
In linear search, search element is compared with each element from the list in a sequence.
Comparison starts with first element from the list and continues till number is found or comparison
reaches to the last element of the list.
As each element is checked with search element, the process of searching requires more time. Time
complexity of linear search is O (n) where n indicates number of elements in list.
Linear search on sorted array:-On sorted array search takes place till element is found or comparison
reaches to an element greater than search element.
Example:- Using array representation Input list 10, 20, 30, 40, 50 and Search element 30, Index =0
Iteration 1
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
4. Find the position of element 29 using binary search method in an array ‘A’ given below. Show each step.
A={11,5,21,3,29,17,2,43}
Ans.
An array which is given A[]= {11,5,21,3,29,17,2,43} is not in sorted manner, first we need to sort them in
order; So an array will be A[ ]={2,3,5,11,17,21,29,43} and the value to be searched is VAL = 29. The binary
search algorithm will proceed in the following manner.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
So, Element 29 is found at 6th location in given array A[]={2,3,5,11,17,21,29,43}.
Though the insertion and deletion in a deque can be performed on both ends, it does not follow the FIFO
rule. The representation of a deque is given as follows -
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Types of deque
There are two types of deque -
In input restricted queue, insertion operation can be performed at only one end, while deletion can be
performed from both ends.
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
We can also perform peek operations in the deque along with the operations listed above. Through peek
operation, we can get the deque's front and rear elements of the deque. So, in addition to the above
operations, following operations are also supported in deque -
1. Bubble Sort
Working:
• Bubble sort compares adjacent elements in an array and swaps them if they are in the wrong order.
• This process is repeated for each element, and after each complete pass, the largest unsorted
element "bubbles" up to its correct position.
• The algorithm continues until no more swaps are needed, indicating that the array is sorted.
Steps:
1. Compare the first two elements. If the first is greater than the second, swap them.
2. Move to the next pair and repeat until you reach the end of the array.
3. After each full pass through the array, the largest element gets placed in its correct position.
4. Repeat the process for the remaining unsorted part of the array until the entire array is sorted.
1. First pass:
o Compare 5 and 3 → Swap them → [3, 5, 8, 4, 2]
o Compare 5 and 8 → No swap → [3, 5, 8, 4, 2]
o Compare 8 and 4 → Swap them → [3, 5, 4, 8, 2]
o Compare 8 and 2 → Swap them → [3, 5, 4, 2, 8]
o After the first pass, 8 is in its correct position.
2. Second pass:
o Compare 3 and 5 → No swap → [3, 5, 4, 2, 8]
o Compare 5 and 4 → Swap them → [3, 4, 5, 2, 8]
o Compare 5 and 2 → Swap them → [3, 4, 2, 5, 8]
o After the second pass, 5 is in its correct position.
3. Third pass:
o Compare 3 and 4 → No swap → [3, 4, 2, 5, 8]
o Compare 4 and 2 → Swap them → [3, 2, 4, 5, 8]
o After the third pass, 4 is in its correct position.
4. Fourth pass:
o Compare 3 and 2 → Swap them → [2, 3, 4, 5, 8]
o Now the array is sorted.
Time Complexity:
2. Insertion Sort
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Working:
• Insertion sort builds the final sorted array one element at a time.
• It works by picking elements from the unsorted part of the array and inserting them into the correct
position in the sorted part of the array.
• It shifts elements in the sorted part to the right to make space for the new element.
Steps:
1. Start with the second element (since the first is considered sorted).
2. Compare the current element with the elements in the sorted part.
3. Move elements that are greater than the current element to one position ahead.
4. Insert the current element in the correct position.
5. Repeat until the entire array is sorted.
Time Complexity:
3. Selection Sort
Working:
• Selection sort works by selecting the smallest (or largest) element from the unsorted part of the
array and swapping it with the first unsorted element.
• It repeats this process until the entire array is sorted.
Steps:
1. Find the smallest (or largest) element in the unsorted part of the array.
2. Swap it with the first unsorted element.
3. Move the boundary of the sorted part of the array to the right and repeat until the array is sorted.
1. First pass: Find the smallest element (2) and swap it with the first element → [2, 3, 8, 4, 5]
2. Second pass: Find the smallest element in the remaining unsorted part ([3, 8, 4, 5]), which is 3 →
No swap → [2, 3, 8, 4, 5]
3. Third pass: Find the smallest element in the remaining unsorted part ([8, 4, 5]), which is 4 → Swap
it with 8 → [2, 3, 4, 8, 5]
4. Fourth pass: Find the smallest element in the remaining unsorted part ([8, 5]), which is 5 → Swap it
with 8 → [2, 3, 4, 5, 8]
5. Now the array is sorted.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
7. Construct a binary search tree for following elements: 30,100,90,15,2,25,36,72,78,10 show each step of
construction of BST
Ans.
Stepwise construction of Binary search tree for following elements: 30,100,90,15,2,25,36,72,78,10 is as
follows:
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
8. Describe circular linked list with suitable diagram. Also state advantage of circular linked list over linear
linked list.
Ans.
Circular Linked List A circular linked list is a variation of linked list in which the last element is linked to the first
element. This forms a circular loop.
A circular linked list can be either singly linked or doubly linked. for singly linked list, next pointer of last item
points to the first item In doubly linked list, prev pointer of first item points to last item as well. We declare
the structure for the circular linked list in the same way as follows:
Struct node
{
int data;
Struct node *next;
};
Typedef struct node *Node;
Node *start = null;
Node *last = null;
For example:
struct Node {
int data;
struct Node* next;
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
};
start = start->next;
}
printf("\n");
}
int main() {
return 0;
}
10. Write C program for performing following operations on array: insertion, display.
Ans.
#include <stdio.h>
int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;
for (i = 0; i < 10; i++)
arr[i] = i + 1;
x = 50;
pos = 5;
n++;
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
// shift elements forward
for (i = n - 1; i >= pos; i--)
arr[i] = arr[i - 1];
// insert x at pos
arr[pos - 1] = x;
return 0;
}
12. Evaluate the following prefix expression: - * + 4 3 2 5 and 5, 6, 2, +, *, 12, 4, /, - show diagrammatically
Ans.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Result of above prefix expression evaluation – 37
13. Sort the given number in ascending order using Radix sort: 348, 14, 641, 3851, 74.
Ans.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
14. Write an algorithm to delete and insert a node from the beginning and end of a circular linked list.
Ans.
15. Draw an expression tree for the following expression: (a-2b+5e) 2 * (4d=6e) 5
Ans.
16. Write an algorithm to insert an element at the beginning and end of linked list
Ans.
Inserting a Node at the Beginning of a Linked List
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 6: EXIT
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Step 1: IF AVAIL = NULL
Write OVERFLOW
Go to Step 7
[END OF IF]
Step 4: PTR=START
[END OF LOOP]
Step 6: EXIT
17. Write an algorithm for performing push and pop operations on stack.
Ans.
Push algorithm:
Step 1: IF TOP = MAX-1
PRINT OVERFLOW
Goto step 4
[END OF IF]
Step 2: SET TOP = TOP + 1
Step 3: SET STACK[TOP] = VALUE
Step 4: END
Pop algorithm:
Step 1: IF TOP = NULL
PRINT UNDERFLOW
Go to step 4
[END OF IF]
Step 2: SET VAL = STACK[TOP]
Step 3: SET TOP = TOP - 1
Step 4: END
18. Write an algorithm to search a particular node in the given linked list.
Ans.
Step 1: SET PTR = HEAD
Step 2: Set I = 0
STEP 3: IF PTR = NULL
WRITE "EMPTY LIST"
GOTO STEP 8
END OF IF
STEP 6: I = I + 1
STEP 7: PTR = PTR → NEXT
[END OF LOOP]
STEP 8: EXIT
19. Create a singly linked list using data fields 90, 25, 46, 39, 56. Search a node 40 from the SLL and show
the procedure step-by-step with the help of a diagram from start to end
Ans.
To Search a data field in singly linked list, need to start searching the data field from first node of singly
linked list. ORIGINAL LIST:
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
20. Write an algorithm to count the number of nodes in a singly linked list
Ans.
0. Set Count =0
1. Initialize a node pointer, ptr = start.
2. Do following while ptr is not NULL
a. ptr = ptr -> next
b. Increment count by 1.
3. Return count.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
21. Traverse a tree in inorder preorder and post order
Ans.
Access Speed Fast access to elements by index is Access speed is not the primary
required (O(1) access). concern, and traversal is acceptable.
Size of Data Structure The size of the data structure is fixed or The size of the data structure is
known in advance. dynamic or frequently changing.
Insertion/Deletion Insertions and deletions are infrequent Frequent insertions and deletions are
and mostly at the end. needed, especially at the beginning or
middle.
Memory Efficiency Memory efficiency is important, and Slightly higher memory usage is
overhead from pointers should be acceptable due to the overhead of
minimized. pointers.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Complexity of Simplicity and ease of implementation Flexibility and dynamic memory
Implementation are priorities. management are priorities.
o Ascending order priority queue: In ascending order priority queue, a lower priority number is given
as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in an
ascending order like 1,2,3,4,5; therefore, the smallest number, i.e., 1 is given as the highest priority
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
in a priority queue.
o Descending order priority queue: In descending order priority queue, a higher priority number is
given as a higher priority in a priority. For example, we take the numbers from 1 to 5 arranged in
descending order like 5, 4, 3, 2, 1; therefore, the largest number, i.e., 5 is given as the highest priority
in a priority queue.
#include<stdio.h>
int main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
return 0;
}
Explanation:
• Base Case: When n == 0, the function returns 1 (since 0! = 1).
• Recursive Case: For n > 0, the function calls itself with n-1 and multiplies it by n.
• Example: For factorial(5), the function will compute 5 * 4 * 3 * 2 * 1.
1. Finiteness
An algorithm must always have a finite number of steps before it ends. When the operation is
finished, it must have a defined endpoint or output and not enter an endless loop.
2. Definiteness
An algorithm needs to have exact definitions for each step. Clear and straightforward directions
ensure that every step is understood and can be taken easily.
3. Input
An algorithm requires one or more inputs. The values that are first supplied to the algorithm
before its processing are known as inputs. These inputs come from a predetermined range of
acceptable values.
4. Output
One or more outputs must be produced by an algorithm. The output is the outcome of the
algorithm after every step has been completed. The relationship between the input and the
result should be clear.
5. Effectiveness
if (top == -1) {
// underflow condition
}
The overflow condition checks if the stack is full (or more memory is available) before pushing any
element. This prevents any error if more space cannot be allocated for the next item.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
if (top == sizeOfStack) {
// overflow condition
}
19. Write any four operations that can be performed on data structure.
Ans Data structure operations (Non-Primitive)
1. Inserting: Adding a new data in the data structure is referred as insertion.
2. Deleting: Removing a data from the data structure is referred as deletion.
3. Sorting: Arranging the data in some logical order (ascending or descending, numerically or alphabetically).
4. Searching: Finding the location of data within the data structure which satisfy the searching condition.
5. Traversing: Accessing each data exactly once in the data structure so that each data item is traversed or
visited.
20. Describe the concept of linked list with the terminologies: node, next Pointer, null pointer and empty list.
Node: Each data element in a linked list is represented as a node. Node contains two parts one is info (data)
and other is next pointer (address). Info part stores data and next pointer stores address of next node.
Next pointer: It is a pointer that holds address of next node in the list i.e. next pointer points to next node in
the list
Null pointer: It is a pointer that does not hold any memory address i.e. it is pointing to nothing. It is used to
specify end of the list. The last element of list contains NULL pointer to specify end of list.
Empty list: Each linked list has a header node. When header node contains NULL value, then that list is said
to be empty list.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
Explain Time & Space Complexity of Algorithm
The valid algorithm takes a finite amount of time for execution. The time required by the algorithm to solve
given problem is called time complexity of the algorithm. Time complexity is very useful measure in
algorithm analysis.
It is the time needed for the completion of an algorithm. To estimate the time complexity, we need to
consider the cost of each fundamental instruction and the number of times the instruction is executed.
Example 1: Addition of two scalar variables.
Algorithm ADD SCALAR(A, B)
//Description: Perform arithmetic addition of two numbers
//Input: Two scalar variables A and B
//Output: variable C, which holds the addition of A and B
C <- A + B
return C
The addition of two scalar numbers requires one addition operation. the time complexity of this algorithm is
constant, so T(n) = O(1) .
Space Complexity:
Problem-solving using computer requires memory to hold temporary data or final result while the program
is in execution. The amount of memory required by the algorithm to solve given problem is called space
complexity of the algorithm.
The space complexity of an algorithm quantifies the amount of space taken by an algorithm to run as a
function of the length of the input. Consider an example: Suppose a problem to find the frequency of array
elements.
It is the amount of memory needed for the completion of an algorithm.
To estimate the memory requirement we need to focus on two parts:
(1) A fixed part: It is independent of the input size. It includes memory for instructions (code), constants,
variables, etc.
(2) A variable part: It is dependent on the input size. It includes memory for recursion stack, referenced
variables, etc.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material