feat: Remove duplicate implementation of Dutch National Flag Sort Algorithm (#5541)

* feat: Remove duplicate implementation of Dutch National Flag Sort Algorithm

* feat: update DIRECTORY.md

---------

Co-authored-by: sailok.chinta <sailok.chinta@kotak.com>
This commit is contained in:
Sailok Chinta
2024-10-03 16:21:34 +05:30
committed by GitHub
parent 7b934af257
commit 48a298028d
2 changed files with 0 additions and 64 deletions

View File

@ -399,7 +399,6 @@
* [PalindromePrime](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/PalindromePrime.java)
* [PalindromeSinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/PalindromeSinglyLinkedList.java)
* [RangeInSortedArray](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/RangeInSortedArray.java)
* [Sort012D](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/Sort012D.java)
* [Sparsity](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/Sparsity.java)
* [ThreeSumProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/ThreeSumProblem.java)
* [TwoSumProblem](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/misc/TwoSumProblem.java)

View File

@ -1,63 +0,0 @@
package com.thealgorithms.misc;
import java.util.Scanner;
/**
* The array is divided into four sections: a[1..Lo-1] zeroes a[Lo..Mid-1] ones
* a[Mid..Hi] unknown a[Hi+1..N] twos 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.
* For more information on the Dutch national flag algorithm refer
* https://en.wikipedia.org/wiki/Dutch_national_flag_problem
*/
public final class Sort012D {
private Sort012D() {
}
public static void main(String[] args) {
Scanner np = new Scanner(System.in);
int n = np.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = np.nextInt();
}
sort012(a);
np.close();
}
public static void sort012(int[] a) {
int l = 0;
int h = a.length - 1;
int mid = 0;
int temp;
while (mid <= h) {
switch (a[mid]) {
case 0:
temp = a[l];
a[l] = a[mid];
a[mid] = temp;
l++;
mid++;
break;
case 1:
mid++;
break;
case 2:
temp = a[mid];
a[mid] = a[h];
a[h] = temp;
h--;
break;
default:
throw new IllegalArgumentException("Unexpected value: " + a[mid]);
}
}
System.out.println("the Sorted array is ");
for (int i = 0; i < a.length; i++) {
System.out.print(+a[i] + " ");
}
}
}