I’ve been playing with a problem of moving all 0's to end
of Arrays in different interviews in various combinations. Sometimes I ask to move all 0 to front of array, sorting an array without any data structure and so on.
In this tutorial, we will go over simple example of moving all 0’s to end preserving an order of an Array. There are two approaches.
Approach-1)
QuickSort Partitioning logic. What is Pivot Point?
Pivot point
is an key element in Quick Sort Algorithm. It performs and partitioning the collection around the pivot point.- It arranges an Array elements larger than the pivot are before it, and elements larger than the pivot are after it.
- Continue through the loop to sort an array
Logic is very simple:
- Iterate through an Array.
- If array[i] is not equals to 0 then swap it with current index.
- If array[i] == 0, simply skip loop
- In our case
0 is a Pivot point
. - Each time we found 0, counter pivot will be incremented and element will be move before pivot point.
private static void approach1(int[] crunchifyData) { // Move 0 to end of array int j = 0; for (int i = 0; i < crunchifyData.length; i++) { if (crunchifyData[i] != 0) { int temp = crunchifyData[j]; crunchifyData[j] = crunchifyData[i]; crunchifyData[i] = temp; j++; } } log("\n\nApproach-1 Result: " + Arrays.toString(crunchifyData)); }
Approach-2)
- Create a new array with same size
- Iterate through an Array and skip adding 0
private static void approach2(int[] crunchifyData) { int[] num = new int[crunchifyData.length]; int j = 0; for (int i = 0; i < crunchifyData.length; i++) { if (crunchifyData[i] != 0) { num[i - j] = crunchifyData[i]; } else { j++; } } System.out.print("\n\nApproach-2 Result: " + Arrays.toString(num)); }
Here is a Complete Program:
CrunchifyMoveAll0ToEnd.java
package crunchify.com.java.tutorials; import java.util.Arrays; /** * @author Crunchify.com * Requirement: Move all 0's to end of array preserving order * Input array: 8 8 3 0 4 0 6 0 9 1 * Output array: 8 8 3 4 6 9 1 0 0 0 */ public class CrunchifyMoveAll0ToEnd { public static void main(String[] args) { int[] crunchifyData; crunchifyData = new int[]{8, 8, 3, 0, 4, 0, 6, 0, 9, 1}; log("Original array: " + Arrays.toString(crunchifyData)); if (crunchifyData == null || crunchifyData.length == 0) { log("Empty Array"); } approach1(crunchifyData); approach2(crunchifyData); } private static void approach1(int[] crunchifyData) { // Move 0 to end of array int j = 0; for (int i = 0; i < crunchifyData.length; i++) { if (crunchifyData[i] != 0) { int temp = crunchifyData[j]; crunchifyData[j] = crunchifyData[i]; crunchifyData[i] = temp; j++; } } log("\n\nApproach-1 Result: " + Arrays.toString(crunchifyData)); } // Create a new array with same size // Iterate through an Array and skip adding 0 private static void approach2(int[] crunchifyData) { int[] num = new int[crunchifyData.length]; int j = 0; for (int i = 0; i < crunchifyData.length; i++) { if (crunchifyData[i] != 0) { num[i - j] = crunchifyData[i]; } else { j++; } } System.out.print("\n\nApproach-2 Result: " + Arrays.toString(num)); } private static void log(String string) { System.out.print(string + " "); } }
Eclipse Console Output:
Original array: [8, 8, 3, 0, 4, 0, 6, 0, 9, 1] Approach-1 Result: [8, 8, 3, 4, 6, 9, 1, 0, 0, 0] Approach-2 Result: [8, 8, 3, 4, 6, 9, 1, 0, 0, 0] Process finished with exit code 0
Let me know if you know better way to solve this issue. I would love to hear your thoughts.