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 (mid <= high) {
if (arr[mid] == 0) {
Swap(arr, low, mid);
low++; mid++;
}
else if (arr[mid] == 2) {
Swap(arr, high, mid);
high--;
}
else { // arr[mid] == 1
mid++;
}
}
How 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 <= high) {
if (arr[mid] == 0) {
Swap(arr, low, mid);
low++;
mid++;
}
else if (arr[mid] == 2) {
Swap(arr, high, mid);
high--;
}
else {
mid++;
}
}
}
public void PrintArray(int[] arr) {
for (int i = 0; i < arr.Length; i++) {
Console.Write(arr[i] + " ");
}
Console.WriteLine();
}
}
class Program {
static void Main(string[] args) {
Arrays a = new Arrays();
int[] arr = { 2, 1, 1, 0, 1, 2, 1, 2, 0, 0, 1 };
Console.WriteLine("Original array:");
a.PrintArray(arr);
a.DutchNationalFlag(arr);
Console.WriteLine("Sorted array:");
a.PrintArray(arr);
}
}
}
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}<br>", low, mid, high);
while (mid <= high) {
Console.WriteLine("Step {0}: arr[mid={1}] = {2}", step++, mid, arr[mid]);
if (arr[mid] == 0) {
Console.WriteLine(" Swap arr[{0}] with arr[{1}]", low, mid);
Swap(arr, low, mid);
low++; mid++;
}
else if (arr[mid] == 2) {
Console.WriteLine(" Swap arr[{0}] with arr[{1}]", high, mid);
Swap(arr, high, mid);
high--;
}
else {
Console.WriteLine(" arr[{0}] = 1, just increment mid", mid);
mid++;
}
Console.WriteLine(" Result: [{0}]", string.Join(", ", arr));
Console.WriteLine(" low={0}, mid={1}, high={2}<br>", 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.
