How to Sort an Array of 0s, 1s, and 2s in Java

Sort an array of 0s, 1s, and 2s in Java

Examples

Input: {0, 1, 2, 0, 1, 2}Output: {0, 0, 1, 1, 2, 2}Input: {0, 1, 1, 0, 1, 2, 1, 2, 0, 0, 0, 1}Output: {0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2}

Index

The Brute Force Approach

Algorithm

Psuedo Code

for(i=0 -> N){for(j=i+1 -> N){if(A[i] > A[j]){Swap(A[i], A[j])}}}Print A

Output

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ateev Duggal

Ateev Duggal

I am a front-end developer from India with over one year of experience in freelancing with skills like Git, HTML5, CSS3, Bootstrap 4&5, and React.