push all zeros to beginning of array code example
Example: Move all zeros present in an array to the end
def swap(A, i, j):
temp = A[i]
A[i] = A[j]
A[j] = temp
# Function to move all zeros present in a list to the end
def partition(A):
j = 0
# each time we encounter a non-zero, `j` is incremented, and
# the element is placed before the pivot
for i in range(len(A)):
if A[i]: # pivot is 0
swap(A, i, j)
j = j + 1