From 02f6f11c1ba99813aff6cde5d3fd486c4dd12630 Mon Sep 17 00:00:00 2001 From: Arnav-NK Date: Fri, 3 Oct 2025 07:55:23 +0530 Subject: [PATCH 1/2] added dutch national flag algorithm or dijkstra 3way partioning in sorts --- .../sorts/DijkstraThreeWayPartition.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java diff --git a/src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java b/src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java new file mode 100644 index 000000000000..d5a31d95185c --- /dev/null +++ b/src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java @@ -0,0 +1,39 @@ +package com.thealgorithms.sorts; +public class DijkstraThreeWayPartition { + + public static void threeWayPartition(int[] arr, int pivot) { + int low = 0, mid = 0; + int high = arr.length - 1; + //this will sort the array in o(n) time complexity + //also with no extra space i,e o(1) space complexity + while (mid <= high) { + if (arr[mid] < pivot) { + // Swap + int temp = arr[low]; + arr[low] = arr[mid]; + arr[mid] = temp; + + low++; + mid++; + } else if (arr[mid] > pivot) { + // Swap + int temp = arr[mid]; + arr[mid] = arr[high]; + arr[high] = temp; + + high--; + } else { + mid++; + } + } + } + + public static void main(String[] args) { + int[] arr = {2, 1, 2, 3, 2, 1, 3, 2}; + int pivot = 2; + threeWayPartition(arr, pivot); + for (int num : arr) { + System.out.print(num + " "); + } + } +} From a8410591028b05e8696ecaad4aa79e105333bc31 Mon Sep 17 00:00:00 2001 From: Arnav-NK Date: Fri, 3 Oct 2025 08:08:12 +0530 Subject: [PATCH 2/2] updated the wikipedia link --- .../java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java b/src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java index d5a31d95185c..bac08b94748a 100644 --- a/src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java +++ b/src/main/java/com/thealgorithms/sorts/DijkstraThreeWayPartition.java @@ -1,3 +1,5 @@ +//https://en.wikipedia.org/wiki/Dutch_national_flag_problem + package com.thealgorithms.sorts; public class DijkstraThreeWayPartition {