You are on page 1of 10

INDIAN INSTITUTE OF TECHNOLOGY ROORKEE

Data Structures and Algorithms (CSE-101)


Tutorial 3 Solutions Spring 23-24

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.

Q1. Code to Reverse an array in-place.

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.

Note: I have given here a little optimized


version, i.e. if array is already sorted, you
will find the time complexity to be O(n).

Swap functions interchanges the


elements at position j and j+1 in arr and
can be implemented like Q1 using temp
variable.
b. Insertion Sort
Code Time Complexity
Best Case: Ω(n) – Array Already Sorted
in desired manner.
Average and Worst Case: O(n2).
c. Merge Sort
Code Time Complexity
Start = 1 and end = N
Best, Average and Worst Case:
θ(nlogn)
Q3. We have a linked list, complete below functions –
a) def pairwise_swapping_ll(self):
6-7-8-3-2
o/p: 7-6-3-8-2

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.

def mergeUtil(h1, h2):

# if only one node in first list


# simply point its head to second list
if (h1.next == None) :
h1.next = h2
return h1

# Initialize current and next pointers of


# both lists
curr1 = h1
next1 = h1.next
curr2 = h2
next2 = h2.next

while (next1 != None and curr2 != None):

# if curr2 lies in between curr1 and next1


# then do curr1.curr2.next1
if ((curr2.data) >= (curr1.data) and
(curr2.data) <= (next1.data)) :
next2 = curr2.next
curr1.next = curr2
curr2.next = next1

# now let curr1 and curr2 to point


# to their immediate next pointers
curr1 = curr2
curr2 = next2

else :
# if more nodes in first list
if (next1.next) :
next1 = next1.next
curr1 = curr1.next

# else point the last node of first list


# to the remaining nodes of second list
else :
next1.next = curr2
return h1

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

Above blogs will give a lot of insights on quick sort

You might also like