Move all zeroes to end of array

Here given code implementation process.

//C Program 
//Move all zeroes to end of array
#include <stdio.h>

//Swap the value of array elements
void swap(int arr[],int start,int end)
{
  int temp=arr[start];
  arr[start]=arr[end];
  arr[end]=temp;
}

void move_zero(int arr[],int size,int current)
{
  int front=0,back=size-1;

  while(front<back)
  {
    if(arr[back]==current)
    {
      back--;
    }
    else if(arr[front]==current)
    {
      swap(arr,front,back);

      front++;
      back--;
    }
    else
    {
      front++;
    }
  }
}
//Display array elements
void display(int arr[],int size)
{

  for (int i = 0; i < size; ++i)
  {
    printf("%3d",arr[i] );
  }
  printf("\n");
}
int main()
{
  //Define array elements
  int arr[] = { 0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0 };
  //Get the size of array
  int size=(sizeof(arr)/sizeof(arr[0]));
  printf("  Before Move  : \n");
  display(arr,size);
  move_zero(arr,size,0);
  printf("  After Move  : \n");
  display(arr,size);


  return 0;
}

Output

  Before Move  :
  0  1  4  7  2  2  1  2  0  0  6  0  1  1  0
  After Move  :
  1  1  4  7  2  2  1  2  1  6  0  0  0  0  0
/*
  C++ Program
  Move all zeroes to end of array
*/
#include<iostream>
using namespace std;

class MyArray {
  public:

    //Swap the value of array elements
    void swap(int arr[], int start, int last) {
      int temp = arr[start];
      arr[start] = arr[last];
      arr[last] = temp;
    }
  void move_zero(int arr[], int size, int current) {
    int front = 0, back = size - 1;
    while (front < back) {
      if (arr[back] == current) {
        back--;
      } else
      if (arr[front] == current) {
        this->swap(arr, front, back);
        front++;
        back--;
      } else {
        front++;
      }
    }
  }
  //Display array element values
  void dispay(int arr[], int size) {
    for (int i = 0; i < size; ++i) {
      cout << " " << arr[i];
    }
    cout << "\n";
  }
};
int main() {
  MyArray obj = MyArray();
  int arr[] = {
    0,
    1,
    4,
    7,
    2,
    2,
    1,
    2,
    0,
    0,
    6,
    0,
    1,
    1,
    0
  };
  //Get the size of array
  int size = sizeof(arr) / sizeof(arr[0]);
  cout << " Before Move : \n";
  obj.dispay(arr, size);
  //zero is move element
  obj.move_zero(arr, size, 0);
  cout << " After Move : \n";
  obj.dispay(arr, size);
  return 0;
}

Output

 Before Move :
 0 1 4 7 2 2 1 2 0 0 6 0 1 1 0
 After Move :
 1 1 4 7 2 2 1 2 1 6 0 0 0 0 0
/*
  Java Program
  Move all zeroes to end of array
*/

public class MyArray 
{
  
  //Swap the value of array elements
  public void swap(int []arr,int start,int last)
  {
    int temp=arr[start];
    arr[start]=arr[last];
    arr[last]=temp;
  }

  public void move_zero(int []arr,int size,int current)
  {
    int front=0,back=size-1;

    while(front<back)
    {
      if(arr[back]==current)
      {
        back--;
      }
      else if(arr[front]==current)
      {
        swap(arr,front,back);

        front++;
        back--;
      }
      else
      {
        front++;
      }
    }
  }
  //Display array element values
  public void dispay(int []arr,int size)
  {

    for (int i = 0; i < size; ++i)
    {
      System.out.print("  "+arr[i] );
    }
    System.out.print("\n");
  }
  public static void main(String[] args) {

    MyArray obj = new MyArray();
    //Define the value of array elements
    int []arr = { 0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0 };
    //Get the size of array
    int size = arr.length;
    System.out.print("  Before Move  : \n");
    obj.dispay(arr,size);
    //zero is move element
    obj.move_zero(arr,size,0);
    System.out.print("  After Move  : \n");
    obj.dispay(arr,size);
 
  }
}

Output

 Before Move :
 0 1 4 7 2 2 1 2 0 0 6 0 1 1 0
 After Move :
 1 1 4 7 2 2 1 2 1 6 0 0 0 0 0
<?php
/*
  Php Program
  Move all zeroes to end of array
*/
class MyArray {
  //Swap the value of array elements

  public  function swap(&$arr, $start, $last) {
    $temp = $arr[$start];
    $arr[$start] = $arr[$last];
    $arr[$last] = $temp;
  }
  public  function move_zero(&$arr, $size, $current) {
    $front = 0;
    $back = $size - 1;
    while ($front < $back) {
      if ($arr[$back] == $current) {
        $back--;
      } else
      if ($arr[$front] == $current) {
        $this->swap($arr, $front, $back);
        $front++;
        $back--;
      } else {
        $front++;
      }
    }
  }
  //Display array element values

  public  function dispay($arr, $size) {
    for ($i = 0; $i < $size; ++$i) {
      echo(" ". $arr[$i]);
    }
    echo("\n");
  }
}

function main() {
  $obj = new MyArray();
  //Define the value of array elements
  $arr = array(0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0);
  //Get the size of array
  $size = count($arr);
  echo(" Before Move : \n");
  $obj->dispay($arr, $size);
  //zero is move element

  $obj->move_zero($arr, $size, 0);
  echo(" After Move : \n");
  $obj->dispay($arr, $size);

}
main();

Output

 Before Move :
 0 1 4 7 2 2 1 2 0 0 6 0 1 1 0
 After Move :
 1 1 4 7 2 2 1 2 1 6 0 0 0 0 0
/*
  Node Js Program
  Move all zeroes to end of array
*/
class MyArray {
  //Swap the value of array elements
  swap(arr, start, last) {
    var temp = arr[start];
    arr[start] = arr[last];
    arr[last] = temp;
  }
  move_zero(arr, size, current) {
    var front = 0;
    var back = size - 1;
    while (front < back) {
      if (arr[back] == current) {
        back--;
      } else
      if (arr[front] == current) {
        this.swap(arr, front, back);
        front++;
        back--;
      } else {
        front++;
      }
    }
  }

  //Display array element values
  dispay(arr, size) {
    for (var i = 0; i < size; ++i) {
      process.stdout.write(" " + arr[i]);
    }

    process.stdout.write("\n");
  }
}

function main(args) {
  var obj = new MyArray();
  //Define the value of array elements
  var arr = [0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0];
  //Get the size of array
  var size = arr.length;
  process.stdout.write(" Before Move : \n");
  obj.dispay(arr, size);
  //zero is move element
  obj.move_zero(arr, size, 0);
  process.stdout.write(" After Move : \n");
  obj.dispay(arr, size);
}

main();

Output

 Before Move :
 0 1 4 7 2 2 1 2 0 0 6 0 1 1 0
 After Move :
 1 1 4 7 2 2 1 2 1 6 0 0 0 0 0
# Python 3 Program
# Move all zeroes to end of array
class MyArray :
  # Swap the value of array elements
  def swap(self, arr, start, last) :
    temp = arr[start]
    arr[start] = arr[last]
    arr[last] = temp
  
  def move_zero(self, arr, size, current) :
    front = 0
    back = size - 1
    while (front < back) :
      if (arr[back] == current) :
        back -= 1
      elif (arr[front] == current) :
        self.swap(arr, front, back)
        front += 1
        back -= 1
      else :
        front += 1
      
    
  
  # Display array element values
  def dispay(self, arr, size) :
    i = 0
    while (i < size) :
      print(" ", arr[i], end = "")
      i += 1
    
    print("\n", end = "")
  

def main() :
  obj = MyArray()
  # Define the value of array elements
  arr = [0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0]
  # Get the size of array
  size = len(arr)
  print(" Before Move : \n", end = "")
  obj.dispay(arr, size)
  # zero is move element
  obj.move_zero(arr, size, 0)
  print(" After Move : \n", end = "")
  obj.dispay(arr, size)


if __name__ == "__main__":
  main()

Output

 Before Move :
  0  1  4  7  2  2  1  2  0  0  6  0  1  1  0
 After Move :
  1  1  4  7  2  2  1  2  1  6  0  0  0  0  0
# Ruby Program
# Move all zeroes to end of array
class MyArray 
  # Swap the value of array elements
  def swap(arr, start, last) 
    temp = arr[start]
    arr[start] = arr[last]
    arr[last] = temp
  end
  def move_zero(arr, size, current) 
    front = 0
    back = size - 1
    while (front < back) 
      if (arr[back] == current) 
        back -= 1
      elsif (arr[front] == current) 
        self.swap(arr, front, back)
        front += 1
        back -= 1
      else 
        front += 1
      end
    end
  end
  # Display array element values
  def dispay(arr, size) 
    i = 0
    while (i < size) 
      print(" ", arr[i])
      i += 1
    end
    print("\n")
  end
end
def main() 
  obj = MyArray.new()
  # Define the value of array elements
  arr = [0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0]
  # Get the size of array
  size = arr.length
  print(" Before Move  :\n")
  obj.dispay(arr, size)
  # zero is move element
  obj.move_zero(arr, size, 0)
  print(" After Move  :\n")
  obj.dispay(arr, size)
end
main()

Output

 Before Move  :
 0 1 4 7 2 2 1 2 0 0 6 0 1 1 0
 After Move  :
 1 1 4 7 2 2 1 2 1 6 0 0 0 0 0
/*
  Scala Program
  Move all zeroes to end of array
*/
class MyArray {
  //Swap the value of array elements
  def swap(arr: Array[Int], start: Int, last: Int): Unit = {
    val temp: Int = arr(start);
    arr(start) = arr(last);
    arr(last) = temp;
  }
  def move_zero(arr: Array[Int], size: Int, current: Int): Unit = {
    var front: Int = 0;
    var back: Int = size - 1;
    while (front < back) {
      if (arr(back) == current) {
        back -= 1;
      } else
      if (arr(front) == current) {
        this.swap(arr, front, back);
        front += 1;
        back -= 1;
      } else {
        front += 1;
      }
    }
  }
  //Display array element values
  def dispay(arr: Array[Int], size: Int): Unit = {
    var i: Int = 0;
    while (i < size) {
      print(" " + arr(i));
      i += 1;
    }
    print("\n");
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val obj: MyArray = new MyArray();

    //Define the value of array elements
    var arr: Array[Int] = Array(0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0);

    //Get the size of array
    val size: Int = arr.length;
    print(" Before Move : \n");
    obj.dispay(arr, size);

    //zero is move element
    obj.move_zero(arr, size, 0);
    print(" After Move : \n");
    obj.dispay(arr, size);
  }
}

Output

 Before Move :
 0 1 4 7 2 2 1 2 0 0 6 0 1 1 0
 After Move :
 1 1 4 7 2 2 1 2 1 6 0 0 0 0 0
/*
  Swift Program
  Move all zeroes to end of array
*/
class MyArray {
  //Swap the value of array elements
  func swap(_ arr: inout [Int], _ start: Int, _ last: Int) {
    let temp: Int = arr[start];
    arr[start] = arr[last];
    arr[last] = temp;
  }
  func move_zero(_ arr: inout [Int], _ size: Int, _ current: Int) {
    var front: Int = 0;
    var back: Int = size - 1;
    while (front < back) {
      if (arr[back] == current) {
        back -= 1;
      } else
      if (arr[front] == current) {
        self.swap(&arr, front, back);
        front += 1;
        back -= 1;
      } else {
        front += 1;
      }
    }
  }
  //Display array element values
  func dispay(_ arr: [Int], _ size: Int) {
    var i: Int = 0;
    while (i < size) {
      print(" ", arr[i], terminator: "");
      i += 1;
    }
    print("\n", terminator: "");
  }
}
func main() {
  let obj: MyArray = MyArray();
  //Define the value of array elements
  var arr: [Int] = [0, 1, 4, 7, 2, 2, 1, 2, 0, 0, 6, 0, 1, 1, 0];
  //Get the size of array
  let size: Int = arr.count;
  print(" Before Move : \n", terminator: "");
  obj.dispay(arr, size);
  //zero is move element
  obj.move_zero(&arr, size, 0);
  print(" After Move : \n", terminator: "");
  obj.dispay(arr, size);
}
main();

Output

 Before Move :
  0  1  4  7  2  2  1  2  0  0  6  0  1  1  0
 After Move :
  1  1  4  7  2  2  1  2  1  6  0  0  0  0  0
/*
  C# Program
  Move all zeroes to end of array
*/
using System;

public class MyArray {
  //Swap the value of array elements
  public void swap(int[] arr, int start, int last) {
    int temp = arr[start];
    arr[start] = arr[last];
    arr[last] = temp;
  }
  public void move_zero(int[] arr, int size, int current) {
    int front = 0, back = size - 1;
    while (front < back) {
      if (arr[back] == current) {
        back--;
      } else
      if (arr[front] == current) {
        swap(arr, front, back);
        front++;
        back--;
      } else {
        front++;
      }
    }
  }
  //Display array element values
  public void dispay(int[] arr, int size) {
    for (int i = 0; i < size; ++i) {
      Console.Write(" " + arr[i]);
    }
    Console.Write("\n");
  }
  public static void Main(String[] args) {
    MyArray obj = new MyArray();
    //Define the value of array elements
    int[] arr = {
      0,
      1,
      4,
      7,
      2,
      2,
      1,
      2,
      0,
      0,
      6,
      0,
      1,
      1,
      0
    };
    //Get the size of array
    int size = arr.Length;
    Console.Write(" Before Move : \n");
    obj.dispay(arr, size);
    obj.move_zero(arr, size, 0);
    Console.Write(" After Move : \n");
    obj.dispay(arr, size);
  }
}

Output

 Before Move :
 0 1 4 7 2 2 1 2 0 0 6 0 1 1 0
 After Move :
 1 1 4 7 2 2 1 2 1 6 0 0 0 0 0


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved