DS-1st Sessional Solution (2024) - 1
DS-1st Sessional Solution (2024) - 1
RN
The increasing order of growth rate is: O(1), O(log n), O(n2), , O(n2 log n)
1 a
Write the differences between array and linked list. Also write various types of linked list.
1 b
Define a stack. Evaluate the following Postfix expression:
2 3 9 * + 2 3 ^ - 6 2 / +
A Stack is a linear data structure that follows the LIFO (Last-In-First-Out) principle. Stack has one end.
1 c It contains only one pointer top pointer pointing to the topmost element of the stack. Whenever an
element is added in the stack, it is added on the top of the stack, and the element can be deleted
only from the stack. In other words, a stack can be defined as a container in which insertion and
deletion can be done from the one end known as the top of the stack.
Answer: 24
Define recursion. Find the output of the following code for n=3:
void fun(int n)
{
if (n > 0) {
fun (n - 1);
printf("%d ", n);
fun (n-1);
1 d }
}
The process in which a function calls itself directly or indirectly is called recursion and the
corresponding function is called a recursive function. Using a recursive algorithm, certain problems
can be solved quite easily.
Answer: 1 2 1 3 1 2 1
Define stable sorting techniques. Also give examples of stable and unstable sorting techniques.
In a stable sorting algorithm, data is sorted in a way that preserves the original order of elements having equal
keys. This means that if two elements have the same key, the one that appeared earlier in the input will also
1 e appear earlier in the sorted output. For instance, if element A[i] and element A[j] have the same key and i < j,
then in sorted order, A[i] should come before A[j].Eg. Insertion sort, Bubble sort, Counting sort
In an unstable sorting algorithm, data is sorted so that the order of elements with equal keys is not preserved
after sorting. Eg. Quick sort, Heap sort
Uni. RN
A polynomial is an algebraic expression consisting of variables, coefficients, and operations such as addition,
subtraction, multiplication, and non-negative exponentiation of variables.
E.g., P(x) = x² − 5x + 9
Polynomial representation using linked list
This section will see how to represent polynomials using linked lists.
Each node in the linked list denotes one term of the polynomial.
Every node stores -
• Value of exponent
• Value of coefficient
• Pointer to the next node
For array X:
L1=5
L2=21
No. of elements=5x21=105
For array Y:
L1=8
3 L2=11
L3=16
No. of elements=8x11x16=1408
E1=2-1=1
E2=2-(-5)=7
E3=3-(-10)=13
= 400+[[13*11+7]*8+1]*4
= 5204
Define the following terms in brief:
(i) Time & Space complexity
(ii) Best case, worst case & average case analysis
(iii) Asymptotic Notations
Time complexity is a concept in computer science that describes the amount of time an algorithm takes to run as
a function of the length of the input. It's a crucial aspect of algorithm analysis as it helps understand how efficiently
an algorithm performs, particularly as the size of the input data increases.
Measure of Efficiency: Time complexity provides a way to quantify the efficiency of an algorithm in terms of time.
It's particularly important for understanding the scalability of an algorithm.
Space complexity is a term in computer science used to describe the amount of memory space required by an
algorithm to run as a function of the length of the input. It is an important metric for understanding how efficient
an algorithm is in terms of memory usage, especially in environments where memory resources are limited.
Key Points About Space Complexity
Memory Usage Measurement: Space complexity measures the total amount of memory or storage space
an algorithm needs to complete. This includes both the space taken up by the input data and any additional space
used by the algorithm for variables, data structures, and function calls.
• Best case
The minimum number of steps an algorithm takes on input data. This analysis determines the lower bound on how
4 long it takes an algorithm to complete a task.
• Worst case
The maximum number of steps an algorithm takes on input data. This analysis provides information about how
long an algorithm takes to run when it's unstable or when the inputs produce poor results.
• Average case
The average number of steps an algorithm takes on input data. This analysis provides a general overview of how
the algorithm performs under normal conditions.
For example, in sequential search, the best case is when the algorithm matches the target immediately after one
comparison. The worst case is when the algorithm matches the last item in the list or doesn't match anything after
making n comparisons.
Asymptotic Notations:
• Asymptotic Notations are mathematical tools used to analyze the performance of algorithms by
understanding how their efficiency changes as the input size grows.
• These notations provide a concise way to express the behavior of an algorithm’s time or space complexity
as the input size approaches infinity.
• Rather than comparing algorithms directly, asymptotic analysis focuses on understanding the relative
growth rates of algorithms’ complexities.
• It enables comparisons of algorithms’ efficiency by abstracting away machine-specific constants and
implementation details, focusing instead on fundamental trends.
• Asymptotic analysis allows for the comparison of algorithms’ space and time complexities by examining
their performance characteristics as the input size varies.
• By using asymptotic notations, such as Big O, Big Omega, and Big Theta, we can categorize algorithms
based on their worst-case, best-case, or average-case time or space complexities, providing valuable
insights into their efficiency.
Omega notation represents the lower bound of the running time of an algorithm. Thus, it provides the best case
complexity of an algorithm.
The execution time serves as a lower bound on the algorithm’s time complexity.
It is defined as the condition that allows an algorithm to complete statement execution in the shortest amount
of time.
Let g and f be the function from the set of natural numbers to itself. The function f is said to be Ω(g), if there is a
constant c > 0 and a natural number n0 such that c*g(n) ≤ f(n) for all n ≥ n0
A stack is a linear data structure in which the insertion of a new element and removal of an existing element takes
place at the same end represented as the top of the stack.
Applications of Stacks:
• Function calls: Stacks are used to keep track of the return addresses of function calls, allowing the program
to return to the correct location after a function has finished executing.
• Recursion: Stacks are used to store the local variables and return addresses of recursive function calls,
allowing the program to keep track of the current state of the recursion.
• Expression evaluation: Stacks are used to evaluate expressions in postfix notation (Reverse Polish
Notation).
• Syntax parsing: Stacks are used to check the validity of syntax in programming languages and other formal
languages.
• Memory management: Stacks are used to allocate and manage memory in some operating systems and
programming languages.
0 (
1 A ( A
2 – (– A
3 B (– AB
4 / (– / AB
5 C (– / ABC
6 * (– * ABC/
7 D (– * ABC/D
8 + (+ ABC/D*–
9 E (+ ABC/D*–E
10 * (+* ABC/D*–E
11 F (+* ABC/D*–EF
12 / (+/ ABC/D*–EF*
13 G (+/ ABC/D*–EF*G
14 ) ABC/D*–EF*G/+
Differentiate between iteration and recursion. Write the recursive solution for Tower of Hanoi
6
problem. Show the working of your algorithm for n=3 disks.
Uni. RN
#include <stdio.h>
#include <stdlib.h>
// Node structure to represent elements in the stack
struct Node {
int data;
struct Node* next;
};
// Stack structure to manage the stack
7 struct Stack {
struct Node* top;
};
// Function to create a new node with given data
struct Node* newNode(int data) {
struct Node* node = (struct Node*) malloc(sizeof(struct Node));
node->data = data;
node->next = NULL;
return node;
}
Uni. RN
int main() {
// Creating a stack and pushing elements onto it
struct Stack* stack1 = createStack();
push_data(stack1, 1);
push_data(stack1, 2);
push_data(stack1, 3);
push_data(stack1, 4);
8
Uni. RN
Write an algorithm/function for bubble sort. Also explain the time complexity in all cases. Apply
the algorithm on the following elements:
11, 3, 14, 1, 17, 61, 51, 18, 91, 1, 19, 10
Bubble Sort
Bubble Sort is a simple sorting algorithm commonly used to sort elements in a list or array. It works by repeatedly
comparing adjacent elements and swapping them if they are in the wrong order. The algorithm iterates through
the list multiple times until no more swaps are needed, resulting in a sorted sequence. During each iteration, the
largest unsorted element "bubbles" up to its correct position, hence the name "Bubble Sort."