Javatpoint Logo
Javatpoint Logo

DUTCH NATIONAL FLAG

Dutch National Flag (DNF) - It is a programming problem proposed by Edsger Dijkstra. The flag of the Netherlands consists of three colors: white, red, and blue. The task is to randomly arrange balls of white, red, and blue in such a way that balls of the same color are placed together. For DNF (Dutch National Flag), we sort an array of 0, 1, and 2's in linear time that does not consume any extra space. We have to keep in mind that this algorithm can be implemented only on an array that has three unique elements.

ALGORITHM -

  • Take three-pointers, namely - low, mid, high.
  • We use low and mid pointers at the start, and the high pointer will point at the end of the given array.

CASES :

  • If array [mid] =0, then swap array [mid] with array [low] and increment both pointers once.
  • If array [mid] = 1, then no swapping is required. Increment mid pointer once.
  • If array [mid] = 2, then we swap array [mid] with array [high] and decrement the high pointer once.

CODE -

(IN C LANGUAGE)

OUTPUT -

Array before executing the algorithm: 0 1 0 1 2 0 1 2

Array after executing the DNFS algorithm: 0 0 0 1 1 1 2 2

DUTCH NATIONAL FLAG

CODE -

(IN JAVA)

OUTPUT -

Array before executing the DNFS algorithm : 0 1 0 1 2 0 1 2

Array after executing the DNFS algorithm : 0 0 0 1 1 1 2 2

DUTCH NATIONAL FLAG

CODE -

(IN PYTHON)

OUTPUT -

Array before executing the algorithm: 0 1 0 1 1 2 0 1 2

Array after executing the DNFS algorithm: 0 0 0 1 1 1 1 2 2

DUTCH NATIONAL FLAG




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA