Dutch National Flag Problem in Java  Java Program to Short an Array of 0's, 1's, and 2'sDutch National Flag (DNF) problem is one of the most popular programming problems proposed by the famous Dutch computer scientist Edsger Dijkstra. As its name suggest, it is based on the flag of Netherlands that consists tri colors i.e. red, white, and blue. The task is to randomly arrange the bolls of red, white, and blue in such a way that balls of the same color are placed together. We will solve the above problem using an array. Instead of using the colors, here we will use 0's, 1's, and 2's that represents red, white, and blue colors, respectively. In order to match the color of the bolls we will sort the array. For example, consider the following array. Input: {0, 1, 2, 2, 1, 0, 0, 2, 0, 1, 1, 0} Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2} So, Dutch National flag problem is the same as short an array of 0's, 1's, and 2's. In this section, we will create a Java program for the same. Solution to the ProblemThere are many solutions are available that may vary in performance and characteristics. Naive ApproachThe simple solution is to perform the counting sort over the array. In this sorting, we count the frequency of the elements (0, 1, and 2), after that put them in the correct order. The disadvantage of the approach is that it traverses two times over the array, one for sorting, second for putting elements in the correct order. In order to overcome the above shortcoming, we reorganize the array in such a way that solves the problem in single traversal. It uses an alternative lineartime partition routine (the same as 3way partition) that separates the values into the following three groups.
AlgorithmLet's understand it through an example. ExampleIn the following example, we have used three variables low, mid, and high. The low and mid pointer point at start and the high pointer point at the end of the array. There are three cases:
Consider the following array. Initially low and mid points the start of the array i.e. 0 and the high points end of the array i.e. 2. Step 1: Check if array[mid]=0, swap array[mid] with array[low] and increment both the pointers by 1. Step 2: Check if array[mid]=1, no swapping is required. Increment the mid pointer by 1. Step 3: Check if array[mid]=0, swap array[mid] with array[low] and increment both the pointers by 1. Step 4: Check if array[mid]=2, swap array[mid] with array[high] and decrement the high pointer by 1. Step 5: Check if array[mid]=1, no swapping required increments the pointers by 1. Step 6: Check if array[mid]=0, swap array[mid] with array[low] and increment both the pointers by 1. Step 7: Check if array[mid]=2, swap array[mid] with array[high] and decrement the high pointer by 1. We overserve that the array is sorted. Let's implement the above approach in Java programs with different logics. Dutch National Flag Problem Java ProgramDutchNationalFlag.java Output: [0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2] Let's see another logic for the same. DutchFlagProblem.java Output: Array before Sorting: 0 0 1 2 0 1 2 Array after sorting: 0 0 0 1 1 2 2 The above program shorts the array in linear time without using extra space. The array is traversed only once. Hence, the time complexity of the is O(n), where n is the size of the array.
Next TopicJava Calculate Average of List
