Java Array Exercises: Sort a given binary array in linear times

Java Array: Exercise-56 with Solution

Write a Java program to sort a given binary array in linear times.

From Wikipedia,
Linear time: An algorithm is said to take linear time, or O(n) time, if its time complexity is O(n). Informally, this means that the running time increases at most linearly with the size of the input. More precisely, this means that there is a constant c such that the running time is at most cn for every input of size n. For example, a procedure that adds up all elements of a list requires time proportional to the length of the list, if the adding time is constant, or, at least, bounded by a constant.
Linear time is the best possible time complexity in situations where the algorithm has to sequentially read its entire input. Therefore, much research has been invested into discovering algorithms exhibiting linear time or, at least, nearly linear time. This research includes both software and hardware methods. There are several hardware technologies which exploit parallelism to provide this. An example is content-addressable memory. This concept of linear time is used in string matching algorithms such as the Boyer–Moore algorithm and Ukkonen's algorithm.

Input :
b_nums[] = { 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 }
After sorting: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]

Sample Solution:

Java Code:

import java.util.Arrays;

class solution

	public static void sort_binary_nums(int[] b_nums)
		int k = 0;

		for (int i = 0; i < b_nums.length; i++)
			if (b_nums[i] == 0) {
				b_nums[k++] = 0;

		for (int i = k; i < b_nums.length; i++) {
			b_nums[k++] = 1;

	public static void main (String[] args)
		int b_nums[] = { 0, 1, 1, 0, 1, 1, 0, 1, 0, 0 };
        System.out.println("Original array: "+Arrays.toString(b_nums)); 
		System.out.println("After sorting: "+Arrays.toString(b_nums));

Sample Output:

Original array: [0, 1, 1, 0, 1, 1, 0, 1, 0, 0]
After sorting: [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]


Flowchart: Sort a given binary array in linear times

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to print all sub-arrays with 0 sum present in a given array of integers.
Next: Write a Java program to check if a sub-array is formed by consecutive integers from a given array of integers.

What is the difficulty level of this exercise?

Java: Tips of the Day

Different between parseInt() and valueOf() in java?

Well, the API for Integer.valueOf(String) does indeed say that the String is interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.

If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore:

Integer k = Integer.valueOf(Integer.parseInt("123"))

Now, if what you want is the object and not the primitive, then using valueOf(String) may be more attractive than making a new object out of parseInt(String) because the former is consistently present across Integer, Long, Double, etc.

Ref: https://bit.ly/3vRuIPY