...

/

Solution: Dutch National Flag Problem

Solution: Dutch National Flag Problem

In this review lesson, we will give a detailed analysis of the solutions for the Dutch National Problem.

Solution #1: brute force

def dutch_national_flag(lst):
"""
This function uses selection_sort approach to solve Dutch National Flag Problem
:param lst: A list of integers
:return: A list of solved Dutch National Flag Problem
"""
size = len(lst)
index = 0
for i in range(size):
index = find_min(lst, i, size)
lst[i], lst[index] = lst[index], lst[i]
return lst
def find_min(lst, start, end):
"""
Finds the minimum value index in a list in specific range
:param lst: A list of integers
:param start: Starting index
:param end: Ending index
:return: Minimum value index
"""
min = lst[start]
index = start
for x in range(start, end):
if lst[x] < min:
min = lst[x]
index = x
return index
# Driver to test above code
if __name__ == '__main__':
lst = [2, 0, 0, 1, 2, 1]
print(dutch_national_flag(lst))

Explanation

We have used simple selection sort to rearrange the list in an ascending order. This method is naive and does not cater to the fact that we have just three different digits for this problem.

Time complexity

The ...

Access this course and 1400+ top-rated courses and projects.