Skip to content

Instantly share code, notes, and snippets.

@shixiaoyu
Created March 15, 2020 21:39
Show Gist options
  • Save shixiaoyu/780b4942f7783c97cc0f04de1a4339b9 to your computer and use it in GitHub Desktop.
Save shixiaoyu/780b4942f7783c97cc0f04de1a4339b9 to your computer and use it in GitHub Desktop.
public void sortColorsCountingSort(int[] A) {
if (A == null || A.length == 0) return;
int[] buckets = new int[3];
for (int i = 0; i < A.length; i++) {
buckets[A[i]] += 1;
}
int index = 0;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < buckets[i]; j++) {
A[index] = i;
index++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment