Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to sort 0,1,2 in an Array (Dutch National Flag) without extra space using C#?
The Dutch National Flag problem is a classic algorithm challenge that sorts an array containing only 0s, 1s, and 2s in a single pass without using extra space. This problem was originally described by Dutch computer scientist Edsger Dijkstra and is also known as the Three-Way Partitioning algorithm.
The algorithm uses three pointers to partition the array into three regions: all 0s on the left, all 1s in the middle, and all 2s on the right.
Algorithm Overview
The algorithm maintains three pointers −
low− Points to the boundary of the 0s regionmid− Current element being examinedhigh− Points to the boundary of the 2s region
Syntax
Following is the basic structure of the Dutch National Flag algorithm −
while (midHow It Works
The algorithm processes each element based on its value −
If
arr[mid] == 0− Swap witharr[low]and increment bothlowandmidIf
arr[mid] == 1− Element is in correct position, just incrementmidIf
arr[mid] == 2− Swap witharr[high]and decrementhigh(don't incrementmid)
Example
using System;
namespace ConsoleApplication {
public class Arrays {
private void Swap(int[] arr, int pos1, int pos2) {
int temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
public void DutchNationalFlag(int[] arr) {
int low = 0;
int mid = 0;
int high = arr.Length - 1;
while (mid
The output of the above code is −
Original array:
2 1 1 0 1 2 1 2 0 0 1
Sorted array:
0 0 0 1 1 1 1 1 2 2 2
Step-by-Step Example
using System;
public class StepByStepDemo {
private void Swap(int[] arr, int pos1, int pos2) {
int temp = arr[pos1];
arr[pos1] = arr[pos2];
arr[pos2] = temp;
}
public void DutchNationalFlagWithSteps(int[] arr) {
int low = 0, mid = 0, high = arr.Length - 1;
int step = 1;
Console.WriteLine("Initial: [{0}]", string.Join(", ", arr));
Console.WriteLine("low={0}, mid={1}, high={2}
", low, mid, high);
while (mid ", low, mid, high);
}
}
public static void Main() {
StepByStepDemo demo = new StepByStepDemo();
int[] arr = { 2, 0, 1, 2, 1 };
demo.DutchNationalFlagWithSteps(arr);
}
}
The output of the above code is −
Initial: [2, 0, 1, 2, 1]
low=0, mid=0, high=4
Step 1: arr[mid=0] = 2
Swap arr[4] with arr[0]
Result: [1, 0, 1, 2, 2]
low=0, mid=0, high=3
Step 2: arr[mid=0] = 1
arr[0] = 1, just increment mid
Result: [1, 0, 1, 2, 2]
low=0, mid=1, high=3
Step 3: arr[mid=1] = 0
Swap arr[0] with arr[1]
Result: [0, 1, 1, 2, 2]
low=1, mid=2, high=3
Step 4: arr[mid=2] = 1
arr[2] = 1, just increment mid
Result: [0, 1, 1, 2, 2]
low=1, mid=3, high=3
Step 5: arr[mid=3] = 2
Swap arr[3] with arr[3]
Result: [0, 1, 1, 2, 2]
low=1, mid=3, high=2
Time and Space Complexity
| Complexity | Value | Explanation |
|---|---|---|
| Time Complexity | O(n) | Each element is visited at most once |
| Space Complexity | O(1) | Only uses a constant amount of extra space |
Conclusion
The Dutch National Flag algorithm efficiently sorts an array of 0s, 1s, and 2s in linear time using constant space. This three-pointer approach is optimal for partitioning problems and demonstrates the power of in-place algorithms for specific constraints.
