0% found this document useful (0 votes)
14 views

FDS Paper Solution

Uploaded by

shamram8974
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

FDS Paper Solution

Uploaded by

shamram8974
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

S.E.

(Computer Engineering) (Semester – III)


SPPU EXAM MOST IMP QUESTIONS (2019 Pattern)
Question Bank [Solved all Question Paper]

210242 : FUNDAMENTALS OF DATA STRUCTURES


(2019 Pattern) (Semester - III) (210242)

• Chapter Wise Most IMP Answer Key –

UNIT – 3 Searching and Sorting Marks


Searching : Search Techniques - Sequential Search / Linear
Search, Variant of Sequential Search- Sentinel Search, Binary
Search, Fibonacci Search, and Indexed Sequential Search.
Sorting: Types of Sorting-Internal and External Sorting,
General Sort Concepts-Sort Order, Stability, Efficiency, and
18 Marks
Number of Passes, Comparison Based Sorting Methods-Bubble
Sort, Insertion Sort, Selection Sort, Quick Sort, Shell Sort,
Non-comparison Based Sorting Methods-Radix Sort, Counting
Sort, and Bucket Sort, Comparison of All Sorting Methods and
their complexities.

Sr. Questions & Answers


Explain the selection sort with algorithm sort the given no. using selection
sort & show the content of array after every pass. What is the time
1
complexity of selection sort?
Given no: 34, 9, 78, 65, 12, -8
Selection Sort :
- Selection Sort is a comparison-based sorting algorithm.
- It works by repeatedly selecting the smallest (or largest) element from
the unsorted portion of the array and swapping it with the first unsorted
-- element.
- This process is repeated until the entire array is sorted.
Working :
- Scan the array to find its smallest element and swap it with the first
element. Then, starting with the second element scan the entire list to

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 1


find the smallest element and swap it with the second element. Then
starting from the third element the entire list is scanned in order to find
the next smallest element. Continuing in this fashion we can sort the
entire list.
- Generally, on pass i (0 ≤ i ≤ n-2), the smallest element is searched among
last n-i elements and is swapped with A[i]
Algorithm of Selection Sort:
1. Start from the first element (i = 0) of the array.
2. Set the first unsorted element as the minimum.
3. Compare this element with the rest of the array to find the smallest
element.
4. Swap the smallest element with the first unsorted element.
5. Move the boundary of the sorted array by one element to the right.
6. Repeat the process for the remaining unsorted part of the array until the
array is fully sorted.
Example : Sort given array by using selection sort method 34, 9, 78, 65, 12,-8.
Show step by step execution of all passes. What is the time complexity of
selection sort?

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 2


Time Complexity –
- Best Case: O(n²)
- Average Case: O(n²)
- Worst Case: O(n²)
-----------------------------------------------------------------------------------------------

Some Practice Question elements –


1) 50, 23, 03, 18, 9, 01, 70, 21, 20, 6, 40, 04.
2) 27, 76, 17, 9, 45, 58, 90, 79, 100
3) 81, 5, 27, –6, 61, 93, 4, 8, 104, 15

Tips –
- Practice all above question as it is asked in each example for 9 marks.
- While writing answers read question carefully & write according to it.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 3


Explain in brief the different searching techniques. What is the time
2
complexity of each of them?
1) Sequential Search (Linear Search)
- Sequential search is the simplest searching algorithm.
- It works by checking each element of the array (or list) one by one, starting
from the first element, until the target element is found or the entire array
is traversed.
Time Complexity :
- The time complexity of this algorithm is O(n).
- The time complexity will increase linearly with the value of n.
- For higher value of n sequential search is not satisfactory solution.
Steps:
- Start from the first element and compare it with the target element.
- If it matches, return the position of the element.
- If not, continue checking the next element.
- If no match is found by the end of the array, return "element not found".
Advantages:
-- - Simple to implement
- Not require specific ordering before applying
Disadvantages:
- It is less efficient
---------------------------------------------------------------------------------
2) Fibonacci Search
- In binary search method we divide the number at mid and based on mid
element i.e. by comparing mid element with key element we search
either the left sublist or right sublist.
- Thus we go on dividing the corresponding sublist each time and
comparing mid element with key element.
- This algorithm is particularly useful for searching large, sorted arrays
and can perform better than binary search in some cases.
Time Complexity:
- The time complexity of this algorithm is O(1).

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 4


Steps of Fibonacci Search Algorithm:
o Find Fibonacci Number:
Find the smallest Fibonacci number greater than or equal to the
array length n. Let this be f(m).
o Initialize Pointers:
Let two preceding Fibonacci numbers be f(m−1) and f(m−2).
While the array has elements to check:
- Compare the key with the element at index f(m−2).
- If key matches, return the index.
- If key is smaller, move Fibonacci pointers two steps down to the left
(reducing the search space).
- If key is larger, move Fibonacci pointers one step down to the right, and
adjust the offset.
o Single Element Check:
If only one element remains, check if it matches the key. Return
the index if it matches.

Example:
According to the algorithm following is the example of sorting the
elements using Fibonacci Search.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 5


Diagram Representation (Optional-for understanding):

Step Fibonacci Numbers Offset i (Index Checked) Value at A[i] Comparison


1 f = 8, a = 5, b = 3 -1 i=2 30 60 > 30
2 f = 5, a = 3, b = 2 2 i=4 50 60 > 50
3 f = 3, a = 2, b = 1 4 i=5 60 60 == 60

Thus, the key 60 is found at index 5 using Fibonacci Search.


---------------------------------------------------------------------------------
3) Binary Search
- Binary search is an efficient algorithm for searching in sorted arrays.
- Binary search is a searching algorithm in which the list of elements is
divided into two sublists and the key element is compared with the
middle element.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 6


- If the match is found then, the location of middle element is returned
otherwise, we search into either of the halves(sublists) depending upon
the result produced through the match.

Algorithm for Binary Search


- if(low>high)
- return;
- mid (low+high)/2;
- if(x==a[mid])
- return (mid);
- if(x<a[mid])
- search for x in a[low] to a[mid-1];
- else
- search for x in a[mid+1] to a[high];

Example
As mentioned earlier the necessity of this method is that all the elements
should be sorted. So let us take an array of sorted elements.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 7


Time Complexity:
- The time complexity of this algorithm is O(1).
------------------------------------------------------------------------------
4) Index Sequential Search
- Index sequential search is a searching technique in which a separate table
containing the indices of the actual elements is maintained.
- The actual search of the element is done with the help of this index table.
Steps:
- Create an index with key elements pointing to their positions in the
dataset.
- Use the index to find the section where the target element might be.
- Perform a linear search within that section.

Time Complexity:
- The time complexity of this algorithm is O(1).
For example -

• Note that the sorted index table is maintained. First we search the index
table and then with the help of an index, actual array is searched for the
element.
--------------------------------------------------------------------------------
5) Sentinel Search
- Sentinel search is an optimization over linear search.
- The sentinel value is a specialized value that acts as a flag or dummy
data. This specialized value is used in context of an algorithm which uses
its presence as a condition of termination.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 8


- Typical examples of sentinel values are
1. Null character used at the end of the string.
2. Null pointer at the end of the linked list.
3. End of file character.
- In sentinel search, when searching for a particular value in an unsorted
list, every element will be compared against this value.
Time Complexity:
- The time complexity of this algorithm is O(1).
Example:
Input: [10,20,30,40,50,60]
Key: 40
Result: The element is present
Input: [10,20,30,40,50,60]
Key: 99
Result: The element is not present in the list.
Write a pseudo code for binary search apply your algorithm on the
3 following no. stored in an array to search no: 23 & 100.
9,17,23,40,45,52,58,80,85,95,100

Pseudo Code for Binary Search


def binary_search(A, low, high, key):
if low > high:
return -1
mid = (low + high) // 2
if A[mid] == key:
return mid
-- elif A[mid] > key:
return binary_search(A, low, mid - 1, key)
else:
return binary_search(A, mid + 1, high, key)
---------------------------------------------------------------------
Execution of Binary Search on the Given Array
A = [9, 17, 23, 40, 45, 52, 58, 80, 85, 95, 100]

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 9


- Search for Key = 23
1. Initial setup:
• low = 0, high = 10
• mid = (0 + 10) // 2 = 5, so A[5] = 52
• Since 52 > 23, update high = mid - 1 = 4
2. Second step:
• low = 0, high = 4
• mid = (0 + 4) // 2 = 2, so A[2] = 23
• Key found at index 2

- Search for Key = 100


1. Initial setup:
o low = 0, high = 10
o mid = (0 + 10) // 2 = 5, so A[5] = 52
o Since 52 < 100, update low = mid + 1 = 6

2. Second step:
o low = 6, high = 10

o mid = (6 + 10) // 2 = 8, so A[8] = 85


o Since 85 < 100, update low = mid + 1 = 9

3. Third step:
o low = 9, high = 10
o mid = (9 + 10) // 2 = 9, so A[9] = 95
o Since 95 < 100, update low = mid + 1 = 10

4. Fourth step:
o low = 10, high = 10
o mid = (10 + 10) // 2 = 10, so A[10] = 100
o Key found at index 10
--------------------------------------------------------------------------
Note
a. You can write another pseudocode too
b. Execution of Binary Search on the Given Array can be written in another
ways mentioned in Q.2.
c. Check Details and do practice- Following is the Practice Example
A[0] to A[10] : 9, 17, 23,38,45,50,57,76,90,100 to search numbers 10 & 100.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 10


Explain quick sort algorithm with suitable example. What is time
4
complexity of quick sort algorithm
Quick sort algorithm
- Quick Sort is a highly efficient and widely used sorting algorithm.
- It uses the divide-and-conquer approach to sort elements in an array.
- In this method division is dynamically carried out. The three steps of
quick sort as follows:
▪ Divide – Split the array into two sub arrays
▪ Conquer – Recursively sort the two sub arrays
▪ Combine – Combine all sorted elements to form sorted elements.

Algorithm
The quick sort algorithm is performed using following two important functions
Quick and partition.
Quick (A [0...n-1],low,high)
//Problem Description: This algorithm performs sorting of
//the elements given in Array A [0...n-1]
//Input: An array A[0...n-1] in which unsorted elements are given.
-- //The low indicates the leftmost element in the list
//and high indicates the rightmost element in the list
//Output: Creates a sub array which is sorted in ascending order
if(low<high)then
//split the array into two sub arrays
m -- partition(A[low...high])// m is mid of the array
Quick (A[low...m-1])
Quick (A[mid+1...high])
Example:
Consider 25, 57, 48, 37, 12, 92, 86, 33 Sort stepwise using radix sort.

Solution : Consider the first element as a pivot element.

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 11


Now, if A[i] < Pivot element then increment i. And if A[j] > Pivot element then
decrement j. When we get these above conditions to be false, Swap A[i] and A[j]

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 12


-Time Complexity:
-

▪ Best Case: O (n log n)


▪ Average case: O (n log n)
▪ Worst Case: O(n²)
----------------------------------------------------------------------------
Practice Question: (must be practiced)
1) 29, 57, 47, 39, 36, 20, 55, 28, 31, 39.
2) 27, 76, 17, 9, 57, 90, 45, 100, 79

Explain insertion, bubble, radix sort algorithm with suitable example.


5
What is time complexity of it.
1. Insertion Sort
- In this method the elements are inserted at their appropriate place.
- Insertion Sort builds a sorted array one element at a time by repeatedly
taking the next element from the unsorted portion and inserting it into its
--
correct position in the sorted portion.

Example:
A [ 30, 70, 20, 50, 40, 10, 60 ]

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 13


Time Complexity:
• Best Case: O(n)
• Average Case: O(n²)
• Worst Case: O(n²)

Practice question: 55, 85, 45, 11, 34, 5, 89, 99, 67


--------------------------------------------------------------------------------

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 14


2. Bubble Sort
- This is the simplest kind of sorting method in this method.
- Bubble Sort repeatedly steps through the list, compares adjacent
elements, and swaps them if they are in the wrong order. This process
continues until the list is sorted.
Example:
A [ 10, 5, 4, 18, 17, 1, 2 ]
Solution: Let, 10, 5, 4, 18, 17, 1, 2 be the given list of elements. We will
adjacent elements say A[i] and A[j]. If A[i]>A[j] then swap the elements.

Pass 1
Compare 10 and 5: Swap → 5,10,4,18,17,1,2
Compare 10 and 4: Swap → 5,4,10,18,17,1,2
Compare 10 and 18: No Swap → 5,4,10,18,17,1,2
Compare 18 and 17: Swap → 5,4,10,17,18,1,2
Compare 18 and 1: Swap → 5,4,10,17,1,18,2
Compare 18 and 2: Swap → 5,4,10,17,1,2,18
End of Pass 1: 5, 4, 10, 17, 1, 2, 18

Pass 2:
Compare 5 and 4: Swap → 4,5,10,17,1,2,18
Compare 5 and 10: No Swap → 4,5,10,17,1,2,18
Compare 10 and 17: No Swap → 4,5,10,17,1,2,18
Compare 17 and 1: Swap → 4,5,10,1,17,2,18
Compare 17 and 2: Swap → 4,5,10,1,2,17,18
Compare 17 and 18: No Swap → 4,5,10,1,2,17,18
End of Pass 2: 4,5,10,1,2,17,18

Pass 3:
Compare 4 and 5: No Swap → 4,5,10,1,2,17,18
Compare 5 and 10: No Swap → 4,5,10,1,2,17,18
Compare 10 and 1: Swap → 4,5,1,10,2,17,18
Compare 10 and 2: Swap → 4,5,1,2,10,17,18
Compare 10 and 17: No Swap → 4,5,1,2,10,17,18
Compare 17 and 18: No Swap → 4,5,1,2,10,17,18
End of Pass 3: 4,5,1,2,10,17,18

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 15


Pass 4:
Compare 4 and 5: No Swap → 4,5,1,2,10,17,18
Compare 5 and 1: Swap → 4,1,5,2,10,17,18
Compare 5 and 2: Swap → 4,1,2,5,10,17,18
Compare 5 and 10: No Swap → 4,1,2,5,10,17,18
Compare 10 and 17: No Swap → 4,1,2,5,10,17,18
Compare 17 and 18: No Swap → 4,1,2,5,10,17,18
End of Pass 4: 4,1,2,5,10,17,18

Pass 5:
Compare 4 and 1: Swap → 1,4,2,5,10,17,18
Compare 4 and 2: Swap → 1,2,4,5,10,17,18
Compare 4 and 5: No Swap → 1,2,4,5,10,17,18
Compare 5 and 10: No Swap → 1,2,4,5,10,17,18
Compare 10 and 17: No Swap → 1,2,4,5,10,17,18
Compare 17 and 18: No Swap → 1,2,4,5,10,17,18
End of Pass 5: 1,2,4,5,10,17,18

Pass 6:
Compare 1 and 2: No Swap → 1,2,4,5,10,17,18
Compare 2 and 4: No Swap → 1,2,4,5,10,17,18
Compare 4 and 5: No Swap → 1,2,4,5,10,17,18
Compare 5 and 10: No Swap → 1,2,4,5,10,17,18
Compare 10 and 17: No Swap → 1,2,4,5,10,17,18
Compare 17 and 18: No Swap → 1,2,4,5,10,17,18
End of Pass 6: 1,2,4,5,10,17,18

Sorted Array: 1,2,4,5,10,17,18

Time Complexity:
• Best Case: O(n)

• Average Case: O(n²)


• Worst Case: O(n²)

Practice question: 64, 34, 25, 12, 22, 11, 90


*Note: This question can be asked for 9 marks as it is asked in may_june-2024 exam so all steps
may mandatory for 9 marks.
--------------------------------------------------------------------------------

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 16


3. Radix Sort
- Radix Sort is a non-comparison-based sorting algorithm that sorts
numbers by processing individual digits.
- In this method sorting can be done digit by digit and thus all the
elements can be sorted.
Example:
A [ 25, 06, 45, 60, 40, 50 ]
Solution:
Step 1: Sort the elements according to last digit and sort them.
Last digit Elements
0 50.60,40
1
2
3
4
5 25,45
6 06
7
8
9
Sorted Elements after Step 1: 50, 60, 40, 25, 45, 06

Step 2: Sort the elements according to second last digits and sort them.
Second Last digit Elements
0 06
1
2 25
3
4 40,45
5 50
6 60
7
8
9
Sorted Elements after Step 2: 06, 25, 40, 45, 50, 60

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 17


Sorted Array: 06, 25, 40, 45, 50, 60

Time Complexity:
• Best Case: O(nlogn)
• Average Case: O(nlogn)
• Worst Case: O(nlogn)

Practice question: 29, 57, 47, 39, 36, 20, 55, 28, 31, 39
*Note: This question can be asked for 9 marks as it is asked in nov_dec-2023 exam so all steps
may mandatory for 9 marks.

▪ Instruction
1. All Unit Wise Solution available on telegram channel
2. For the Customized Micro PDFs Solution of All Paper contact us on telegram.
3. Notes – Handwritten Notes / Textbook notes available
4. Join the telegram channel to get access to all.
5. Feel free to message us on Each single doubt, we will definitely help you regarding.….!!
---------------------------------------------------------------------------------------------------------------------------------

• Free Question Paper Set Available


• Free Hand Written Notes / PDF Format Books Available
• Model Answer / Question Paper Solution Available
• College Level / Industrial Level Project Guides
• Joined Telegram Channel - https://t.me/sppuengineersss

Join Telegram Channel - https://t.me/sppuengineersss | ©Vishal Ghuge 18


JOIN TELEGRAM CHANNEL : https://t.me/sppuengineersss
- Access all FDS Study Materials | All Subjects Study Materials
- All repeated Question paper | Last year PTQ With Solutions
- Each required Study materials with Best Notes / Handwritten
- Grab this all study materials by joining telegram channels and get
• Upto 9 CGPA
JOIN TELEGRAM CHANNEL : https://t.me/sppuengineersss
- All repeated Question paper | Last year PYQ With Solutions
- Each required Study materials with Best Notes / Handwritten
- Grab this all study materials by joining telegram channels and get

• Upto 9 CGPA

You might also like