Professional Documents
Culture Documents
CSE 101 Tutorial 3 Sol
CSE 101 Tutorial 3 Sol
Note:
1) We are not interested in actual code in any language but would be great if you are writing in
one.
2) Make sure to handle edge cases if any.
3) Make sure you put assumptions wherever taken.
Q2. Run on [32, 41, 17, 8, 41] step by step. Default sort is ascending.
a. Bubble Sort
Code Time Complexity
O(n2), for all the three cases in general.
b) def middle_elemet_ll(self):
6-7-8-3-2
o/p: 8
Methods:
i) get the length of linked list, find mid and again traverse linked list to get the
element.
ii) Traverse LL using two pointers (famous approach, will be used in many
questions)
Q4. Write a pseudo code of implementing a dynamic array using a static array?
Please refer to the following blog.
https://www.geeksforgeeks.org/implementation-of-dynamic-array-in-python/
Q.5 Suppose you are given two sorted lists containing P and Q nodes respectively. Write
pseudocode to merge both list and return the merge list, remember the merge procedure of
merge sort?
Input:
a:5->10->15
b:2->3->20
Output: 2->3->5->10->15->20
Approaches:
i) We have two head pointers, pointing to LL(a) and LL(b), now like merge
procedure we increment pointers, and create a new node each time and
copying data of pointer which is smaller.
ii) We can play with the pointers and without using extra space or constant space
also we can implement the same.
else :
# if more nodes in first list
if (next1.next) :
next1 = next1.next
curr1 = curr1.next
return h1
Q.6 Write all steps to sort the elements using Quick sort algorithm.
44, 11, 77, 90, 40
Above solution is based on Lomuto’s Partitioning where in pivot separates array in two parts, left all
small and right all bigger or vice versa.
There exists another partitioning algorithm which actually takes very less comparisons called Hoare’s,
you may give a try to that, where in two pointers move opposite to each other and partition happens
accordingly.
There exist many variations of Quick sort algorithm in terms of implementation and optimisations.
https://www.geeksforgeeks.org/quick-sort/?ref=lbp
https://www.geeksforgeeks.org/improvement-on-the-quick-sort-algorithm/?ref=lbp