Move all zeros to the beginning of the array

Here given code implementation process.

// C Program 
// Move all zeros to the beginning of the array 
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("  %d", arr[i]);
	}
	printf("\n");
}
// Swap the elements of array by given location
void swapElement(int arr[], int front, int tail)
{
	int temp = arr[front];
	arr[front] = arr[tail];
	arr[tail] = temp;
}
// Move all zero elements to front side of given array
void moveZero(int arr[], int size)
{
	// Get first position
	int head = 0;
	// Get last position
	int tail = size - 1;
	// Executes the loop until the value of head variable is less than tail variable
	while (head < tail)
	{
		if (arr[head] == 0)
		{
			// When starting i position zero exist
			head++;
		}
		else
		{
			if (arr[tail] == 0)
			{
				// Swap the array elements
				swapElement(arr, head, tail);
				head++;
				tail--;
			}
			else
			{
				tail--;
			}
		}
	}
}
int main(int argc, char const *argv[])
{
	// Define array of integer elements
	int arr[] = {
		1 , 8 , 12 , 0 , 3 , 1 , 0 , 2 , 0 , 1 , 1 , 0
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	printf("  Array Element \n");
	printArray(arr, size);
	moveZero(arr, size);
	printf("  After move zero at beginning \n");
	printArray(arr, size);
	return 0;
}

Output

  Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
  After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1
/*
    Java Program
    Move all zeros to the beginning of the array 
*/
public class MoveElement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print("  " + arr[i]);
		}
		System.out.print("\n");
	}
	// Swap the elements of array by given location
	public void swapElement(int[] arr, int front, int tail)
	{
		int temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move all zero elements to front side of given array
	public void moveZero(int[] arr, int size)
	{
		// Get first position
		int head = 0;
		// Get last position
		int tail = size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while (head < tail)
		{
			if (arr[head] == 0)
			{
				// When starting i position zero exist
				head++;
			}
			else
			{
				if (arr[tail] == 0)
				{
					// Swap the array elements
					swapElement(arr, head, tail);
					head++;
					tail--;
				}
				else
				{
					tail--;
				}
			}
		}
	}
	public static void main(String[] args)
	{
		MoveElement task = new MoveElement();
		// Define array of integer elements
		int[] arr = {
			1 , 8 , 12 , 0 , 3 , 1 , 0 , 2 , 0 , 1 , 1 , 0
		};
		// Get the size
		int size = arr.length;
		System.out.print(" Array Element \n");
		task.printArray(arr, size);
		task.moveZero(arr, size);
		System.out.print(" After move zero at beginning \n");
		task.printArray(arr, size);
	}
}

Output

 Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1
// Include header file
#include <iostream>

using namespace std;
/*
    C++ Program
    Move all zeros to the beginning of the array 
*/
class MoveElement
{
	public:
		//Display elements of given array
		void printArray(int arr[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << "  " << arr[i];
			}
			cout << "\n";
		}
	// Swap the elements of array by given location
	void swapElement(int arr[], int front, int tail)
	{
		int temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move all zero elements to front side of given array
	void moveZero(int arr[], int size)
	{
		// Get first position
		int head = 0;
		// Get last position
		int tail = size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while (head < tail)
		{
			if (arr[head] == 0)
			{
				// When starting i position zero exist
				head++;
			}
			else
			{
				if (arr[tail] == 0)
				{
					// Swap the array elements
					this->swapElement(arr, head, tail);
					head++;
					tail--;
				}
				else
				{
					tail--;
				}
			}
		}
	}
};
int main()
{
	MoveElement task = MoveElement();
	// Define array of integer elements
	int arr[] = {
		1 , 8 , 12 , 0 , 3 , 1 , 0 , 2 , 0 , 1 , 1 , 0
	};
	// Get the size
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << " Array Element \n";
	task.printArray(arr, size);
	task.moveZero(arr, size);
	cout << " After move zero at beginning \n";
	task.printArray(arr, size);
	return 0;
}

Output

 Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1
// Include namespace system
using System;
/*
    C# Program
    Move all zeros to the beginning of the array 
*/
public class MoveElement
{
	//Display elements of given array
	public void printArray(int[] arr, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write("  " + arr[i]);
		}
		Console.Write("\n");
	}
	// Swap the elements of array by given location
	public void swapElement(int[] arr, int front, int tail)
	{
		int temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move all zero elements to front side of given array
	public void moveZero(int[] arr, int size)
	{
		// Get first position
		int head = 0;
		// Get last position
		int tail = size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while (head < tail)
		{
			if (arr[head] == 0)
			{
				// When starting i position zero exist
				head++;
			}
			else
			{
				if (arr[tail] == 0)
				{
					// Swap the array elements
					swapElement(arr, head, tail);
					head++;
					tail--;
				}
				else
				{
					tail--;
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		MoveElement task = new MoveElement();
		// Define array of integer elements
		int[] arr = {
			1 , 8 , 12 , 0 , 3 , 1 , 0 , 2 , 0 , 1 , 1 , 0
		};
		// Get the size
		int size = arr.Length;
		Console.Write(" Array Element \n");
		task.printArray(arr, size);
		task.moveZero(arr, size);
		Console.Write(" After move zero at beginning \n");
		task.printArray(arr, size);
	}
}

Output

 Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1
<?php
/*
    Php Program
    Move all zeros to the beginning of the array 
*/
class MoveElement
{
	//Display elements of given array
	public	function printArray( & $arr, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo "  ". $arr[$i];
		}
		echo "\n";
	}
	// Swap the elements of array by given location
	public	function swapElement( & $arr, $front, $tail)
	{
		$temp = $arr[$front];
		$arr[$front] = $arr[$tail];
		$arr[$tail] = $temp;
	}
	// Move all zero elements to front side of given array
	public	function moveZero( & $arr, $size)
	{
		// Get first position
		$head = 0;
		// Get last position
		$tail = $size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while ($head < $tail)
		{
			if ($arr[$head] == 0)
			{
				// When starting i position zero exist
				$head++;
			}
			else
			{
				if ($arr[$tail] == 0)
				{
					// Swap the array elements
					$this->swapElement($arr, $head, $tail);
					$head++;
					$tail--;
				}
				else
				{
					$tail--;
				}
			}
		}
	}
}

function main()
{
	$task = new MoveElement();
	// Define array of integer elements
	$arr = array(1, 8, 12, 0, 3, 1, 0, 2, 0, 1, 1, 0);
	// Get the size
	$size = count($arr);
	echo " Array Element \n";
	$task->printArray($arr, $size);
	$task->moveZero($arr, $size);
	echo " After move zero at beginning \n";
	$task->printArray($arr, $size);
}
main();

Output

 Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1
/*
    Node Js Program
    Move all zeros to the beginning of the array 
*/
class MoveElement
{
	//Display elements of given array
	printArray(arr, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write("  " + arr[i]);
		}
		process.stdout.write("\n");
	}
	// Swap the elements of array by given location
	swapElement(arr, front, tail)
	{
		var temp = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move all zero elements to front side of given array
	moveZero(arr, size)
	{
		// Get first position
		var head = 0;
		// Get last position
		var tail = size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while (head < tail)
		{
			if (arr[head] == 0)
			{
				// When starting i position zero exist
				head++;
			}
			else
			{
				if (arr[tail] == 0)
				{
					// Swap the array elements
					this.swapElement(arr, head, tail);
					head++;
					tail--;
				}
				else
				{
					tail--;
				}
			}
		}
	}
}

function main()
{
	var task = new MoveElement();
	// Define array of integer elements
	var arr = [1, 8, 12, 0, 3, 1, 0, 2, 0, 1, 1, 0];
	// Get the size
	var size = arr.length;
	process.stdout.write(" Array Element \n");
	task.printArray(arr, size);
	task.moveZero(arr, size);
	process.stdout.write(" After move zero at beginning \n");
	task.printArray(arr, size);
}
main();

Output

 Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1
#   Python 3 Program
#   Move all zeros to the beginning of the array 

class MoveElement :
	# Display elements of given array
	def printArray(self, arr, size) :
		i = 0
		while (i < size) :
			print("  ", arr[i], end = "")
			i += 1
		
		print(end = "\n")
	
	#  Swap the elements of array by given location
	def swapElement(self, arr, front, tail) :
		temp = arr[front]
		arr[front] = arr[tail]
		arr[tail] = temp
	
	#  Move all zero elements to front side of given array
	def moveZero(self, arr, size) :
		#  Get first position
		head = 0
		#  Get last position
		tail = size - 1
		#  Executes the loop until the value of head variable is less than tail variable
		while (head < tail) :
			if (arr[head] == 0) :
				#  When starting i position zero exist
				head += 1
			else :
				if (arr[tail] == 0) :
					#  Swap the array elements
					self.swapElement(arr, head, tail)
					head += 1
					tail -= 1
				else :
					tail -= 1
				
			
		
	

def main() :
	task = MoveElement()
	#  Define array of integer elements
	arr = [1, 8, 12, 0, 3, 1, 0, 2, 0, 1, 1, 0]
	#  Get the size
	size = len(arr)
	print(" Array Element ")
	task.printArray(arr, size)
	task.moveZero(arr, size)
	print(" After move zero at beginning ")
	task.printArray(arr, size)

if __name__ == "__main__": main()

Output

 Array Element
   1   8   12   0   3   1   0   2   0   1   1   0
 After move zero at beginning
   0   0   0   0   3   1   12   2   8   1   1   1
#  Ruby Program
#  Move all zeros to the beginning of the array 

class MoveElement 
	# Display elements of given array
	def printArray(arr, size) 
		i = 0
		while (i < size) 
			print("  ", arr[i])
			i += 1
		end

		print("\n")
	end

	#  Swap the elements of array by given location
	def swapElement(arr, front, tail) 
		temp = arr[front]
		arr[front] = arr[tail]
		arr[tail] = temp
	end

	#  Move all zero elements to front side of given array
	def moveZero(arr, size) 
		#  Get first position
		head = 0
		#  Get last position
		tail = size - 1
		#  Executes the loop until the value of head variable is less than tail variable
		while (head < tail) 
			if (arr[head] == 0) 
				#  When starting i position zero exist
				head += 1
			else 
				if (arr[tail] == 0) 
					#  Swap the array elements
					self.swapElement(arr, head, tail)
					head += 1
					tail -= 1
				else 
					tail -= 1
				end

			end

		end

	end

end

def main() 
	task = MoveElement.new()
	#  Define array of integer elements
	arr = [1, 8, 12, 0, 3, 1, 0, 2, 0, 1, 1, 0]
	#  Get the size
	size = arr.length
	print(" Array Element \n")
	task.printArray(arr, size)
	task.moveZero(arr, size)
	print(" After move zero at beginning \n")
	task.printArray(arr, size)
end

main()

Output

 Array Element 
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning 
  0  0  0  0  3  1  12  2  8  1  1  1
/*
    Scala Program
    Move all zeros to the beginning of the array 
*/
class MoveElement
{
	//Display elements of given array
	def printArray(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print("  " + arr(i));
			i += 1;
		}
		print("\n");
	}
	// Swap the elements of array by given location
	def swapElement(arr: Array[Int], front: Int, tail: Int): Unit = {
		var temp: Int = arr(front);
		arr(front) = arr(tail);
		arr(tail) = temp;
	}
	// Move all zero elements to front side of given array
	def moveZero(arr: Array[Int], size: Int): Unit = {
		// Get first position
		var head: Int = 0;
		// Get last position
		var tail: Int = size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while (head < tail)
		{
			if (arr(head) == 0)
			{
				// When starting i position zero exist
				head += 1;
			}
			else
			{
				if (arr(tail) == 0)
				{
					// Swap the array elements
					this.swapElement(arr, head, tail);
					head += 1;
					tail -= 1;
				}
				else
				{
					tail -= 1;
				}
			}
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: MoveElement = new MoveElement();
		// Define array of integer elements
		var arr: Array[Int] = Array(1, 8, 12, 0, 3, 1, 0, 2, 0, 1, 1, 0);
		// Get the size
		var size: Int = arr.length;
		print(" Array Element \n");
		task.printArray(arr, size);
		task.moveZero(arr, size);
		print(" After move zero at beginning \n");
		task.printArray(arr, size);
	}
}

Output

 Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1
/*
    Swift 4 Program
    Move all zeros to the beginning of the array 
*/
class MoveElement
{
	//Display elements of given array
	func printArray(_ arr: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print("  ", arr[i], terminator: "");
			i += 1;
		}
		print(terminator: "\n");
	}
	// Swap the elements of array by given location
	func swapElement(_ arr: inout[Int], _ front: Int, _ tail: Int)
	{
		let temp: Int = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move all zero elements to front side of given array
	func moveZero(_ arr: inout[Int], _ size: Int)
	{
		// Get first position
		var head: Int = 0;
		// Get last position
		var tail: Int = size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while (head < tail)
		{
			if (arr[head] == 0)
			{
				// When starting i position zero exist
				head += 1;
			}
			else
			{
				if (arr[tail] == 0)
				{
					// Swap the array elements
					self.swapElement(&arr, head, tail);
					head += 1;
					tail -= 1;
				}
				else
				{
					tail -= 1;
				}
			}
		}
	}
}
func main()
{
	let task: MoveElement = MoveElement();
	// Define array of integer elements
	var arr: [Int] = [1, 8, 12, 0, 3, 1, 0, 2, 0, 1, 1, 0];
	// Get the size
	let size: Int = arr.count;
	print(" Array Element ");
	task.printArray(arr, size);
	task.moveZero(&arr, size);
	print(" After move zero at beginning ");
	task.printArray(arr, size);
}
main();

Output

 Array Element
   1   8   12   0   3   1   0   2   0   1   1   0
 After move zero at beginning
   0   0   0   0   3   1   12   2   8   1   1   1
/*
    Kotlin Program
    Move all zeros to the beginning of the array 
*/
class MoveElement
{
	//Display elements of given array
	fun printArray(arr: Array<Int>, size: Int): Unit
	{
		var i: Int = 0;
		while (i<size)
		{
			print("  " + arr[i]);
			i += 1;
		}
		print("\n");
	}
	// Swap the elements of array by given location
	fun swapElement(arr: Array<Int>, front: Int, tail: Int): Unit
	{
		var temp: Int = arr[front];
		arr[front] = arr[tail];
		arr[tail] = temp;
	}
	// Move all zero elements to front side of given array
	fun moveZero(arr: Array<Int>, size: Int): Unit
	{
		// Get first position
		var head: Int = 0;
		// Get last position
		var tail: Int = size - 1;
		// Executes the loop until the value of head variable is less than tail variable
		while (head<tail)
		{
			if (arr[head] == 0)
			{
				// When starting i position zero exist
				head += 1;
			}
			else
			{
				if (arr[tail] == 0)
				{
					// Swap the array elements
					this.swapElement(arr, head, tail);
					head += 1;
					tail -= 1;
				}
				else
				{
					tail -= 1;
				}
			}
		}
	}
}

fun main(args: Array<String>): Unit
{
	var task: MoveElement = MoveElement();
	// Define array of integer elements
	var arr: Array<Int> = arrayOf(1, 8, 12, 0, 3, 1, 0, 2, 0, 1, 1, 0);
	// Get the size
	var size: Int = arr.count();
	print(" Array Element \n");
	task.printArray(arr, size);
	task.moveZero(arr, size);
	print(" After move zero at beginning \n");
	task.printArray(arr, size);
}

Output

 Array Element
  1  8  12  0  3  1  0  2  0  1  1  0
 After move zero at beginning
  0  0  0  0  3  1  12  2  8  1  1  1


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