Recursive Bubble Sort

Here given code implementation process.

//C Program
//Recursive Bubble Sort
#include <stdio.h>

//Display array elements
void display(int record[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		printf("%d ", record[i]);
	}
	printf("\n");
}
//Perform bubble sort in given array
void bubble_sort(int record[], int size)
{
	if (size <= 0)
	{
		//When have no more than one element
		return;
	}
	int temp = 0;
	for (int i = 0; i < size - 1; ++i)
	{
		//Compare two adjacent elements
		if (record[i] > record[i + 1])
		{
			//When need to swap two adjacent elements
			temp = record[i];
			record[i] = record[i + 1];
			record[i + 1] = temp;
		}
	}
	bubble_sort(record, size - 1);
}
int main()
{
	//Assume given array elements
	int record[] = {
		2 , 8 , 1 , 5 , 0 , 9 , 3 , 7 , 2
	};
	//Get the size of array 
	int size = sizeof(record) / sizeof(record[0]);
	//Before sort
	display(record, size);
	bubble_sort(record, size);
	//After sort
	display(record, size);
	return 0;
}

Output

2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
//Java Program
//Recursive Bubble Sort
class MySort
{
	//Display array elements
	public void display(int[] record, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			System.out.print(" " + record[i]);
		}
		System.out.print("\n");
	}
	//Perform bubble sort in given array
	public void bubble_sort(int[] record, int size)
	{
		if (size <= 0)
		{
			//When have no more than one element
			return;
		}
		int temp = 0;
		for (int i = 0; i < size - 1; ++i)
		{
			//Compare two adjacent elements
			if (record[i] > record[i + 1])
			{
				//When need to swap two adjacent elements
				temp = record[i];
				record[i] = record[i + 1];
				record[i + 1] = temp;
			}
		}
		bubble_sort(record, size - 1);
	}
	public static void main(String[] args)
	{
		MySort obj = new MySort();
		//Assume given array elements
		int[] record = {
			2,
			8,
			1,
			5,
			0,
			9,
			3,
			7,
			2
		};
		//Get the size of array 
		int size = record.length;
		//Before sort
		obj.display(record, size);
		obj.bubble_sort(record, size);
		//After sort
		obj.display(record, size);
	}
}

Output

 2 8 1 5 0 9 3 7 2
 0 1 2 2 3 5 7 8 9
//Include header file
#include <iostream>
using namespace std;

//C++ Program
//Recursive Bubble Sort
class MySort
{
	public:
		//Display array elements
		void display(int record[], int size)
		{
			for (int i = 0; i < size; ++i)
			{
				cout << " " << record[i];
			}
			cout << "\n";
		}
	//Perform bubble sort in given array
	void bubble_sort(int record[], int size)
	{
		if (size <= 0)
		{
			//When have no more than one element
			return;
		}
		int temp = 0;
		for (int i = 0; i < size - 1; ++i)
		{
			//Compare two adjacent elements
			if (record[i] > record[i + 1])
			{
				//When need to swap two adjacent elements
				temp = record[i];
				record[i] = record[i + 1];
				record[i + 1] = temp;
			}
		}
		this->bubble_sort(record, size - 1);
	}
};
int main()
{
	MySort obj = MySort();
	int record[] = {
		2 , 8 , 1 , 5 , 0 , 9 , 3 , 7 , 2
	};
	//Get the size of array 
	int size = sizeof(record) / sizeof(record[0]);
	//Before sort
	obj.display(record, size);
	obj.bubble_sort(record, size);
	//After sort
	obj.display(record, size);
	return 0;
}

Output

 2 8 1 5 0 9 3 7 2
 0 1 2 2 3 5 7 8 9
//Include namespace system
using System;
//C# Program
//Recursive Bubble Sort
class MySort
{
	//Display array elements
	public void display(int[] record, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			Console.Write(" " + record[i]);
		}
		Console.Write("\n");
	}
	//Perform bubble sort in given array
	public void bubble_sort(int[] record, int size)
	{
		if (size <= 0)
		{
			//When have no more than one element
			return;
		}
		int temp = 0;
		for (int i = 0; i < size - 1; ++i)
		{
			//Compare two adjacent elements
			if (record[i] > record[i + 1])
			{
				//When need to swap two adjacent elements
				temp = record[i];
				record[i] = record[i + 1];
				record[i + 1] = temp;
			}
		}
		bubble_sort(record, size - 1);
	}
	public static void Main(String[] args)
	{
		MySort obj = new MySort();
		int[] record = {
			2 , 8 , 1 , 5 , 0 , 9 , 3 , 7 , 2
		};
		//Get the size of array 
		int size = record.Length;
		//Before sort
		obj.display(record, size);
		obj.bubble_sort(record, size);
		//After sort
		obj.display(record, size);
	}
}

Output

 2 8 1 5 0 9 3 7 2
 0 1 2 2 3 5 7 8 9
<?php
//Php Program
//Recursive Bubble Sort
class MySort
{
	//Display array elements
	public	function display( $record, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			echo " ". $record[$i];
		}
		echo "\n";
	}
	//Perform bubble sort in given array
	public	function bubble_sort( & $record, $size)
	{
		if ($size <= 0)
		{
			//When have no more than one element
			return;
		}
		$temp = 0;
		for ($i = 0; $i < $size - 1; ++$i)
		{
			//Compare two adjacent elements
			if ($record[$i] > $record[$i + 1])
			{
				//When need to swap two adjacent elements
				$temp = $record[$i];
				$record[$i] = $record[$i + 1];
				$record[$i + 1] = $temp;
			}
		}
		$this->bubble_sort($record, $size - 1);
	}
}

function main()
{
	$obj = new MySort();
	//Assume given array elements
	$record = array(2, 8, 1, 5, 0, 9, 3, 7, 2);
	//Get the size of array 
	$size = count($record);
	//Before sort
	$obj->display($record, $size);
	$obj->bubble_sort($record, $size);
	//After sort
	$obj->display($record, $size);
}
main();

Output

 2 8 1 5 0 9 3 7 2
 0 1 2 2 3 5 7 8 9
//Node Js Program
//Recursive Bubble Sort
class MySort
{
	//Display array elements
	display(record, size)
	{
		for (var i = 0; i < size; ++i)
		{
			process.stdout.write(" " + record[i]);
		}
		process.stdout.write("\n");
	}
	//Perform bubble sort in given array
	bubble_sort(record, size)
	{
		if (size <= 0)
		{
			//When have no more than one element
			return;
		}
		var temp = 0;
		for (var i = 0; i < size - 1; ++i)
		{
			//Compare two adjacent elements
			if (record[i] > record[i + 1])
			{
				//When need to swap two adjacent elements
				temp = record[i];
				record[i] = record[i + 1];
				record[i + 1] = temp;
			}
		}
		this.bubble_sort(record, size - 1);
	}
}

function main()
{
	var obj = new MySort();
	//Assume given array elements
	var record = [2, 8, 1, 5, 0, 9, 3, 7, 2];
	//Get the size of array 
	var size = record.length;
	//Before sort
	obj.display(record, size);
	obj.bubble_sort(record, size);
	//After sort
	obj.display(record, size);
}
main();

Output

 2 8 1 5 0 9 3 7 2
 0 1 2 2 3 5 7 8 9
# Python 3 Program
# Recursive Bubble Sort
class MySort :
	# Display array elements
	def display(self, record, size) :
		i = 0
		while (i < size) :
			print(" ", record[i], end = "")
			i += 1
		
		print("\n", end = "")
	
	# Perform bubble sort in given array
	def bubble_sort(self, record, size) :
		if (size <= 0) :
			# When have no more than one element
			return
		
		temp = 0
		i = 0
		while (i < size - 1) :
			# Compare two adjacent elements
			if (record[i] > record[i + 1]) :
				# When need to swap two adjacent elements
				temp = record[i]
				record[i] = record[i + 1]
				record[i + 1] = temp
			
			i += 1
		
		self.bubble_sort(record, size - 1)
	

def main() :
	obj = MySort()
	# Assume given array elements
	record = [2, 8, 1, 5, 0, 9, 3, 7, 2]
	# Get the size of array 
	size = len(record)
	# Before sort
	obj.display(record, size)
	obj.bubble_sort(record, size)
	# After sort
	obj.display(record, size)

if __name__ == "__main__": main()

Output

  2  8  1  5  0  9  3  7  2
  0  1  2  2  3  5  7  8  9
# Ruby Program
# Recursive Bubble Sort
class MySort

	# Display array elements
	def display(record, size)
	
		i = 0
		while (i < size)
		
			print(" ", record[i])
			i += 1
		end
		print("\n")
	end
	# Perform bubble sort in given array
	def bubble_sort(record, size)
	
		if (size <= 0)
		
			# When have no more than one element
			return
		end
		temp = 0
		i = 0
		while (i < size - 1)
		
			# Compare two adjacent elements
			if (record[i] > record[i + 1])
			
				# When need to swap two adjacent elements
				temp = record[i]
				record[i] = record[i + 1]
				record[i + 1] = temp
			end
			i += 1
		end
		self.bubble_sort(record, size - 1)
	end
end
def main()

	obj = MySort.new()
	# Assume given array elements
	record = [2, 8, 1, 5, 0, 9, 3, 7, 2]
	# Get the size of array 
	size = record.length
	# Before sort
	obj.display(record, size)
	obj.bubble_sort(record, size)
	# After sort
	obj.display(record, size)
end
main()

Output

 2 8 1 5 0 9 3 7 2
 0 1 2 2 3 5 7 8 9
//Scala Program
//Recursive Bubble Sort
class MySort
{
	//Display array elements
	def display(record: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			print(" " + record(i));
			i += 1;
		}
		print("\n");
	}
	//Perform bubble sort in given array
	def bubble_sort(record: Array[Int], size: Int): Unit = {
		if (size <= 0)
		{
			//When have no more than one element
			return;
		}
		var temp: Int = 0;
		var i: Int = 0;
		while (i < size - 1)
		{
			//Compare two adjacent elements
			if (record(i) > record(i + 1))
			{
				//When need to swap two adjacent elements
				temp = record(i);
				record(i) = record(i + 1);
				record(i + 1) = temp;
			}
			i += 1;
		}
		bubble_sort(record, size - 1);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MySort = new MySort();
		//Assume given array elements
		var record: Array[Int] = Array(2, 8, 1, 5, 0, 9, 3, 7, 2);
		//Get the size of array 
		var size: Int = record.length;
		//Before sort
		obj.display(record, size);
		obj.bubble_sort(record, size);
		//After sort
		obj.display(record, size);
	}
}

Output

 2 8 1 5 0 9 3 7 2
 0 1 2 2 3 5 7 8 9
//Swift Program
//Recursive Bubble Sort
class MySort
{
	//Display array elements
	func display(_ record: [Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			print(" ", record[i], terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
	//Perform bubble sort in given array
	func bubble_sort(_ record: inout[Int], _ size: Int)
	{
		if (size <= 0)
		{
			//When have no more than one element
			return;
		}
		var temp: Int = 0;
		var i: Int = 0;
		while (i < size - 1)
		{
			//Compare two adjacent elements
			if (record[i] > record[i + 1])
			{
				//When need to swap two adjacent elements
				temp = record[i];
				record[i] = record[i + 1];
				record[i + 1] = temp;
			}
			i += 1;
		}
		self.bubble_sort(&record, size - 1);
	}
}
func main()
{
	let obj: MySort = MySort();
	//Assume given array elements
	var record: [Int] = [2, 8, 1, 5, 0, 9, 3, 7, 2];
	//Get the size of array 
	let size: Int = record.count;
	//Before sort
	obj.display(record, size);
	obj.bubble_sort(&record, size);
	//After sort
	obj.display(record, size);
}
main();

Output

  2  8  1  5  0  9  3  7  2
  0  1  2  2  3  5  7  8  9


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