Horje
Dutch National Flag Problem in Python

The Dutch National Flag problem is a popular algorithmic problem proposed by Edsger Dijkstra. The problem is to sort an array consisting of three distinct elements (or “colors”) in a single pass through the array. The three elements could be anything, but for simplicity, we’ll use 0, 1, and 2.

The goal is to arrange the array such that all 0s come first, followed by all 1s, and then all 2s.

Examples:

Input: arr[] = {0, 1, 2, 0, 1, 2}
Output: {0, 0, 1, 1, 2, 2}

Input: arr[] = {2, 1, 0, 0, 1}
Output: {0, 0, 1, 1, 2}

Dutch National Algorithm:

We can solve this problem using the three-pointer technique, also known as the Dutch National Flag algorithm. The idea is to use three pointers (low, mid, high) to maintain the position of 0s, 1s, and 2s. low points to the next position where 0 can be inserted, mid points to the current number and high points to the next position where 2 can be inserted. Depending on the value at the mid pointer, swap elements to place them in the correct partition:

  • If the value is 0, swap it with the value at low and move both low and mid pointers forward.
  • If the value is 1, just move the mid pointer forward.
  • If the value is 2, swap it with the value at high and move the high pointer backward.

Step-by-step algorithm:

  • Initialization:
    • Define three pointers: low, mid, and high.
      • low – Points to the next position where a 0 should be placed.
      • mid – Points to the current element being processed.
      • high – Points to the next position where a 2 should be placed.
  • Traverse the Array:
    • Iterate through the array with the mid pointer.
    • Depending on the value at arr[mid], perform the following actions:
      • If arr[mid] is 0, swap it with arr[low], and move both low and mid pointers forward.
      • If arr[mid] is 1, just move the mid pointer forward.
      • If arr[mid] is 2, swap it with arr[high], and move the high pointer backward.
  • Termination:
    • The process continues until the mid pointer exceeds the high pointer.

Step-by-Step Execution:

Consider the array [2, 0, 2, 1, 1, 0]:

  1. Initialization:
    • low = 0, mid = 0, high = 5
    • Array: [2, 0, 2, 1, 1, 0]
  2. Iteration 1:
    • arr[mid] = 2 (swap arr[mid] with arr[high])
    • low = 0, mid = 0, high = 4
    • Array: [0, 0, 2, 1, 1, 2]
  3. Iteration 2:
    • arr[mid] = 0 (swap arr[mid] with arr[low])
    • low = 1, mid = 1, high = 4
    • Array: [0, 0, 2, 1, 1, 2]
  4. Iteration 3:
    • arr[mid] = 0 (swap arr[mid] with arr[low])
    • low = 2, mid = 2, high = 4
    • Array: [0, 0, 2, 1, 1, 2]
  5. Iteration 4:
    • arr[mid] = 2 (swap arr[mid] with arr[high])
    • low = 2, mid = 2, high = 3
    • Array: [0, 0, 1, 1, 2, 2]
  6. Iteration 5:
    • arr[mid] = 1 (move mid forward)
    • low = 2, mid = 3, high = 3
    • Array: [0, 0, 1, 1, 2, 2]
  7. Iteration 6:
    • arr[mid] = 1 (move mid forward)
    • low = 2, mid = 4, high = 3
    • Array: [0, 0, 1, 1, 2, 2]

Below is the implementation of Dutch National Flag Algorithm in Python:

Python
# Python program to sort an array with
# 0, 1 and 2 in a single pass

# Function to sort array


def sort012(a, arr_size):
    lo = 0
    hi = arr_size - 1
    mid = 0
    # Iterate till all the elements
    # are sorted
    while mid <= hi:
        # If the element is 0
        if a[mid] == 0:
            a[lo], a[mid] = a[mid], a[lo]
            lo = lo + 1
            mid = mid + 1
        # If the element is 1
        elif a[mid] == 1:
            mid = mid + 1
        # If the element is 2
        else:
            a[mid], a[hi] = a[hi], a[mid]
            hi = hi - 1
    return a

# Function to print array


def printArray(a):
    for k in a:
        print(k, end=' ')


# Driver Program
arr = [0, 1, 2, 0, 1, 2]
arr_size = len(arr)
arr = sort012(arr, arr_size)
printArray(arr)

Output
0 0 1 1 2 2 

Complexity Analysis:

  • Time Complexity: O(n), where n is the number of elements in the array. Each element is processed at most once.
  • Space Complexity: O(1), as the algorithm sorts the array in place and does not require additional storage proportional to the input size.



Reffered: https://www.geeksforgeeks.org


Arrays

Related
Assign Campus Bikes to Workers II Assign Campus Bikes to Workers II
Assign Campus Bikes to Workers Assign Campus Bikes to Workers
Find the Largest Values From Labels Find the Largest Values From Labels
Find the Sum of Mutated Array Closest to Target Find the Sum of Mutated Array Closest to Target
Reduce Array Elements by Single-Digit Origin Reduce Array Elements by Single-Digit Origin

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
14