Skip to content

Instantly share code, notes, and snippets.

@liuqinh2s
Created March 7, 2019 08:11
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 liuqinh2s/a7bcf1f8cf89ca4f62fc4ad41d9ccba5 to your computer and use it in GitHub Desktop.
Save liuqinh2s/a7bcf1f8cf89ca4f62fc4ad41d9ccba5 to your computer and use it in GitHub Desktop.
public class Solution {
public void partition(int[] array) {
if (array == null || array.length <= 1) {
return;
}
int index1 = 0;
int index2 = 0;
int index3 = 0;
for (int i = 0; i < array.length; i++) {
switch (array[i] % 4) {
case 0:
int temp = array[index1];
array[index1++] = array[i];
int temp2 = array[index2];
array[index2++] = temp;
int temp3 = array[index3];
array[index3++] = temp2;
array[i] = temp3;
break;
case 1:
temp2 = array[index2];
array[index2++] = array[i];
temp3 = array[index3];
array[index3++] = temp2;
array[i] = temp3;
break;
case 2:
temp3 = array[index3];
array[index3++] = array[i];
array[i] = temp3;
break;
case 3:
break;
}
}
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
solution.partition(array);
for(int i=0;i<array.length;i++){
System.out.println(array[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment