我经历了两种和三种颜色的解决方案,但我无法获得四种颜色的解决方案。
请帮忙。
会rrbb????yyyggg
吗?我们将如何交换绿旗?
我尝试了下面的解决方案,但无法将最后一个黄色换成绿色。
public class count123 {
// Java program to sort an array of 0, 1 and 2,3
static void sort0123(int a[], int arr_size)
{
int lo = 0;
int hi = arr_size - 1;
int mid = 0,temp=0;
int h2=arr_size - 1;
while (mid <= hi)
{
switch (a[mid])
{
case 0:
{
temp = a[lo];
a[lo] = a[mid];
a[mid] = temp;
lo++;
mid++;
break;
}
case 1:
mid++;
break;
case 2:
{
temp = a[mid];
a[mid] = a[hi];
a[hi] = temp;
hi--;
h2=hi;
break;
}
case 3:{
temp = a[mid];
a[mid] = a[h2];
a[h2] = temp;
// h2--;
//hi=h2;
break;
}
}
}
}
/* Utility function to print array arr[] */
static void printArray(int arr[], int arr_size)
{
int i;
for (i = 0; i < arr_size; i++)
System.out.print(arr[i]+" ");
System.out.println("");
}
/*Driver function to check for above functions*/
public static void main (String[] args)
{
int arr[] = {0, 1, 0,1,2,2,0,3,3,0,0,1};
int arr_size = arr.length;
sort0123(arr, arr_size);
System.out.println("Array after seggregation ");
printArray(arr, arr_size);
}
}
/*This code is contributed by Devesh Agrawal*/