📖 Scenario: Imagine you have a list of colored balls represented by numbers: 0 for red, 1 for white, and 2 for blue. You want to sort them so that all reds come first, then whites, and then blues, just like the Dutch flag.
🎯 Goal: You will write a program that rearranges an array of 0s, 1s, and 2s so that all 0s come first, followed by all 1s, and then all 2s.
📋 What You'll Learn
Create an array called colors with the exact values: [2, 0, 2, 1, 1, 0]
Create a variable called low and set it to 0
Use a while loop with a variable mid starting at 0 and a variable high starting at the last index of colors
Inside the loop, rearrange the elements so that all 0s are before low, all 1s between low and high, and all 2s after high
Print the colors array after sorting
💡 Why This Matters
🌍 Real World
Sorting items into categories quickly and efficiently is useful in many applications like organizing data, color sorting in graphics, or grouping similar objects.
💼 Career
Understanding partitioning algorithms like Dutch National Flag is important for software engineers working on sorting, searching, and optimizing data processing.
Progress0 / 4 steps
1
Create the initial array
Create an array called colors with these exact values: [2, 0, 2, 1, 1, 0]
DSA Javascript
Hint
Use const colors = [2, 0, 2, 1, 1, 0]; to create the array.
2
Set up pointers for partitioning
Create variables called low and mid and set both to 0. Create a variable called high and set it to colors.length - 1
DSA Javascript
Hint
Use let low = 0;, let mid = 0;, and let high = colors.length - 1;.
3
Implement the Dutch National Flag partition logic
Use a while loop with the condition mid <= high. Inside the loop, check the value at colors[mid]. If it is 0, swap it with colors[low], then increment low and mid. If it is 1, just increment mid. If it is 2, swap it with colors[high] and decrement high.
DSA Javascript
Hint
Use a while loop and swap elements using destructuring assignment.
4
Print the sorted array
Write console.log(colors) to print the sorted colors array
DSA Javascript
Hint
Use console.log(colors) to print the array.
Practice
(1/5)
1. What is the main goal of the Dutch National Flag Three Way Partition algorithm?
easy
A. To find the maximum element in an array
B. To sort an array in ascending order using quicksort
C. To reverse the elements of an array
D. To rearrange an array so that all elements less than a pivot come first, then equal elements, then greater elements
Solution
Step 1: Understand the partition goal
The algorithm divides the array into three parts: less than pivot, equal to pivot, and greater than pivot.
Step 2: Identify the rearrangement
It rearranges elements so that all smaller come first, then equal, then larger, without full sorting.
Final Answer:
To rearrange an array so that all elements less than a pivot come first, then equal elements, then greater elements -> Option D
Quick Check:
Partitioning = Rearranging by pivot [OK]
Hint: Remember: three parts - less, equal, greater [OK]
Common Mistakes:
Confusing partition with full sorting
Thinking it only finds max or min
Assuming it reverses the array
2. Which of the following is the correct way to initialize pointers for the Dutch National Flag algorithm in JavaScript?
easy
A. let low = 0, mid = 0, high = arr.length - 1;
B. let low = arr.length - 1, mid = 0, high = 0;
C. let low = 0, mid = arr.length - 1, high = 0;
D. let low = 0, mid = 1, high = arr.length;
Solution
Step 1: Identify pointer roles
Low and mid start at 0, high at last index to cover full array.
Step 2: Match initialization to algorithm
Correct initialization is low=0, mid=0, high=arr.length-1 to scan and partition properly.
Final Answer:
let low = 0, mid = 0, high = arr.length - 1; -> Option A
Quick Check:
Pointers start at array ends and middle [OK]
Hint: Start low and mid at 0, high at last index [OK]
Common Mistakes:
Setting high to 0 instead of last index
Starting mid at wrong position
Using array length instead of length - 1
3. What will be the output of the following code snippet?