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

Q1.

Write short notes on:


1. Illustrate the linked list representation in list.

A linked list is a linear data structure consisting of nodes where each node contains
a data element and a reference (or pointer) to the next node in the sequence.

It differs from an array in that the elements are not stored in contiguous memory
locations; instead, each node can be located anywhere in memory, and the links
between nodes determine the sequence.

Each node consists of two fields, the information stored in linked list and a pointer
that stores address of the next node in case of a singly linked list and also includes
the address of the previous node in case of doubly linked list.

The last element contains null in its field because it will point to no node.

Following is the creation of a singly linked list:

In a doubly linked list, each node contains pointers to both the next node and the
previous node.
2. Write a program to insert an element in sorted array at it’s deserving
position and explain.

3. Explain Sparse matrices and their types with the help of suitable example.
Sparse matrices are matrices that contain mostly zero elements. Storing sparse matrices
efficiently can lead to significant savings in memory and computation time compared to
dense matrices (matrices with mostly non-zero elements).
1. Tridiagonal Matrix:
• A tridiagonal matrix is a square matrix where all elements outside the main
diagonal, the diagonal above the main diagonal (superdiagonal), and the
diagonal below the main diagonal (subdiagonal) are zero.
• Mathematically, in an n×n tridiagonal matrix, the non-zero elements are
located in three diagonals: the main diagonal, the subdiagonal (one position
below the main diagonal), and the superdiagonal (one position above the
main diagonal).
• Example of a 4x4 tridiagonal matrix:

• Tridiagonal matrices often arise in the numerical solution of partial


differential equations (PDEs) and linear systems with special structures.
2. Lower Triangular Matrix:
• A lower triangular matrix is a square matrix where all elements above the
main diagonal are zero.
• Mathematically, in an n×n lower triangular matrix, the non-zero elements are
located on and below the main diagonal.
• Example of a 4x4 lower triangular matrix:

• Lower triangular matrices are commonly used in linear algebra, solving


systems of linear equations, and implementing algorithms that involve
forward substitution.
3. Upper Triangular Matrix:
• An upper triangular matrix is a square matrix where all elements below the
main diagonal are zero.
• Mathematically, in an n×n upper triangular matrix, the non-zero elements
are located on and above the main diagonal.
• Example of a 4x4 upper triangular matrix:

• Upper triangular matrices are also used in linear algebra, solving systems of
linear equations, and implementing algorithms that involve backward
substitution.

4. Write a program to create a linear link list, showing all the operation that
can be performed on a linked list.
5. Define Data Structure. In how many ways can you categorized data
structure? Explain each of them.
A data structure is a way of organizing and storing data in a computer so that it can be
efficiently accessed and manipulated. It provides a systematic way of representing and
managing data, allowing for operations such as insertion, deletion, searching, and sorting to
be performed efficiently.
1. Linear Data Structures: Linear data structures organize data elements sequentially
or in a linear manner, where each element is connected to its adjacent element.
Examples of linear data structures include arrays, linked lists, stacks, and queues.
• Arrays: A collection of elements stored in contiguous memory locations,
accessed using indices.

• Linked Lists: A collection of nodes where each node contains a data element
and a reference to the next node.

• Stacks: A Last-In-First-Out (LIFO) data structure where elements are added


and removed from one end (top).
• Queues: A First-In-First-Out (FIFO) data structure where elements are added
at the rear end and removed from the front end.

2. Non-Linear Data Structures: Non-linear data structures organize data elements in


a hierarchical or non-sequential manner, allowing for more complex relationships
between elements. Examples include trees and graphs.
• Trees: A hierarchical data structure consisting of nodes connected in parent-
child relationships, where each node can have multiple children.

• Graphs: A collection of nodes (vertices) connected by edges, representing


relationships between elements.

6. Perform Selection sort on the following data.


6, 55, 11, 10, 8.

Q3. Write a program to create a doubly link list, showing all the operation that
can be performed on a linked list.
Q4. (a) Write a neat algorithm for merge sort and explain.
(b) Perform the Merge Sort on the following data: -
12, 34, 43, 2, 1, 5, 6, 32, 90, 18

Q5. (a) How 2-D array are internally stored? What is Column major and Row
Major matrices.
In computer memory, a 2-D array is internally stored as a linear array (a 1-
D array) using either row-major order or column-major order. These two
storage orders define how the elements of the 2-D array are laid out in
memory.
1. Row-Major Order:
In row-major order, elements of a 2-D array are stored row by row in
memory. This means that the elements of each row are stored consecutively,
and rows are stored one after the other in memory.
For example, consider a 3x3 matrix:
123
456
789
In row-major order, the elements are stored in memory as:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
In terms of memory addressing, the elements are stored in increasing order
of memory addresses across rows, i.e., elements in the same row are stored
next to each other, and rows are stored one after another.
2. Column-Major Order:
In column-major order, elements of a 2-D array are stored column by column
in memory. This means that the elements of each column are stored
consecutively, and columns are stored one after the other in memory.
For the same 3x3 matrix:
123
456
789
In column-major order, the elements are stored in memory as:
[1, 4, 7, 2, 5, 8, 3, 6, 9]

In terms of memory addressing, the elements are stored in increasing order


of memory addresses down columns, i.e., elements in the same column are
stored next to each other, and columns are stored one after another.

The choice between row-major and column-major order can affect


performance in algorithms that access elements of 2-D arrays. For example,
in languages like C and C++, arrays are stored in row-major order by default.
However, in languages like Fortran, arrays are stored in column-major order
by default.

It's important to be aware of the memory layout when working with 2-D
arrays, especially in performance-critical applications or when interfacing
with libraries that expect a specific storage order.
(b) Write a algorithm of selection sort and perform it on the following data.
12, 23, 3, 4, 5, 65, 76, 6, 54, 43, 32, 2

Q6. (a) Write a short notes on the different operation that can be performed on
the data structure.

Data structures are fundamental components in computer science and programming,


designed to efficiently organize and manipulate data. Various operations can be performed
on data structures, each serving different purposes and providing specific functionalities.
Here are some common operations performed on data structures:

1. Insertion: Insertion involves adding new data elements into the data
structure. Depending on the type of data structure, insertion can occur at the
beginning, end, or a specific position within the structure. Examples include
inserting elements into arrays, adding nodes to linked lists, and inserting key-value
pairs into hash tables.
2. Deletion: Deletion refers to removing data elements from the data structure.
Similar to insertion, deletion can target specific elements or follow a particular order
(e.g., first-in-first-out in queues). Examples include deleting elements from arrays,
removing nodes from linked lists, and deleting entries from hash tables.
3. Traversal: Traversal involves visiting each element in the data structure in a
systematic manner. Different traversal techniques are used depending on the
structure, such as linear traversal for arrays, traversing linked lists, and tree traversal
algorithms like in-order, pre-order, and post-order traversal for trees.
4. Searching: Searching operations involve finding a specific element or value
within the data structure. Common search algorithms include linear search for
arrays, binary search for sorted arrays, and various search techniques for tree-based
structures like binary search trees.
5. Sorting: Sorting operations arrange the elements of the data structure in a
specified order (e.g., ascending or descending). Sorting is crucial for efficiently
retrieving and organizing data. Examples of sorting algorithms include bubble sort,
insertion sort, merge sort, and quicksort.
6. Accessing/Retrieval: Accessing or retrieval operations involve obtaining
specific elements or values from the data structure. This could include accessing
elements by index in arrays, retrieving nodes in linked lists, or searching for keys in
hash tables.
7. Updating/Modification: Updating or modification operations involve
changing the value or properties of existing elements within the data structure. This
can include updating values in arrays, modifying attributes of nodes in linked lists,
or changing key-value pairs in hash tables.

(b) Write a program to insert a node in the beginning of Singly Linked List.
(c) Write a program to delete a node in the end of doubly Linked list.

Q7. (a) Define hashing? Why do we use hashing? Discuss any two-hashing
method with example.
Hashing refers to the process of generating a fixed-size output from an input
of variable size using the mathematical formulas known as hash functions. This
technique determines an index or location for the storage of an item in a data
structure.
There are majorly three components of hashing:
1. Key: A Key can be anything string or integer which is fed as input in the
hash function the technique that determines an index or location for
storage of an item in a data structure.
2. Hash Function: The hash function receives the input key and returns the
index of an element in an array called a hash table. The index is known as
the hash index.
3. Hash Table: Hash table is a data structure that maps keys to values using
a special function called a hash function. Hash stores the data in an
associative manner in an array where each data value has its own unique
index.
NEED:
1. Fast Data Retrieval: Hashing allows for fast retrieval of data based on its unique
identifier (hash code). Instead of searching through a large dataset sequentially, a
hash code can be used to directly access the corresponding data item.
2. Efficient Storage: Hashing enables efficient storage of data by mapping large keys
or data items to smaller hash codes. This reduces memory requirements and
improves performance in terms of storage and access.
3. Collision Handling: Hashing provides techniques for handling collisions, which
occur when multiple data items map to the same hash code. Collision resolution
methods ensure that data integrity is maintained and that each data item is
correctly stored and retrievable.
Types of hash functions:
1. Division Method.
The hash function divides the value k by M and then uses the remainder
obtained.
Formula: h(K) = k mod M
Here,
k is the key value, and
M is the size of the hash table.

2. Mid Square Method.


Square the value of the key k i.e. k 2 and then extract the middle r digits
as the hash value.
Formula: h(K) = h(k x k)
Here,
k is the key value.

3. Folding Method.
Divide the key-value k into a number of parts i.e. k1, k2, k3,….,kn, where
each part has the same number of digits except for the last part that can
have lesser digits than the other parts.
Add the individual parts. The hash value is obtained by ignoring the last
carry if any.

4. Multiplication Method.
1. Choose a constant value A such that 0 < A < 1.
2. Multiply the key value with A.
3. Extract the fractional part of kA.
4. Multiply the result of the above step by the size of the hash table i.e.
M.
5. The resulting hash value is obtained by taking the floor of the result
obtained in step 4.

(b) Which searching technique is best and under what condition? Justify
your answer with the help of an example.
The "best" searching technique depends on various factors such as the size of the dataset
(small- linear, large- binary) , the nature of the data (sorted-linear, unsorted-binary), memory
constraints, and the frequency of searches versus updates.
Q8 (a) Which sorting technique is better and why? Explain with an example.
The "best" sorting technique among selection sort, bubble sort, insertion sort, and merge sort
depends on various factors such as the size of the dataset, the nature of the data (partially
sorted or unsorted), memory constraints, and the desired time complexity.
In conclusion, for small datasets or nearly sorted datasets, bubble sort, selection sort, and
insertion sort can be suitable. However, for large datasets or when efficiency is crucial, merge
sort is generally the better choice due to its superior time complexity.

Q9. (a) Add and subtract the following two sparse matrices.
(b) Perform insertion sort on the following values.
6, 55, 11, 10, 18

You might also like