Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.