Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

1.HOW TO ACESS THE THIRD ELEMENT IN AN ARRAY?

- #include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int thirdElement = arr[2];
printf("The third element in the array is: %d\n", thirdElement);
return 0;
}
2.EXPLAIN IN THE DIFFERENCE BETWEEN A FIXED SIZE ARRAY AND DYNAMIC ARRAY.
- In C, a fixed-size array has a predetermined size that cannot be altered during runtime, set at compile time. On
the other hand, a dynamic array, often implemented using pointers and memory allocation functions like malloc
and free, allows for resizing during program execution. Fixed-size arrays are suitable when the size is known
beforehand and remains constant, providing faster access due to contiguous memory allocation. Dynamic arrays,
while flexible in size, may incur overhead due to memory reallocation and fragmentation. Understanding the
distinction between these array types is crucial for efficient memory management and program performance in C.
3.NAME TWO PRIMARY OPERATIONS OF A STACK
- There are two primary operations of stack are push and pop, which are used to insert an element in the stack
and remove an element from the stack.
20.Explain Big O notation.
- .Big O notation is a mathematical notation used in computer science to describe the upper bound or worst-case
scenario of the runtime complexity of an algorithm in terms of the input size. It provides a standardized and
concise way to express how the performance of an algorithm scales as the size of the input grows.

19.Explain 2D Array.
- A two-dimensional array, also known as a 2D array, is a collection of data elements arranged in a grid-like
structure with rows and columns. Each element in the array is referred to as a cell and can be accessed by its row
and column indices/indexes.

[1]
4.EXPLAIN HOW STACK OPERATES WITH AN EXAMPLE.
- #include <stdio.h>
#define MAX_SIZE 10
int stack[MAX_SIZE];
int top = -1;
void push(int item) {
if (top == MAX_SIZE - 1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = item;
}
int pop() {
if (top == -1) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
int main() {
push(10);
push(20);
push(30);
printf("Popped element: %d\n", pop());
printf("Popped element: %d\n", pop());
return 0;
}
5.DESCRIBE THE USE OF PUSH AND POP OPERATION IN A STACK.
- In stack Push operation adds an element to the collection and Pop operation removes the most recently added
element.

[2]
6.EXPLAIN THE PRIMARY DIFFERENCE BETWEEN STACK AND A QUEUE.
- Stack follows a Last In, First Out (LIFO) arrangement, meaning the last entered data is processed first. On the
other hand, Queue follows a First In, First Out (FIFO) arrangement, meaning the first entered data is processed
first.

7. Describe how elements are inserted and deleted in a queue.


- In a queue, elements are inserted at the rear and deleted at the front. This is because queues
operate on a first-in, first-out (FIFO) basis, meaning that the first element added to the queue is
also the first to be removed.
8. Define the structure of a singly linked list.

- A singly linked list is a linear data structure made up of nodes that are connected by pointers, with each node
containing data and a reference to the next node. The first node is called the head, and the last node is called the
tail. The tail's next pointer is usually set to null.

9. Describe how to traverse a linked list to find a specific value.


- In order to traverse the linked list, we initialize a pointer with the head of the linked list. Now,
we iterate till the pointer does not point to null. In each iteration, we print the value of the node
to which the pointer currently points to and move the pointer to the next node.
10.Write down the main difference between a linked list and an array.

11.Write down the properties of a binary search tree (BST).


- - Each node has a value
.The left subtree contains only values less than the parent node's value
.The right subtree contains only values greater than or equal to the parent node's value

12.Explain the process of an in-order traversal of a binary tree.


- An inorder traversal first visits the left child (including its entire subtree), then visits the node, and finally visits
the right child (including its entire subtree). The binary search tree makes use of this traversal to print all nodes in
ascending order of value.

[3]
13.Explain AVL tree.
- An AVL is a self-balancing Binary Search Tree (BST) where the difference between the heights of left and right
subtrees of any node cannot be more than one.
14.Explain how binary search works on a sorted array.
- Binary search works on sorted arrays. Binary search begins by comparing an element in the middle of the array
with the target value. If the target value matches the element, its position in the array is returned. If the target
value is less than the element, the search continues in the lower half of the array. If the target value is greater
than the element, the search continues in the upper half of the array.

15.Describe the difference between linear search and binary search.

-
16.Explain the basic idea of the bubble sort algorithm.
- Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order. This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
In Bubble Sort algorithm,
A) traverse from left and compare adjacent elements and the higher one is placed at right side.
B) In this way, the largest element is moved to the rightmost end at first.
C) This process is then continued to find the second largest and place it and so on until
the data is sorted.

[4]
17.Discuss how the merge sort algorithm works.
Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the
divide-and-conquer approach to sort a given array of elements.
Here's a step-by-step explanation of how merge sort works:
Divide: Divide the list or array recursively into two halves until it can no more be divided.
Conquer: Each subarray is sorted individually using the merge sort algorithm.
Merge: The sorted subarrays are merged back together in sorted order. The process continues
until all elements from both subarrays have been merged.
18.Explain the primary advantage of quicksort over other sorting algorithms.
- A) It works rapidly and effectively.
B) It has the best time complexity when compared to other sorting algorithms.
C) QuickSort has a space complexity of O(logn), making it an excellent choice for situations when
space is limited.
Group b –
1.Provide simple examples for each data type to illustrate their usage.
data types and their examples:
1.Integer: Represents whole numbers. For instance, 42 or -10.
2.Character: Denotes individual letters or symbols. In C++, you can use char to represent a single
character, like 'A'. For strings (sequences of characters), you can use "Hello, World!".
3.Date: Stores calendar dates. An example could be 2024-03-26.
4.Floating Point (Float): Represents fractional numbers. For instance, 3.14 or -0.005.
5.Double: Similar to float but with higher precision (more decimal places). Example:
2.718281828459045.
6.Long: Often used for large integers. Example: 1234567890.
7.Short: Represents smaller integers. Example: 42.
8.String: A sequence of characters. For instance, "Hello" or "Programming"

[5]
2.Describe the concept of a linked list and explain its advantages over contiguous memory allocation for storing
data.

Linked List: A linked list is a data structure where elements (nodes) are connected through
pointers or references. Unlike arrays, linked lists do not require contiguous memory allocation.
Each node contains data and a reference (usually a pointer) to the next node in the sequence. The
last node typically points to null, indicating the end of the list.
Advantages of Linked Lists:
1.No External Fragmentation:
Linked lists eliminate external fragmentation, as they can use non-contiguous memory blocks.
Files can occupy any available blocks, even if they are not continuous.
This flexibility allows efficient storage of small and frequently modified files.
2.Easy to Increase File Size:
Linked lists make it relatively easy to increase the size of a file.
The system does not need to find a contiguous chunk of memory; it can simply allocate new
blocks wherever available.
3.Simpler Insertions and Deletions:
Adding or removing elements in a linked list is straightforward.
No shifting of existing elements is required, unlike in contiguous arrays.
This simplicity is especially beneficial for dynamic data structures.
4.Efficient for Large Records:
When dealing with large records, moving pointers (references) is faster than moving the actual
data items.
Linked structures handle insertions and deletions more efficiently than contiguous arrays.

[6]
3.Write down the advantages and disadvantages of the Linear search and Binary search.
Linear Search:
Advantages:
Simplicity: Linear search is straightforward to implement. It involves scanning each element in the
list until the target item is found.
Works with Unsorted Data: Linear search does not require the data to be sorted. It can handle
unsorted arrays.
Multidimensional Arrays: Linear search can be used with multidimensional arrays.
Equality Comparisons: It performs equality comparisons (checking if an element is equal to the
target).
Disadvantages:
Time Complexity: Linear search has a time complexity of , where O(n)
is n is the size of the input array. In the worst-case scenario (when the target element is not
present), it needs to check every element.
Slower Process: Due to its linear nature, it can be slow for large datasets.
Binary Search:
Advantages:
Efficiency: Binary search has a time complexity of O(logn)
, making it efficient for searching large, sorted arrays.
Simple Implementation: Binary search is relatively easy to understand and implement.
Versatility: It can be used in various applications.
Ordering Comparisons: Binary search performs ordering comparisons (checking if an element is
greater than or less than the target).
Disadvantages:
Sorted Data Requirement: Binary search requires the input data to be sorted.
Single-Dimensional Arrays Only: It works only with single-dimensional arrays.
Complexity: Binary search is more complex than linear search.

[7]
4. How array Implementation of Queue.
- To implement a queue using an array,
A) create an array
arr of size n and
B) take two variables front and rear both of which will be initialized to 0 which means the queue
is currently empty.
C) Element
i)rear is the index up to which the elements are stored in the array and
ii) front is the index of the first element of the array
Algorithm:
Step 1: IF REAR = MAX - 1
Write OVERFLOW
Go to step
[END OF IF]
Step 2: IF FRONT = -1 and REAR = -1
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: Set QUEUE[REAR] = NUM
Step 4: EXIT

[8]
5.Explain the process of implementing a stack using arrays in C language.
- Before implementing actual operations, first follow the below steps to create an empty stack.
Step 1 - Include all the header files which are used in the program and define a constant 'SIZE'
with specific value.
Step 2 - Declare all the functions used in stack implementation.
Step 3 - Create a one dimensional array with fixed size (int stack[SIZE])
Step 4 - Define a integer variable 'top' and initialize with '-1'. (int top = -1)
Step 5 - In main method, display menu with list of operations and make suitable function calls to
perform operation selected by the user on the stack.
push() function:-
Step 1 - Check whether stack is FULL. (top == SIZE-1)
Step 2 - If it is FULL, then display "Stack is FULL!!! Insertion is not possible!!!" and terminate the
function.
Step 3 - If it is NOT FULL, then increment top value by one (top++) and set stack[top] to value
(stack[top] = value).
pop() function:-
Step 1 - Check whether stack is EMPTY. (top == -1)
Step 2 - If it is EMPTY, then display "Stack is EMPTY!!! Deletion is not possible!!!" and terminate
the function.
Step 3 - If it is NOT EMPTY, then delete stack[top] and decrement top value by one (top--).
6.How are the elements of a 2D array stored in the memory?
The elements of a two-dimensional (2D) array are stored contiguously in memory.A two-
dimensional (2D) array is like a grid with rows and columns. When stored in memory, the
elements are arranged row by row. Each row occupies a continuous segment of memory,
following a row-major order. For example, consider a 2D array:
01
23
45
In memory, it’s equivalent to a 1D array: 0 1 2 3 4 5. The elements are stored consecutively. When
dynamically allocating a 2D array, you create an array of pointers. Each pointer points to a
separate one-dimensional array (a row), and these individual rows are also stored contiguously.

[9]
7.What is a doubly-linked list? Give some examples.
A doubly linked list is a data structure where each node contains a value and two pointers: one
pointing to the previous node and another to the next node. This allows efficient traversal in both
directions. _Here are some examples:
Subway Stops: Imagine your daily subway commute. Each stop represents a node, with your
home as the head and your workplace as the tail. You can move forward or backward through the
stops, just like traversing a doubly linked list.
Web Browser History: When you visit websites in a web browser, each site (node) can be
navigated forward or backward using the browser’s forward and back buttons. This mirrors the
traversal of a doubly linked list in a computer program.
9.Explain the differences between signed and unsigned integers in C. How do these differences
affect memory usage and data manipulation?

Another important distinction is the memory usage of unsigned and signed integers. Since they
can only hold non-negative values, unsigned integers don’t need to allocate a bit to represent the
sign. This means that unsigned integers can utilize all of their bits to store the number’s
magnitude, allowing them to represent larger values than signed integers with the
same number of bits.

[10]
10.Describe the process of balancing an AVL tree. Provide a detailed example of inserting elements into an AVL
tree and explain how rotations are used to maintain balance.
- AVL trees can maintain this balance by keeping track of the balance factor of each node. If the balance factor of
a node doesn't lie in the range
[−1,1] , then AVL trees can perform rotations. These rotations involve switching around positions of nodes in the
unbalanced node's sub-tree(s) each time the balance factor is disrupted.
There are two categories of rotations, which each contain 2 types of rotations. Each rotation deals with a
particular type of imbalance that may arise.
Single rotations
There are 2 rotations in this category:
Right rotation—to deal with the left-left imbalance.
Left rotation—to deal with the right-right imbalance.
Given an unbalanced node
𝑥 with children 𝑦 and 𝑇1, a single-rotation involves making 𝑦 a parent of 𝑥
Double rotations
There are 2 rotations in this category:
Right-left rotation—to deal with the right-left imbalance.
Left-right rotation—to deal with the left-right imbalance.
A) Given an unbalanced node 𝑥 with children 𝑦 and 𝑇1, and given that 𝑦 is the parent of node 𝑧, the first rotation
involves making 𝑧 the parent of 𝑦. Now, 𝑧 is a child of 𝑥.
B) Then, the second rotation involves making 𝑧 the parent of 𝑥 as well.
11.How String is represented in Memory?How to take user input in a String.

In C, strings are represented in memory as an array of characters. A string is a sequence of


characters terminated by a null character ('\0'). The characters in the string are stored in
consecutive memory locations, forming a character array.
User input of string in C:-
#include <stdio.h>
int main()
{ char str[20];
gets(str);
printf("%s", str);
return 0; }

[11]
12.Define algorithmic complexity and explain the significance of big O notation. Provide an example of an
algorithm and analyze its complexity using big O notation.
- Algorithmic complexity is a measure of how long an algorithm would take to complete given an input of size n. If
an algorithm has to scale, it should compute the result within a finite and practical time bound even for large
values of n.
Significance of Big O notation:-
Big-O, commonly referred to as “Order of”, is a way to express the upper bound of an algorithm’s time complexity,
since it analyses the worst-case situation of algorithm. It provides an upper limit on the time taken by an
algorithm in terms of the size of the input. It’s denoted as O(f(n)), where f(n) is a function that represents the
number of operations (steps) that an algorithm performs to solve a problem of size n.
Analysis of algorithm using big O notation:-
The bubble sort algorithm’s average/worst time complexity is O(n²), as we have to pass through the array as many
times as there are pairs in a provided array. Therefore, when time is a factor, there may be better options.
Worst-case time complexity: O(n²).
Average time complexity: O(n²).
Best-case time complexity: O(n), the array is already sorted.

[12]
Group c --

1.Provide simple examples for each data type to illustrate their usage.
- A Stack is a linear data structure that follows a particular order in which the operations are
performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that
the element that is inserted last, comes out first and FILO implies that the element that is
inserted first, comes out last.
To implement the stack, it is required to maintain the pointer to the top of the stack , which is the
last element to be inserted because we can access the elements only on the top of the stack.
Applications of stack:-
Recursion
Expression Evaluation and Parsing
Depth-First Search (DFS)
Undo/Redo Operations
Browser History
Function Calls
Operations on stack:-
push() to insert an element into the stack
pop() to remove an element from the stack
top() Returns the top element of the stack.
isEmpty() returns true if stack is empty else false.
isFull() returns true if the stack is full else false.

[13]
2. What is Binary Search Algorithm?
Binary search is a search algorithm used to find the position of a target value within a
sorted array. It works by repeatedly dividing the search interval in half until the target
value is found or the interval is empty. The search interval is halved by comparing the
target element with the middle value of the search space.
Algorithm:
Let array a[n] of elements in increasing order, n>=0 determine whether X is present
and if so, set j such that x = a[j] else return 0.
binsrch(a[], n, x)
{
low = 1; high = n;
while (low < high) do
{
mid = (low + high)/2
if (x < a[mid])
high = mid 1;
else if (x > a[mid])
low = mid + 1;
else return mid;
}
return 0;
}
Advantages of Binary Search:
● Binary search is faster than linear search, especially for large arrays.
● More efficient than other searching algorithms with a similar time complexity,
such as interpolation search or exponential search.
● Binary search is well-suited for searching large datasets that are stored in external
memory, such as on a hard drive or in the cloud.
Disadvantages of Binary Search:
[14]
● The array should be sorted.
● Binary search requires that the data structure being searched be stored in
contiguous memory locations.
● Binary search requires that the elements of the array be comparable,
meaning that they must be able to be ordered.
3. Discuss Sorting?Write down the Characteristics of Sorting Algorithms?How many types are sorting in Data
Structure?

Sorting :
Sorting refers to rearrangement of a given array or list of elements according to a
comparison operator on the elements. The comparison operator is used to decide the new
order of elements in the respective data structure. Sorting means reordering of all the
elements either in ascending or in descending order.
Characteristics of Sorting Algorithms:
● Time Complexity: Time complexity, a measure of how long it takes to run an
algorithm, is used to categorize sorting algorithms. The worst-case, average-case,
and best-case performance of a sorting algorithm can be used to quantify the time
complexity of the process.
● Space Complexity: Sorting algorithms also have space complexity, which is the
amount of memory required to execute the algorithm.
● Stability: A sorting algorithm is said to be stable if the relative order of equal
elements is preserved after sorting. This is important in certain applications where
the original order of equal elements must be maintained.
● In-Place Sorting: An in-place sorting algorithm is one that does not require
additional memory to sort the data. This is important when the available memory
is limited or when the data cannot be moved.
● Adaptivity: An adaptive sorting algorithm is one that takes advantage of
pre-existing order in the data to improve performance.
types are sorting:
1.Bubble Sort 2.Insertion Sort 3.Merge Sort 4.Quick Sort

[15]
4. Explain Bubble Sort? Write down the algorithm of bubble sort? Advantages and Disadvantages of the bubble
sort?

- Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in the wrong order. This algorithm is not suitable for large data sets as its
average and worst-case time complexity is quite high.
In Bubble Sort algorithm,
A) traverse from left and compare adjacent elements and the higher one is placed at right side.
B) In this way, the largest element is moved to the rightmost end at first.
C) This process is then continued to find the second largest and place it and so on until the data is
sorted.
Advantages of Bubble Sort:
● Bubble sort is easy to understand and implement.
● It does not require any additional memory space.
● It is a stable sorting algorithm, meaning that elements with the same key value maintain their
relative order in the sorted output.
Disadvantages of Bubble Sort:
● Bubble sort has a time complexity of O(N2) which makes it very slow for large data sets
● Bubble sort is a comparison-based sorting algorithm, which means that it requires a
comparison operator to determine the relative order of elements in the input data set. It can limit
the efficiency of the algorithm in certain cases.

[16]

You might also like