# 算法设计：将所有零移动到数组末尾详细代码实现

2021年3月12日14:35:53 发表评论 459 次浏览

## 本文概述

``````Input :  arr[] = {1, 2, 0, 4, 3, 0, 5, 0};
Output : arr[] = {1, 2, 4, 3, 5, 0, 0};

Input : arr[]  = {1, 2, 0, 0, 0, 3, 6};
Output : arr[] = {1, 2, 3, 6, 0, 0, 0};``````

## C ++

``````// A C++ program to move all zeroes at the end of array
#include <iostream>
using namespace std;

// Function which pushes all zeros to end of an array.
void pushZerosToEnd( int arr[], int n)
{
int count = 0;  // Count of non-zero elements

// Traverse the array. If element encountered is non-
// zero, then replace the element at index 'count'
// with this element
for ( int i = 0; i < n; i++)
if (arr[i] != 0)
arr[count++] = arr[i]; // here count is
// incremented

// Now all non-zero elements have been shifted to
// front and  'count' is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
arr[count++] = 0;
}

// Driver program to test above function
int main()
{
int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = sizeof (arr) / sizeof (arr);
pushZerosToEnd(arr, n);
cout << "Array after pushing all zeros to end of array :\n" ;
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
return 0;
}``````

## Java

``````/* Java program to push zeroes to back of array */
import java.io.*;

class PushZero
{
// Function which pushes all zeros to end of an array.
static void pushZerosToEnd( int arr[], int n)
{
int count = 0 ;  // Count of non-zero elements

// Traverse the array. If element encountered is
// non-zero, then replace the element at index 'count'
// with this element
for ( int i = 0 ; i < n; i++)
if (arr[i] != 0 )
arr[count++] = arr[i]; // here count is
// incremented

// Now all non-zero elements have been shifted to
// front and 'count' is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
arr[count++] = 0 ;
}

/*Driver function to check for above functions*/
public static void main (String[] args)
{
int arr[] = { 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 };
int n = arr.length;
pushZerosToEnd(arr, n);
System.out.println( "Array after pushing zeros to the back: " );
for ( int i= 0 ; i<n; i++)
System.out.print(arr[i]+ " " );
}
}
/* This code is contributed by Devesh Agrawal */``````

## Python3

``````# Python3 code to move all zeroes
# at the end of array

# Function which pushes all
# zeros to end of an array.
def pushZerosToEnd(arr, n):
count = 0 # Count of non-zero elements

# Traverse the array. If element
# encountered is non-zero, then
# replace the element at index
# 'count' with this element
for i in range (n):
if arr[i] ! = 0 :

# here count is incremented
arr[count] = arr[i]
count + = 1

# Now all non-zero elements have been
# shifted to front and 'count' is set
# as index of first 0. Make all
# elements 0 from count to end.
while count < n:
arr[count] = 0
count + = 1

# Driver code
arr = [ 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 ]
n = len (arr)
pushZerosToEnd(arr, n)
print ( "Array after pushing all zeros to end of array:" )
print (arr)

# This code is contributed by "Abhishek Sharma 44"``````

## C#

``````/* C# program to push zeroes to back of array */
using System;

class PushZero
{
// Function which pushes all zeros
// to end of an array.
static void pushZerosToEnd( int []arr, int n)
{
// Count of non-zero elements
int count = 0;

// Traverse the array. If element encountered is
// non-zero, then replace the element
// at index â..countâ.. with this element
for ( int i = 0; i < n; i++)
if (arr[i] != 0)

// here count is incremented
arr[count++] = arr[i];

// Now all non-zero elements have been shifted to
// front and â..countâ.. is set as index of first 0.
// Make all elements 0 from count to end.
while (count < n)
arr[count++] = 0;
}

// Driver function
public static void Main ()
{
int []arr = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = arr.Length;
pushZerosToEnd(arr, n);
Console.WriteLine( "Array after pushing all zeros to the back: " );
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
/* This code is contributed by Anant Agrawal */``````

## 的PHP

``````<?php
// A PHP program to move all
// zeroes at the end of array

// Function which pushes all
// zeros to end of an array.
function pushZerosToEnd(& \$arr , \$n )
{
// Count of non-zero elements
\$count = 0;

// Traverse the array. If
// element encountered is
// non-zero, then replace
// the element at index
// 'count' with this element
for ( \$i = 0; \$i < \$n ; \$i ++)
if ( \$arr [ \$i ] != 0)
// here count is incremented
\$arr [ \$count ++] = \$arr [ \$i ];

// Now all non-zero elements
// have been shifted to front
// and 'count' is set as index
// of first 0. Make all elements
// 0 from count to end.
while ( \$count < \$n )
\$arr [ \$count ++] = 0;
}

// Driver Code
\$arr = array (1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9);
\$n = sizeof( \$arr );
pushZerosToEnd( \$arr , \$n );
echo "Array after pushing all " .
"zeros to end of array :\n" ;

for ( \$i = 0; \$i < \$n ; \$i ++)
echo \$arr [ \$i ] . " " ;

// This code is contributed
// by ChitraNayal
?>``````

``````Array after pushing all zeros to end of array :
1 9 8 4 2 7 6 9 0 0 0 0`````` 