DSU Super 25 BY RAJAN SIR - V2V
DSU Super 25 BY RAJAN SIR - V2V
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
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
`
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
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:
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
`
10. Write C program for performing following operations on array: insertion, display.
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
inta[10],x,i,n,pos;
clrscr();
printf(“Enter the number of array element\n”);
scanf(“%d”,&n);
printf(“Enter the array with %d element\n”, n);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“Enter the key value and its position\n”);
scanf(“%d%d” ,&x,&pos);
for(i=n; i >= pos; i--)
{
a[i]=a[i-1];
}
a[pos-1]=x;
printf(“Array element\n “);
for(i=0;i<n+1;i++)
printf(“%d\t”,a[i]);
getch();
}
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
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
`
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.
Delete at Beginning (Circular Queue):
Step 1:
IF FRONT = -1
◦ Write "UNDERFLOW"
◦ Goto Step 5
◦ [END of IF]
Step 2:
SET VAL = QUEUE[FRONT]
Step 3:
IF FRONT == REAR
◦ SET FRONT = REAR = -1
◦ ELSE
◦ IF FRONT = MAX - 1
◦ SET FRONT = 0
◦ ELSE
◦ SET FRONT = FRONT + 1
◦ [END of IF]
◦ [END of IF]
Step 4:
Write "Deleted Element = VAL"
Step 5:
EXIT
---
Step 1:
IF FRONT = -1
◦ Write "UNDERFLOW"
◦ Goto Step 5
◦ [END of IF]
Step 2:
SET VAL = QUEUE[REAR]
Step 3:
IF FRONT == REAR
◦ SET FRONT = REAR = -1
◦ ELSE
◦ IF REAR = 0
◦ SET REAR = MAX - 1
◦ ELSE
◦ SET REAR = REAR - 1
◦ [END of IF]
◦ [END of IF]
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
Step 4:
Write "Deleted Element = VAL"
Step 5:
EXIT
Insert at Beginning (Circular Queue):
Step 1:
IF (FRONT == 0 AND REAR == MAX - 1) OR (FRONT = REAR + 1)
◦ Write "OVERFLOW"
◦ Goto Step 6
◦ [END of IF]
Step 2:
IF FRONT = -1
◦ SET FRONT = REAR = 0
◦ Goto Step 5
◦ [END of IF]
Step 3:
IF FRONT = 0
◦ SET FRONT = MAX - 1
◦ ELSE
◦ SET FRONT = FRONT - 1
◦ [END of IF]
Step 4:
SET QUEUE[FRONT] = VAL
Step 5:
Write "Inserted Element = VAL"
Step 6:
EXIT
---
Step 1:
IF (FRONT == 0 AND REAR == MAX - 1) OR (FRONT = REAR + 1)
◦ Write "OVERFLOW"
◦ Goto Step 6
◦ [END of IF]
Step 2:
IF FRONT = -1
◦ SET FRONT = REAR = 0
◦ Goto Step 5
◦ [END of IF]
Step 3:
IF REAR = MAX - 1
◦ SET REAR = 0
◦ ELSE
◦ SET REAR = REAR + 1
◦ [END of IF]
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
Step 4:
SET QUEUE[REAR] = VAL
Step 5:
Write "Inserted Element = VAL"
Step 6:
EXIT
15. Draw an expression tree for the following expression: (a-2b+5e) 2 * (4d=6e) 5
Ans.
16.Explain stack & Queue implementation using array & representation using linked list
Ans.
• Concept: A stack follows the LIFO (Last In, First Out) principle. The last element added is the first
one to be removed. We use an array to store elements, and an integer top tracks the index of the last
element.
Operations:
Code:
#include <stdio.h>
#define MAX 10
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
int stack[MAX];
int top = -1;
// Push operation
void push(int data) {
if (top < MAX - 1) {
stack[++top] = data;
} else {
printf("Stack Overflow\n");
}
}
// Pop operation
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
// Peek operation
int peek() {
if (top == -1) {
return -1; // Stack is empty
}
return stack[top];
}
• Concept: A linked list-based stack uses nodes where each node has data and a pointer to the next
node. The top of the stack is the head of the linked list.
Operations:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Push operation
void push(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = top;
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
top = newNode;
}
// Pop operation
int pop() {
if (top == NULL) {
printf("Stack Underflow\n");
return -1;
}
struct Node* temp = top;
int data = temp->data;
top = top->next;
free(temp);
return data;
}
// Peek operation
int peek() {
if (top == NULL) {
return -1; // Stack is empty
}
return top->data;
}
• Concept: A queue follows the FIFO (First In, First Out) principle. The first element added is the
first one to be removed. We use an array with two pointers: front and rear.
Operations:
Code:
#include <stdio.h>
#define MAX 10
int queue[MAX];
int front = -1, rear = -1;
// Enqueue operation
void enqueue(int data) {
if ((rear + 1) % MAX == front) {
printf("Queue Overflow\n");
} else {
if (front == -1) front = 0;
rear = (rear + 1) % MAX;
queue[rear] = data;
}
}
// Dequeue operation
int dequeue() {
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
if (front == -1) {
printf("Queue Underflow\n");
return -1;
}
int data = queue[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
return data;
}
// Front operation
int frontElement() {
if (front == -1) {
return -1; // Queue is empty
}
return queue[front];
}
• Concept: A linked list-based queue uses nodes, where each node contains data and a pointer to the
next node. The front pointer points to the first node and the rear pointer to the last node.
Operations:
Code:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
// Enqueue operation
void enqueue(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
} else {
rear->next = newNode;
rear = newNode;
}
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
// Dequeue operation
int dequeue() {
if (front == NULL) {
printf("Queue Underflow\n");
return -1;
}
struct Node* temp = front;
int data = temp->data;
front = front->next;
if (front == NULL) rear = NULL; // Queue is empty
free(temp);
return data;
}
// Front operation
int frontElement() {
if (front == NULL) {
return -1; // Queue is empty
}
return front->data;
}
Summary:
Operation Stack (Array) Stack (Linked List) Queue (Array) Queue (Linked List)
Insert Push (top++) Push (new node) Enqueue (rear++) Enqueue (new node)
Remove Pop (top--) Pop (remove node) Dequeue (front++) Dequeue (remove node)
Peek/Front Peek (top) Peek (top data) Front (front data) Front (front data)
Empty isEmpty (top == isEmpty (front ==
isEmpty (top == -1) isEmpty (front == -1)
Check NULL) NULL)
isFull (top == MAX- isFull (rear == MAX-
Full Check Not applicable Not applicable
1) 1)
17. Write an algorithm to insert an element at the beginning and end of linked list
Ans.
Ans
Algorithm to insert an element at the be
ginning of linked list:
1. Start
2. Create the node pointer *temp Struct node * temp
3. Allocate address to temp using malloc
temp = malloc(sizeof(struct node));
4. Check whether temp is null, if null then
Display “Overflow”
Else
temp
--> info=data
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
te
mp --> next=start
5. Start=temp
6. stop
18. Write an algorithm for performing push and pop operations on stack.
Ans.
Push algorithm: - Max is maximum size of stack.
Step 1: [Check for stack full/ overflow]
If stack_top is equal to max-1 then
Display output as “Stack Overflow” and return to calling function
Otherwise
Go to step 2
Step 2: [Increment stack_top] Increment stack top pointer by one.
stack_top=stack_top +1;
Step 3: [Insert element] stack [stack_top] = item;
Step 4: return to calling function
Step 5: Stop
19. Elaborate the steps for performing selection sort for given elements of array. A={37,12,4,90,49,23,-19}
Ans.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
20. Write an algorithm to search a particular node in the given linked list.
Ans.
Assumption: Node contains two fields: info and next pointer start pointer : Header node
that stores address of first node
step 1: start
step 2: Declare variable no, flag and pointer temp
step 3: Input search element
step 4: Initialize pointer temp with the
address from start pointer.
( temp=start), flag with 0
step 5: Repeat step 6
till
temp != NULL
step 6: compare: temp
-->info = no then set flag=1
and go to step 7
otherwise increment pointer temp and
go to step5
step 7: compare: flag=1 then display "
Node found"
otherwise
display "node not found"
step 8: stop
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
21. 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
`
22. Write an algorithm to count the number of nodes in a singly linked list
Ans.
Let start is pointer variable which always stores address of first node in single linked list.
If single linked list is empty then start will point to NULL.
q is pointer variable used to store address of nodes in single linked list.
Step 1: Start
Step
2: [Assign starting address of single linked list to pointer q]
q=start
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
Unsorted Data: 29 35 3 8 11 15 56 12 1 4 85 5
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
`
Preorder Traversal:
A,B,D,E,Q,F,R,H,C,I,J,K,L,P
Postorder Traversal: Q,R,F,E,H,D,B,K,J,I,P,L,C,A
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
`
Space Complexity: Total amount of computer memory required by an algorithm to complete its execution is
called as space complexity of that algorithm. Whena program is under execution it uses the computer
memory for THREE reasons.
They are as follows...
Instruction Space: It is the amount of memory used to store compiled version of instructions.
Environmental Stack: It is the amount of memory used to store information of partially executed functions
at the time of function call.
Data Space: It is the amount of memory used to store all the variables and constants. If the amount of
space required by an algorithm is increased with the increase of input value, then that space complexity is
said to be Linear Space Complexity.
Example:
int sum(int A[ ], int n)
{
int sum = 0, i;
for(i = 0; i < n; i++)
sum = sum + A[i];
return sum;
}
In the above piece of code it requires
'n*2' bytes of memory to store array variable
'a[ ]' 2 bytes of memory for integer parameter
'n' 4 bytes of memory for local integer variables 'sum' and 'i' (2 bytes each)
2 bytes of memory for return value.
Thatmeans, totally it requires '2n+8' bytes of memory to complete its execution. Here, the total amount of
memory required depends on the value of 'n'. As 'n' value increases the space required also increases
proportionately. This type of space complexity is said to be Linear Space Complexity.
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
A Priority Queue is a special type of queue where each element is associated with a priority. Elements are
dequeued in order of their priority, rather than the order they were added. In other words, the element with
the highest priority is always dequeued first, regardless of its position in the queue. A priority queue can be
implemented using various data structures such as a heap, array, or linked list.
Key Characteristics:
• Max Priority Queue: The element with the highest priority is dequeued first.
• Min Priority Queue: The element with the lowest priority is dequeued first.
Example:
Consider a scenario where tasks are processed based on their priority in a max priority queue. Assume we
have the following tasks with priorities:
Task Priority
Task A 2
Task B 5
Task C 3
Task D 1
1. Enqueueing Tasks:
o When tasks are added to the priority queue, they are stored along with their priorities.
o The queue initially contains:
arduino
Copy code
Task A (Priority 2), Task B (Priority 5), Task C (Priority 3), Task D
(Priority 1)
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
2. Dequeuing Tasks:
o The task with the highest priority (Task B, Priority 5) is dequeued first.
o After Task B is dequeued, the queue will look like:
arduino
Copy code
Task C (Priority 3), Task A (Priority 2), Task D (Priority 1)
arduino
Copy code
Task B → Task C → Task A → Task D
1. Task Scheduling: In operating systems, tasks or processes can be assigned priorities and processed
in order of their priority.
2. Dijkstra's Algorithm: Used in finding the shortest path in a graph where each node is processed
based on its priority (distance).
3. Huffman Coding: Used in data compression algorithms where characters are assigned priorities
based on frequency.
Summary:
A priority queue ensures that elements are processed according to their priority rather than the order they
were added, making it an essential data structure for handling tasks that require urgent or prioritized
processing.
if (q->rear == MAX - 1) {
return 1; // Queue is full
}
return 0; // Queue is not full
}
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material
`
int main() {
struct Queue q;
initialize(&q);
return 0;
}
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
printf("Factorial of %d is %d\n", number, factorial(number));
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.
Example Output:
mathematica
Copy code
Enter a number: 5
Factorial of 5 is 120
Free Study Material Buy Ty Diploma Buy Sy Diploma Whatsapp Group for Study Material