java - Sort an array of zeros, ones and twos -


i trying sort arrays in such way such zeros should come first , followed ones , followed twos.

input: { 0, 1, 2, 1, 2, 2, 0, 0, 1 };

output: { 0, 0, 0, 1, 1, 1, 2, 2, 2 };

below program doesn't work on above input:

public class seggregatezeroonetwos {      public static void main(string[] args) {         int[] = { 0, 1, 2, 1, 2, 2, 0, 0, 1 };         arrangenumbers(a);         system.out.println(arrays.tostring(a));     }      public static void arrangenumbers(int[] arr) {         int low = 0;         int high = arr.length - 1;         int = 0;         while (i < high) {             if (arr[i] < 1) {                 swap(arr, i, low);                 low++;                 i++;             } else if (arr[i] > 1) {                 swap(arr, i, high);                 high--;             } else {                 i++;             }         }     }      public static void swap(int[] arr, int i, int j) {         arr[i] = arr[i] ^ arr[j];         arr[j] = arr[i] ^ arr[j];         arr[i] = arr[i] ^ arr[j];     } } 

i getting output in can see, 0 coming in between 1 , 2. what's wrong code?

[0, 0, 1, 1, 1, 0, 2, 2, 2] 

the number @ high not checked yet.

    while (i <= high) { 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -