Problem statement: Given an array of random 1s and 0s, sort the array in ascending order without using any built-in sort function and no two loops.

This is an interesting problem. Here since we cannot use two for loops we can't use algorithms like bubble sort. However the solution to this is simple and can be achieved with a single loop itself.

The logic is loop through the array and if a 0 is found then swap it with the first occurring 1.

class Main {
    static int[] arr = {0, 1, 0, 1, 0, 0, 1, 1};
    
    public static int indexOf(int elem, int[] arr) {
        int idx = -1;
        for (int i = 0; i < arr.length; i++) {
            if (elem == arr[i]) {
                idx = i;
                break;
            }
        }
        return idx;
    }
    
    public static void main(String[] args) {
        for (int i = 0; i < arr.length; i++) {
            int x = arr[i];
            if (x == 0 && i > 0) {
                int idx = indexOf(1, arr);
                if (idx > -1) {
                    arr[idx] = x;
                    arr[i] = 1;
                }
            }
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

This will give the sorted array 0 0 0 0 1 1 1 1.