0% found this document useful (0 votes)
2 views7 pages

Dutch National flag Algorithm

The Dutch National Flag Algorithm, developed by Edsger W. Dijkstra, sorts an array of three unique elements into distinct groups using efficient in-place sorting techniques in C. The algorithm operates with linear time complexity O(n) and constant space complexity O(1), making it suitable for problems involving three discrete categories. It is particularly useful for sorting binary arrays and has applications in quicksort and memory optimization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Dutch National flag Algorithm

The Dutch National Flag Algorithm, developed by Edsger W. Dijkstra, sorts an array of three unique elements into distinct groups using efficient in-place sorting techniques in C. The algorithm operates with linear time complexity O(n) and constant space complexity O(1), making it suitable for problems involving three discrete categories. It is particularly useful for sorting binary arrays and has applications in quicksort and memory optimization.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Dutch National Flag Algorithm in C

Introduction

The Dutch National Flag Algorithm, conceptualized by Edsger W. Dijkstra, is a computer science algorithm

that aims to sort an array of three unique elements into three distinct groups. The algorithm derives its name

from the flag of the Netherlands, which features three colors: red, white, and blue. It is commonly used to

solve problems that involve partitioning arrays, especially in scenarios where the input consists of three

discrete categories.

In the context of computer science and particularly C programming, the Dutch National Flag Algorithm is

crucial in understanding in-place sorting mechanisms, efficient array manipulations, and condition-based

element segregation. It is extensively applied in problems involving sorting binary arrays, segregating 0s, 1s,

and 2s, and quicksort algorithms.

The core principle behind the Dutch National Flag Algorithm is partitioning: placing elements of similar types

together while ensuring the process is efficient in terms of time and space complexity. In C language, this is

implemented using index manipulation and conditional logic, ensuring constant space and linear time

complexity.

Problem Statement

Given an array containing only 0s, 1s, and 2s, write a function in C to sort the array in a single traversal such

that all 0s come first, followed by 1s and then 2s.


Dutch National Flag Algorithm in C

Input: An array of integers, e.g., arr[] = {0, 1, 2, 0, 1, 2}

Output: The array should be sorted as arr[] = {0, 0, 1, 1, 2, 2}

Objectives

1. To understand and implement the Dutch National Flag Algorithm in C.

2. To efficiently sort an array with three distinct values.

3. To learn in-place sorting with constant space complexity.

4. To enhance problem-solving skills related to array manipulation.

Algorithm

1. Initialize three pointers: low = 0, mid = 0, high = n - 1

2. Iterate through the array while mid <= high:

a. If arr[mid] == 0, swap arr[low] and arr[mid]; increment low and mid.

b. If arr[mid] == 1, increment mid.

c. If arr[mid] == 2, swap arr[mid] and arr[high]; decrement high.

3. Continue the loop until mid > high.

This ensures that all 0s are at the beginning, followed by 1s, and then 2s.

C Program
Dutch National Flag Algorithm in C

#include <stdio.h>

void dutchNationalFlag(int arr[], int n) {

int low = 0, mid = 0, high = n - 1;

while (mid <= high) {

switch (arr[mid]) {

case 0:

int temp = arr[low];

arr[low] = arr[mid];

arr[mid] = temp;

low++;

mid++;

break;

case 1:

mid++;

break;

case 2:

int temp = arr[mid];

arr[mid] = arr[high];

arr[high] = temp;

high--;
Dutch National Flag Algorithm in C

break;

void printArray(int arr[], int n) {

for (int i = 0; i < n; i++)

printf("%d ", arr[i]);

printf("\n");

int main() {

int arr[] = {2, 0, 2, 1, 1, 0};

int n = sizeof(arr) / sizeof(arr[0]);

dutchNationalFlag(arr, n);

printArray(arr, n);

return 0;

Expected Output

001122
Dutch National Flag Algorithm in C

Applications

1. Used in sorting arrays with three types of elements efficiently.

2. Helps in implementing variations of the quicksort algorithm.

3. Useful in low-level memory optimization problems.

4. Helps in classifying items with three discrete values (e.g., traffic signals, categories).

Advantages

1. Linear time complexity: O(n).

2. Constant space complexity: O(1).

3. Simple and intuitive implementation.

4. Effective in scenarios where only three unique elements are involved.

Disadvantages

1. Not suitable for sorting arrays with more than three unique values.

2. Requires multiple conditional checks which may affect performance in highly optimized systems.

3. Not stable; the relative order of equal elements is not preserved.

Conclusion
Dutch National Flag Algorithm in C

The Dutch National Flag Algorithm is a classic example of efficient in-place sorting. Understanding and

implementing this algorithm in C helps in mastering array manipulations and optimizing solutions with minimal

resource usage. It remains a fundamental algorithm with significant theoretical and practical implications in

computer science.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy