mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-07 09:45:04 +08:00
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:
@ -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)
|
||||
|
@ -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] + " ");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user