DUTCH NATIONAL FLAGDutch 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 -
CASES :
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 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 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 Next TopicLongest Palindrome Subsequence |