1.
Algorithm to display the largest of three numbers
text
Step 1: Start
Step 2: Input three numbers A, B, C
Step 3: If A >= B and A >= C then
Display A is largest
Else if B >= A and B >= C then
Display B is largest
Else
Display C is largest
Step 4: Stop
2. Four characteristics of algorithms
1. Finiteness – The algorithm must terminate after a finite number of steps.
2. Definiteness – Each step must be clearly and unambiguously defined.
3. Input – The algorithm should have zero or more well-defined inputs.
4. Output – The algorithm must produce at least one output result.
3. Choice of sorting algorithm for a large dataset
I would choose Algorithm Y with time complexity O(n log n).
Justification: For large datasets, O(n log n) grows much slower than O(n²). As n
increases, the quadratic algorithm becomes impractical due to excessive
execution time. For example, sorting 1 million elements: O(n log n) ≈ 20 million
operations, while O(n²) ≈ 1 trillion operations.
4. Difference between static and dynamic data
structures
• Static data structures have a fixed memory size allocated at compile time
(e.g., arrays). Example: int arr[100]; cannot grow or shrink.
• Dynamic data structures can change size during runtime using heap memory
(e.g., linked lists). Example: a linked list can add or remove nodes as needed.
5. Algorithm to implement enqueue operation in a
queue
text
Algorithm Enqueue(queue, item, rear, maxSize)
Step 1: If rear == maxSize - 1 then
Display "Queue Overflow"
Return
Step 2: rear = rear + 1
Step 3: queue[rear] = item
Step 4: Display "Item inserted"
Step 5: End
SECTION B
1. a. Explanation of array
An array is a linear data structure that stores a fixed-size sequential collection of
elements of the same data type. Elements are accessed using an index (usually
starting from 0).
1. b. C++ program for array operations
cpp
#include <iostream>
using namespace std;
int main() {
// i. Initialize an array called numbers with the given values
int numbers[] = {15, 26, 37, 45, 59, 61, 75, 82};
int size = 8;
// ii. Loop to print all elements
cout << "All elements: ";
for (int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}
cout << endl;
// iii. Print only the first element
cout << "First element: " << numbers[0] << endl;
// iv. Delete the element at position 3 (index 3)
int pos = 3; // 0-based index for position 3
for (int i = pos; i < size - 1; i++) {
numbers[i] = numbers[i + 1];
}
size--; // reduce size
cout << "After deletion: ";
for (int i = 0; i < size; i++) {
cout << numbers[i] << " ";
}
cout << endl;
// v. Search for element 61 and print its position
int searchItem = 61;
int position = -1;
for (int i = 0; i < size; i++) {
if (numbers[i] == searchItem) {
position = i;
break;
}
}
if (position != -1)
cout << "Element 61 found at index: " << position << endl;
else
cout << "Element 61 not found" << endl;
return 0;
}
2. a. Two reasons to implement a list over an array
1. Dynamic size – Lists (linked lists) can grow and shrink dynamically during
runtime, whereas arrays have a fixed size.
2. Efficient insertion/deletion – Insertion and deletion in a list (especially at the
beginning or middle) do not require shifting elements, unlike arrays which need
O(n) shifting.
2. b. Algorithm for singly linked list operations
(insert, delete, reverse)
text
Algorithm for Singly Linked List Operations
1. Define Node structure with data and next pointer.
2. Initialize linked list: 5 -> 8 -> 12 -> 15
Insert element 10 at position 2 (0-based index 2):
- Create new node with data = 10
- Traverse to node at index 1 (position before target)
- Set new node's next = current's next
- Set current's next = new node
Result: 5 -> 8 -> 10 -> 12 -> 15
Delete element 12 at position 3 (0-based index 3):
- Traverse to node at index 2 (node before target)
- Set current's next = target's next
- Delete target node
Result: 5 -> 8 -> 10 -> 15
Reverse the order of elements:
- Initialize prev = null, current = head, next = null
- While current != null:
next = [Link]
[Link] = prev
prev = current
current = next
- Set head = prev
Result: 15 -> 10 -> 8 -> 5
Algorithm in structured form:
Procedure insertAtPosition(head, data, pos):
newNode = createNode(data)
if pos == 0:
[Link] = head
return newNode
current = head
for i = 0 to pos-2:
current = [Link]
[Link] = [Link]
[Link] = newNode
return head
Procedure deleteAtPosition(head, pos):
if pos == 0:
temp = head
head = [Link]
delete temp
return head
current = head
for i = 0 to pos-2:
current = [Link]
temp = [Link]
[Link] = [Link]
delete temp
return head
Procedure reverseList(head):
prev = null
current = head
while current != null:
next = [Link]
[Link] = prev
prev = current
current = next
return prev
2. c. Four types of linked lists
1. Singly Linked List – Each node has a data field and a single pointer to the next
node. Traversal is only forward.
2. Doubly Linked List – Each node has pointers to both the next and previous
nodes. Allows forward and backward traversal.
3. Circular Linked List – The last node's next pointer points back to the first node
(head). Can be singly or doubly circular.
4. Circular Doubly Linked List – Combines doubly linked list with circular
behavior: first node's previous points to last node, and last node's next points to
first node. Allows traversal in both directions with no NULL pointers.