Java Program to Shift the 0’s in an Array to the End – Sanfoundry

This is the Java Program to Shift the 0’s in an Array to the End.

Problem Description

Given an array of integers, shift all the zeroes present in it to the end.
Example:

Array = [1 0 2 3 0 4]

Output
Array = [1 2 3 4 0 0]

Problem Solution

Traverse the array from beginning to end, and whenever a nonzero is encountered, move it to the position of the first element having a zero value, in the array.

Program/Source Code

Here is the source code of the Java Program to Shift the 0’s in an Array to the End. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

  1.  
  2. //Java Program to Shift the 0's in an Array to the End
  3.  
  4. import java.io.BufferedReader;
  5. import java.io.InputStreamReader;
  6.  
  7. public class ShiftZeroesEnd {
  8.     // Function to shift the zeroes in the end
  9.     static void inTheEnd(int[] array){
  10.         int startIndex = 0;
  11.         int i,j,temp;
  12.         for(i=0;i<array.length;i++){
  13.             if(array[i] != 0){
  14.                 for(j=i; j>startIndex;j--){
  15.                     temp = array[j];
  16.                     array[j] = array[j-1];
  17.                     array[j-1] = temp;
  18.                 }
  19.                 startIndex++;
  20.             }
  21.         }
  22.     }
  23.     // Function to read user input and display the output
  24.     public static void main(String[] args){
  25.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  26.         int size;
  27.         System.out.println("Enter the size of the array");
  28.         try {
  29.             size = Integer.parseInt(br.readLine());
  30.         } catch (Exception e) {
  31.             System.out.println("Invalid Input");
  32.             return;
  33.         }
  34.         int[] array = new int[size];
  35.         System.out.println("Enter array elements");
  36.         int i;
  37.         for (i = 0; i < array.length; i++) {
  38.             try {
  39.                 array[i] = Integer.parseInt(br.readLine());
  40.             } catch (Exception e) {
  41.                 System.out.println("An error occurred");
  42.                 return;
  43.             }
  44.         }
  45.         inTheEnd(array);
  46.         System.out.println("The array after shifting the zeroes in the end is");
  47.         for(i=0; i<array.length; i++){
  48.             System.out.print(array[i] + " ");
  49.         }
  50.     }
  51. }

Program Explanation

1. In function inTheEnd(), the loop for(i=0;i<array.length;i++) is used to iterate through the array.
2. The statement if(array[i]!=0) looks for non-zero elements.
3. Any non-zero element is shifted to the position of the first zero element using the nested loop for(j=i;j>startIndex;j–).
4. Consequently, all the zeroes get shifted to the end of the array.

Time Complexity: O(n2) where n is the number of elements in the array.

Runtime Test Cases

 
Case 1 (Simple test Case):
 
Enter the size of the array
6
Enter array elements
1
0
2
3
0
4
The array after shifting the zeroes in the end is
1 2 3 4 0 0
 
Case 2 (Simple Test Case - another example):
 
Enter the size of the array
6
Enter array elements
6
7
8
9
0
0
The array after shifting the zeroes in the end is
6 7 8 9 0 0

Sanfoundry Global Education & Learning Series – Java Programs.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Leave a Comment

Your email address will not be published. Required fields are marked *