Skip to content

Instantly share code, notes, and snippets.

@OBrutus
Last active November 21, 2023 08:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OBrutus/0de637dc1ae2e46e8a9eb0dab6fdbf5a to your computer and use it in GitHub Desktop.
Save OBrutus/0de637dc1ae2e46e8a9eb0dab6fdbf5a to your computer and use it in GitHub Desktop.
Sort arrays of zero's, one's and two's | https://dev.to/obrutus/sort-arrays-of-zeros-ones-and-twos-348i
import java.util.Arrays;
/**
* Approach1
* Simple sort
* Simplest but, no one want's
*/
public class Approach1 {
public void sort012(int[] a) {
if (a == null || a.length == 0) {
throw new IllegalArgumentException("Array is null or empty");
}
Arrays.sort(a);
}
}
/**
* Approach2
* hashing method
*/
public class Approach2 {
void sort012(int[] array) {
if (array == null || array.length == 0) {
throw new IllegalArgumentException("Array is null or empty");
}
/**
* The countTable will store count of 3 number's
* As, these 3 numbers are the content within the array
* ie,
* countTable[0] -> count of zero within the source array
* countTable[1] -> for 1 and so for countTable[2]
*/
int[] countTable = new int[3];
for (int number : array) {
countTable[number] += 1;
}
for (int i = 0; i < array.length; i++) {
// here we need to look for 3 content
// ie, 0 then 1 and then 2
// better to use for loop than writing 3 different
// if statements and having same logic within it.
for (int contnet = 0; contnet < 3; contnet++) {
if (countTable[contnet] >= 1) {
// we need to put zero here in array
array[i] = contnet;
// now the content has been used
// we need to decrement the count
// from the count table
countTable[contnet] -= 1;
// break the loop so we can get a next
// value for the i'th position within an array
break;
}
}
}
}
}
import java.util.Arrays;
import java.util.Collections;
/**
* Approach3
*
* Pointers and Insertion Solution
*/
public class Approach3 {
void sort012(int[] a) {
if (a == null || a.length == 0) {
throw new IllegalArgumentException("Array is null or empty");
}
// take 3 pointers
// where we can insert the elemts
// we can say this as
// low, mid and high
// for 0 1 and 2
int low = 0;
int mid = 0;
// as 2 can be always appended to the end (being highest)
int high = a.length - 1;
/***
* Life would be super easy if we had
* only 0 and 2,
* just insert from left in case of 0
* and just insert from right in case of 2
*
* but with 1 we take mid into consideration
* and parse the mid from 0 (which is assigned initally)
* to the apt location till the point it is valid
* and does not bump into high
*/
while (mid <= high) {
switch (a[mid]) {
case 2:
// here we need to insert at the high position
// and decrement the pointer for any upcoming 2
// swap the high and mid as it's quite possible
// that while inserting to high original value within
// that place would be lost,
// which is what we do not want to occur
swap(a, high, mid);
high -= 1;
break;
case 1:
// it's alright to find 1 at place of mid
// it is what is meant to
// only thing to do is check for the next position
mid++;
break;
case 0:
// here if we see any 0 we insert into low place
// and increment the pointer for any upcoming 0
// at this point we found 0 at mid.
// give this from mid to low
// as low have all zero
// best way to exchange an element is by swap
swap(a, low, mid);
low++;
mid++;
break;
default:
throw new IllegalArgumentException("Corrupted array!");
}
}
}
void swap(int[] a, int l, int r) {
int t = a[l];
a[l] = a[r];
a[r] = t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment