Unit-2: Searching, Sorting, Linked Lists: 2M Questions
Unit-2: Searching, Sorting, Linked Lists: 2M Questions
12. Differences between Run time Polymorphism and Compile time polymorphism.
13. What is Encapsulation. Explain with an example program.
14. Write a python program which calculates areas of square, rectangle, circle using
Abstraction concept.
ALL LAB PROGRAMS OF UNIT-1
8M Questions:
1. Explain the following searching techniques with examples 1.Linear search 2.Binary
search
2. What is a single linked list? Explain Different operations on single linked list
3. Write algorithm and program for implementing Binary search
4. Write algorithm and program for implementing Linear search
5. Write a program for implementing a singly linked list.
6. Explain in detail about Insertion Sort with an example python program.
7. Explain in detail about Selection Sort with an example python program.
8. Explain in detail about Bubble Sort with an example python program.
9. Write a Python program to Insert a node at the beginning and end and to delete a node at the
beginning and end of circular linked list.
10. Write a Python program to Insert a node at the beginning and end and to delete a node at the
beginning and end of doubly linked list.
11. a) Differences between Linear and Binary search.
b) Differences between Doubly and Circular Linked Lists.
12. Perform bubble sort on these numbers( 10,2,4,8,5,12,11) and write a program for the bubble sort
13. Perform bubble sort on these numbers( 20,13,6,8,1,19,3) and write a program for the Insertion sort
14. Perform bubble sort on these numbers(9,3,7,12,8,11,15) and write a program for the Selection sort.
15. How many number of iterations will be taken to search key=12 for the given list of numbers (
10,2,4,8,5,12,11) using Linear search and Binary search. Explain step by step.
2M Questions
8M Questions
1. Define stack. Implement push pop and display functions.
2. Different types of queue? list applications of queues.
3. Mention different ways to implement stack data structure. Write sample code for each method.
4. Implement queue using stack.
5. Implement Queue using Linked lists in Python.
6. Differentiate between Queue, Priority Queue and Deque.
7. Implement Stack using Linked list in Python.
8. Explain in detail about Deque with an example python program.
9. Implement priority and double ended queue in python
10. Convert following expression X+(Y * Z) – ((N * M +O) /Q) in to post fix form.
11. Differences between stacks and queues.
10. How many iterations are possible to sort a list of 120 numbers
using Insertion Sort technique?
A)
Insertion sort does N - 1 comparisons if the input is already sorted. This is
because for every element it compares it with a previous element and
does something if the order is not right
11. How many iterations are possible to sort a list of 120 numbers
using Selection Sort technique?
A)
The loop in indexOfMinimum will always make n2/2 + n/2 n2/2 + n/2 n2/2 +
n/2n, squared, slash, 2, plus, n, slash, 2 iterations, regardless of the input.
Therefore, we can say that selection sort runs in θ( n2 ) \θ(n2) θ(n2)\θ, left
parenthesis, n, squared, right parenthesis time in all cases.
13. In Which type of Linked List there will not be None/null pointer
in the address/Next field.
A)
A list in which last node points back to the header node is called circular
linked list. The chains do not indicate first or last nodes. In this case,
external pointers provide a frame of reference because last node of a
circular linked list does not contain the NULL pointer.
8M Questions :
1. Explain the following searching techniques with examples
1.Linear search 2.Binary search
A)
Linear search : Linear search is a very basic and simple search
algorithm. In Linear search, we search an element or value in a given
array by traversing the array from the starting, till the desired element or
value is found.
Example:
if (arr[i] == x):
return i
return -1
# Driver Code
x = 10
n = len(arr)
# Function call
result = search(arr, n, x)
if(result == -1):
else:
Output
Element is present at index 3
Binary Search :Binary Search is applied on the sorted array or list of
large size. It's time complexity of O(log n) makes it very fast as compared
to other sorting algorithms. The only limitation is that the array or list of
elements must be sorted for the binary search algorithm to work on it.
if r >= l:
mid = l + (r - l) // 2
return mid
# in right subarray
else:
else:
return -1
# Driver Code
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
if result != -1:
else:
print ("Element is not present in array")
Output :
Element is present at index 3
You can determine and retrieve a specific node either from the front, the
end, or anywhere in the list.
The worst case Time Complexity for retrieving a node from anywhere in
the list is O(n).
if arr[mid] == x:
return mid
else:
else:
return -1
# Test array
arr = [ 2, 3, 4, 10, 40 ]
x = 10
# Function call
if result != -1:
else:
Output:
Element is present at index 3
Program
if (arr[i] == x):
return i
return -1
# Driver Code
x = 10
n = len(arr)
# Function call
result = search(arr, n, x)
if(result == -1):
else:
Output
Element is present at index 3
Solution
Python
1. #Represent a node of the singly linked list
2. class Node:
3. def __init__(self,data):
4. self.data = data;
5. self.next = None;
6.
7. class SinglyLinkedList:
8. #Represent the head and tail of the singly linked list
9. def __init__(self):
10. self.head = None;
11. self.tail = None;
12.
13. #addNode() will add a new node to the list
14. def addNode(self, data):
15. #Create a new node
16. newNode = Node(data);
17.
18. #Checks if the list is empty
19. if(self.head == None):
20. #If list is empty, both head and tail will point to new nod
e
21. self.head = newNode;
22. self.tail = newNode;
23. else:
24. #newNode will be added after tail such that tail's next w
ill point to newNode
25. self.tail.next = newNode;
26. #newNode will become new tail of the list
27. self.tail = newNode;
28.
29. #display() will display all the nodes present in the list
30. def display(self):
31. #Node current will point to head
32. current = self.head;
33.
34. if(self.head == None):
35. print("List is empty");
36. return;
37. print("Nodes of singly linked list: ");
38. while(current != None):
39. #Prints each node by incrementing pointer
40. print(current.data),
41. current = current.next;
42.
43. sList = SinglyLinkedList();
44.
45. #Add nodes to the list
46. sList.addNode(1);
47. sList.addNode(2);
48. sList.addNode(3);
49. sList.addNode(4);
50.
51. #Displays the nodes present in the list
52. sList.display();
Output:
def insertionSort(arr):
key = arr[i]
# Move elements of arr[0..i-1], that are
j = i-1
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
insertionSort(arr)
for i in range(len(arr)):
Output:
Sorted array is:
5
6
11
12
13
7. Explain in detail about Selection Sort with an example python
program.
A)
The selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted part
and putting it at the beginning. The algorithm maintains two subarrays
in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering
ascending order) from the unsorted subarray is picked and moved to
the sorted subarray.
# Sort
import sys
for i in range(len(A)):
# unsorted array
min_idx = i
min_idx = j
for i in range(len(A)):
print("%d" %A[i]),
Output:
Sorted array:
11 12 22 25 64
This process is repeated until all the values in a list have been compared
and swapped if necessary. Each iteration is usually called a pass. The
number of passes in a bubble sort is equal to the number of elements in a
list minus one.
def bubbleSort(arr):
n = len(arr)
for i in range(n):
# Last i elements are already in place
bubbleSort(arr)
for i in range(len(arr)):
Output:
Sorted array:
11 12 22 25 34 64 90
Output:
class CSLL:
def __init__(self):
self.start_node = None
def insert_last(self, value):
new_node = Node(value) # create new node
# empty list case
if self.start_node is None:
self.start_node = new_node
self.start_node.next = self.start_node
else: # non_empty list
temp = self.start_node
while temp.next is not self.start_node:
temp = temp.next
# insert the node here
new_node.next = self.start_node
temp.next = new_node
def display(self):
# non empty list
if self.start_node is not None:
temp = self.start_node
while temp.next is not self.start_node:
print(temp.data)
temp = temp.next
print(temp.data)
s1 = CSLL()
# create list by calling insert_last
for i in range(10,50,10):
s1.insert_last(i)
s1.insert_last(100)
s1.display()
output
10
20
30
40
100
delete a node at end
class CSLL:
def __init__(self):
self.start_node = None
def insert_last(self, value):
new_node = Node(value) # create new node
# empty list case
if self.start_node is None:
self.start_node = new_node
self.start_node.next = self.start_node
else: # non_empty list
temp = self.start_node
while temp.next is not self.start_node:
temp = temp.next
# insert the node here
new_node.next = self.start_node
temp.next = new_node
def display(self):
# non empty list
if self.start_node is not None:
temp = self.start_node
while temp.next is not self.start_node:
print(temp.data, end= ' ')
temp = temp.next
print(temp.data)
def delete_last(self):
if self.start_node is not None:
# single node
if self.start_node.next is self.start_node:
self.start_node = None
else:
temp = self.start_node
while temp.next.next is not self.start_node:
temp = temp.next
temp.next= self.start_node
s2 = CSLL()
for i in range(50,0,-10):
s2.insert_last(i)
s2.display()
s2.delete_last()
s2.display()
output
50 40 30 20 10
50 40 30 20
delete a node at beginning
class CSLL:
def __init__(self):
self.start_node = None
def insert_last(self, value):
new_node = Node(value) # create new node
# empty list case
if self.start_node is None:
self.start_node = new_node
self.start_node.next = self.start_node
else: # non_empty list
temp = self.start_node
while temp.next is not self.start_node:
temp = temp.next
# insert the node here
new_node.next = self.start_node
temp.next = new_node
def display(self):
# non empty list
if self.start_node is not None:
temp = self.start_node
while temp.next is not self.start_node:
print(temp.data, end=' ')
temp = temp.next
print(temp.data)
def delete_first(self):
# non empty list
if self.start_node is not None:
# single node
if self.start_node.next is self.start_node:
self.start_node = None
else: # multiple nodes case
temp = self.start_node
while temp.next is not self.start_node:
temp = temp.next
temp.next = self.start_node.next
self.start_node = self.start_node.next
s3 = CSLL()
for i in range(50,100,10):
s3.insert_last(i)
s3.display()
s3.delete_first()
s3.display()
output
50 60 70 80 90
60 70 80 90
10. Write a Python program to Insert a node at the beginning and
end and to delete a node at the beginning and end of doubly
linked list.
Insert a node at the beginning
A) class Node:
def __init__(self, value):
self.data = value
self.next = None
self.prev = None
Insert first & display
class GDLL:
def __init__(self):
self.start_node = None
def insert_first(self, new_value):
# create the node
new_node = Node(new_value)
# empty list
if self.start_node is None:
self.start_node = new_node
return
# non-empty list
new_node.next = self.start_node
self.start_node.prev = new_node
self.start_node = new_node
def disp(self):
temp = self.start_node
while temp is not None:
print(temp.data, end=' ')
temp=temp.next
s1 = GDLL()
for i in range(100,50,-10):
s1.insert_first(i)
s1.disp()
output
60 70 80 90 100
Insert a node at the end
Output:
Output:
Original List:
1 2 3 4 5
Updated List:
2 3 4 5
Updated List:
3 4 5
Updated List:
4 5
Updated List:
5
Updated List:
List is empty
Definition The linear search starts searching from It finds the position of the searched
the first element and compares each element by finding the middle element
element with a searched element till the of the array.
element is not found.
Sorted data In a linear search, the elements don't The pre-condition for the binary search
need to be arranged in sorted order. is that the elements must be arranged
in a sorted order.
Implementation The linear search can be implemented The implementation of binary search is
on any linear data structure such as an limited as it can be implemented only
array, linked list, etc. on those data structures that have
two-way traversal.
Approach It is based on the sequential approach. It is based on the divide and conquer
approach.
Size It is preferrable for the small-sized data It is preferrable for the large-size data
sets. sets.
Efficiency It is less efficient in the case of large-size It is more efficient in the case of large-
data sets. size data sets.
Worst-case In a linear search, the worst- case In a binary search, the worst-case
scenario scenario for finding the element is O(n). scenario for finding the element is
O(log2n).
Best-case In a linear search, the best-case scenario In a binary search, the best-case
scenario for finding the first element in the list is scenario for finding the first element in
O(1). the list is O(1).
Here, 'link1' field is used to store the address of the previous node in
the sequence, 'link2' field is used to store the address of the next
node in the sequence and 'data' field is used to store the actual value
of that node.
Example:
Circular Linked List (CLL)
Example:
2M Questions