Introduction
In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..
import java.util.*; import java.lang.*; public class Javaexcercise { public static void main (String[] args) { int nums[] = {1,1,0,1,0,1,1,1,0,0,0}; int nums_size = nums.length; int left = 0, right = nums_size - 1; System.out.println("Original Array : "+Arrays.toString(nums)); while (left < right) { while (nums[left] == 0 && left < right) left++; while (nums[right] == 1 && left < right) right--; if (left < right) { nums[left] = 0; nums[right] = 1; left++; right--; } } System.out.println("Array after segregation is : "+Arrays.toString(nums)); } }
int[] arr = {1, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0};
Arrays.sort(arr);
for (int a : arr)
{
System.out.print(a);
}