Answers for "Move all zeros present in an array to the end"

0

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
Posted by: Guest on February-02-2021

Code answers related to "Move all zeros present in an array to the end"

Python Answers by Framework

Browse Popular Code Answers by Language