C program to move all zeroes to end of array

In this blog post, we learn how to write a C program to move all zeroes to end of array? So if given an array of random numbers, push all the zero’s of a given array to the end of the array. We will also see how to shift all zero’s of an array to the end of the array.

Example,

Input :  arr[] = {7, 2, 0, 4, 3, 0, 5, 0};

Output : arr[] = {7, 2, 4, 3, 5, 0, 0};

So let’s see the solution to the C program to move all zeroes to the end of the array. Suppose arr is a given integer array of size N (arr[N] ), the task is to write the C program to move all zeroes to the end of the array.

Brute force solutions

It is the simplest solution to shift all zero’s at the end. Let’s divide the solution into a few steps.

1. Create a variable ‘endOfNonZero ‘to track non-zero element.

2. Traverse the given array ‘arr’ from start to end.

3. For every non-zero element arr[i], put the element at  arr[i] to arr[endOfNonZero ] and increment ‘endOfNonZero ’.

4. After complete traversal, all non-zero elements have already pushed to the beginning of the array and endOfNonZero denote the end of the non zero elements.

5. Now fill zero from ‘endOfNonZero’ to the end of the array (n).

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


// Function to move all zeros present in the array to the end
void moveAllZeroesAtArrayEnd(int arr[], int n)
{
    // endOfNonZero stores index of next available position
    int endOfNonZero = 0;
    int i;

    for (i = 0; i < n; i++)
    {
        // if current element is non-zero, put the element at
        // next free position in the array
        if (arr[i] != 0)
        {
            arr[endOfNonZero++] = arr[i];
        }
    }

    // move all 0's to the end of the array
    for (i = endOfNonZero; i < n; i++)
    {
        arr[i] = 0;
    }
}


int main()
{
    int arr[] = {0,1,5,2,0,9,8,0,7};

    //get array size
    int arr_size = ARRAY_SIZE(arr);

    int i = 0;

    moveAllZeroesAtArrayEnd(arr, arr_size);

    for (i = 0; i < arr_size; i++)
    {
        printf("%d ",arr[i]);
    }

    return 0;
}



C program to move all zeroes to end of array

If you want to learn more about the C language, you can check this course, Free Trial Available.

Partitioning logic of QuickSort

In this solution, we need to traverse the loop only a single time. Let’s see the steps,

1. Create two array indexes i and j and initialize both variables with 0.

2. Run a for loop from i = 0 to n-1.

3. At every iteration for non-zero ith value ( A[i] != 0 ), swap A[j] and A[i] and increment j by 1.

 

#include <stdio.h>

//Calculate array size
#define ARRAY_SIZE(a)  sizeof(a)/sizeof(a[0])


//swap function not handle null pointer scenario
//not much safe to use
void swap(int *s1, int *s2)
{
    int temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}

// Function to move all zeros present in the array to the end
void moveAllZeroesAtArrayEnd(int arr[], int n)
{
    int i =0,j = 0;

    // each time we encounter a non-zero, j is incremented and
    // the element is placed before the pivot
    for (i = 0; i < n; i++)
    {
        if (arr[i] != 0)//non-zero
        {
            swap(&arr[i],&arr[j]);
            j++;
        }
    }
}



int main()
{
    int arr[] = {0,1,5,2,0,9,8,0,7};

    //get array size
    int arr_size = ARRAY_SIZE(arr);

    int i = 0;

    moveAllZeroesAtArrayEnd(arr, arr_size);

    for (i = 0; i < arr_size; i++)
    {
        printf("%d ",arr[i]);
    }

    return 0;
}



C program to move all zeroes to end of array