Bubble Sort Program

Here given code implementation process.

//C Program 
//Bubble Sort Program
#include <stdio.h>
//Swap the array element
void swap(int arr[],int start,int end)
{
  int temp=arr[start];
  arr[start]=arr[end];
  arr[end]=temp;
}

//Implement bubble sort in given array
void bubble_sort(int arr[],int size)
{
  for (int i = 0; i < size; ++i)
  {
    for (int j = 0; j <(size-1)-i ; ++j)
    {
      //Swap array element when next node is small
      if(arr[j] >arr[j+1] )
      {
        swap(arr,j,j+1);
      }
    }
  }
}
//Display array elements
void print_array(int arr[],int size)
{
  
  for(int i=0;i<size;i++)
  {
    printf(" %d ",arr[i] );
  }
  printf("\n");
}
int main()
{
  //Given array elements
  int arr[]={ 9,2,1,6,-3,7,-2,1,-5,12 };
  
  //Get the size of array elements
  int size=sizeof(arr)/sizeof(arr[0]);

  printf("Before Sort : \n");
  print_array(arr,size);

  bubble_sort(arr,size);
  printf("After Sort : \n");
  print_array(arr,size);

  return 0;
}

Output

Before Sort :
 9  2  1  6  -3  7  -2  1  -5  12
After Sort :
 -5  -3  -2  1  1  2  6  7  9  12
/*
  C++ Program
  Bubble Sort Program
*/
#include<iostream>

using namespace std;


class MySort {
	public:

		//Swap the array element
		void swap(int arr[], int start, int end) {
			int temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
		}
	//Implement bubble sort in given array
	void bubble_sort(int arr[], int size) {
		for (int i = 0; i < size; ++i) {
			for (int j = 0; j < (size - 1) - i; ++j) {
				//Swap array element when next node is small

				if (arr[j] > arr[j + 1]) {
					this->swap(arr, j, j + 1);
				}
			}
		}
	}
	//Display array elements
	void print_array(int arr[], int size) {
		for (int i = 0; i < size; i++) {
			cout << " " << arr[i];
		}
		cout << "\n";
	}
};
int main() {
	MySort obj ;
	int arr[] = {
		9,
		2,
		1,
		6,
		-3,
		7,
		-2,
		1,
		-5,
		12
	};
	//Get the size of array elements
	int size = sizeof(arr) / sizeof(arr[0]);
	cout << "Before Sort : \n";
	obj.print_array(arr, size);
	obj.bubble_sort(arr, size);
	cout << "After Sort : \n";
	obj.print_array(arr, size);
	return 0;
}

Output

Before Sort :
 9 2 1 6 -3 7 -2 1 -5 12
After Sort :
 -5 -3 -2 1 1 2 6 7 9 12
/*
  Java Program
  Bubble Sort Program
*/
public class MySort {

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

  //Implement bubble sort in given array
  public void bubble_sort(int []arr,int size)
  {
    for (int i = 0; i < size; ++i)
    {
      for (int j = 0; j <(size-1)-i ; ++j)
      {
        //Swap array element when next node is small
        if(arr[j] >arr[j+1] )
        {
          swap(arr,j,j+1);
        }
      }
    }
  }
  //Display array elements
  public void print_array(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) 
  {
  

    MySort obj = new MySort();
    //Given array elements
    int []arr={ 9,2,1,6,-3,7,-2,1,-5,12 };
    
    //Get the size of array elements
    int size=arr.length;

    System.out.print("Before Sort : \n");
    obj.print_array(arr,size);

    obj.bubble_sort(arr,size);
    System.out.print("After Sort : \n");
    obj.print_array(arr,size);
  }
}

Output

Before Sort :
 9 2 1 6 -3 7 -2 1 -5 12
After Sort :
 -5 -3 -2 1 1 2 6 7 9 12
/*
  C# Program
  Bubble Sort Program
*/
using System;

public class MySort {
	//Swap the array element
	public void swap(int[] arr, int start, int end) {
		int temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
	}
	//Implement bubble sort in given array
	public void bubble_sort(int[] arr, int size) {
		for (int i = 0; i < size; ++i) {
			for (int j = 0; j < (size - 1) - i; ++j) {
				//Swap array element when next node is small

				if (arr[j] > arr[j + 1]) {
					swap(arr, j, j + 1);
				}
			}
		}
	}
	//Display array elements
	public void print_array(int[] arr, int size) {
		for (int i = 0; i < size; i++) {
			Console.Write(" " + arr[i]);
		}
		Console.Write("\n");
	}
	public static void Main(String[] args) {
		MySort obj = new MySort();
      	//Given array elements
		int[] arr = {
			9,
			2,
			1,
			6,
			-3,
			7,
			-2,
			1,
			-5,
			12
		};
		//Get the size of array elements
		int size = arr.Length;
		Console.Write("Before Sort : \n");
		obj.print_array(arr, size);
		obj.bubble_sort(arr, size);
		Console.Write("After Sort : \n");
		obj.print_array(arr, size);
	}
}

Output

Before Sort :
 9 2 1 6 -3 7 -2 1 -5 12
After Sort :
 -5 -3 -2 1 1 2 6 7 9 12
<?php
/*
  Php Program
  Bubble Sort Program
*/
class MySort {
	//Swap the array element

	public 	function swap(&$arr, $start, $end) {
		$temp = $arr[$start];
		$arr[$start] = $arr[$end];
		$arr[$end] = $temp;
	}
	//Implement bubble sort in given array

	public 	function bubble_sort(&$arr, $size) {
		for ($i = 0; $i < $size; ++$i) {
			for ($j = 0; $j < ($size - 1) - $i; ++$j) {
				//Swap array element when next node is small

				if ($arr[$j] > $arr[$j + 1]) {
					$this->swap($arr, $j, $j + 1);
				}
			}
		}
	}
	//Display array elements

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

function main() {
	$obj = new MySort();
	//Given array elements
	$arr = array(9, 2, 1, 6, -3, 7, -2, 1, -5, 12);
	//Get the size of array elements
	$size = count($arr);
	echo("Before Sort : \n");
	$obj->print_array($arr, $size);
	$obj->bubble_sort($arr, $size);
	echo("After Sort : \n");
	$obj->print_array($arr, $size);

}
main();

Output

Before Sort :
 9 2 1 6 -3 7 -2 1 -5 12
After Sort :
 -5 -3 -2 1 1 2 6 7 9 12
/*
  Node Js Program
  Bubble Sort Program
*/
class MySort {
	//Swap the array element
	swap(arr, start, end) {
		var temp = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
	}

	//Implement bubble sort in given array
	bubble_sort(arr, size) {
		for (var i = 0; i < size; ++i) {
			for (var j = 0; j < (size - 1) - i; ++j) {
				//Swap array element when next node is small

				if (arr[j] > arr[j + 1]) {
					this.swap(arr, j, j + 1);
				}
			}
		}
	}

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

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

function main(args) {
	var obj = new MySort();
	//Given array elements
	var arr = [9, 2, 1, 6, -3, 7, -2, 1, -5, 12];
	//Get the size of array elements
	var size = arr.length;
	process.stdout.write("Before Sort : \n");
	obj.print_array(arr, size);
	obj.bubble_sort(arr, size);
	process.stdout.write("After Sort : \n");
	obj.print_array(arr, size);
}

main();

Output

Before Sort :
 9 2 1 6 -3 7 -2 1 -5 12
After Sort :
 -5 -3 -2 1 1 2 6 7 9 12
#   Python 3 Program
#   Bubble Sort Program
class MySort :
	# Swap the array element
	def swap(self, arr, start, end) :
		temp = arr[start]
		arr[start] = arr[end]
		arr[end] = temp
	
	# Implement bubble sort in given array
	def bubble_sort(self, arr, size) :
		i = 0
		while (i < size) :
			j = 0
			while (j < (size - 1) - i) :
				# Swap array element when next node is small

				if (arr[j] > arr[j + 1]) :
					self.swap(arr, j, j + 1)
				
				j += 1
			
			i += 1
		
	
	# Display array elements
	def print_array(self, arr, size) :
		i = 0
		while (i < size) :
			print(" ", arr[i], end = "")
			i += 1
		
		print("\n", end = "")
	

def main() :
	obj = MySort()
	arr = [9, 2, 1, 6, -3, 7, -2, 1, -5, 12]
	size = len(arr)
	print("Before Sort : \n", end = "")
	obj.print_array(arr, size)
	obj.bubble_sort(arr, size)
	print("After Sort : \n", end = "")
	obj.print_array(arr, size)


if __name__ == "__main__":
	main()

Output

Before Sort :
  9  2  1  6  -3  7  -2  1  -5  12
After Sort :
  -5  -3  -2  1  1  2  6  7  9  12
# Ruby Program
# Bubble Sort Program
class MySort 
	# Swap the array element
	def swap(arr, first, second) 
		temp = arr[first]
		arr[first] = arr[second]
		arr[second] = temp
	end
	# Implement bubble sort in given array
	def bubble_sort(arr, size) 
		i = 0
		while (i < size) 
			j = 0
			while (j < (size - 1) - i) 
				# Swap array element when next node is small

				if (arr[j] > arr[j + 1]) 
					self.swap(arr, j, j + 1)
				end
				j += 1
			end
			i += 1
		end
	end
	# Display array elements
	def print_array(arr, size) 
		i = 0
		while (i < size) 
			print(" ", arr[i])
			i += 1
		end
		print("\n")
	end
end
def main() 
	obj = MySort.new()
	arr = [9, 2, 1, 6, -3, 7, -2, 1, -5, 12]
	size = arr.length
	print("Before Sort  :\n")
	obj.print_array(arr, size)
	obj.bubble_sort(arr, size)
	print("After Sort  :\n")
	obj.print_array(arr, size)
end
main()

Output

Before Sort  :
 9 2 1 6 -3 7 -2 1 -5 12
After Sort  :
 -5 -3 -2 1 1 2 6 7 9 12
/*
  Scala Program
  Bubble Sort Program
*/
class MySort {
	//Swap the array element
	def swap(arr: Array[Int], start: Int, end: Int): Unit = {
		val temp: Int = arr(start);
		arr(start) = arr(end);
		arr(end) = temp;
	}
	//Implement bubble sort in given array
	def bubble_sort(arr: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size) {
			var j: Int = 0;
			while (j < (size - 1) - i) {
				//Swap array element when next node is small

				if (arr(j) > arr(j + 1)) {
					this.swap(arr, j, j + 1);
				}
				j += 1;
			}
			i += 1;
		}
	}
	//Display array elements
	def print_array(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: MySort = new MySort();
		val arr: Array[Int] = Array(9, 2, 1, 6, -3, 7, -2, 1, -5, 12);
		val size: Int = arr.length;
		print("Before Sort : \n");
		obj.print_array(arr, size);
		obj.bubble_sort(arr, size);
		print("After Sort : \n");
		obj.print_array(arr, size);
	}
}

Output

Before Sort :
 9 2 1 6 -3 7 -2 1 -5 12
After Sort :
 -5 -3 -2 1 1 2 6 7 9 12
/*
  Swift Program
  Bubble Sort Program
*/
class MySort {
	//Swap the array element
	func swap(_ arr: inout [Int], _ start: Int, _ end: Int) {
		let temp: Int = arr[start];
		arr[start] = arr[end];
		arr[end] = temp;
	}
	//Implement bubble sort in given array
	func bubble_sort(_ arr: inout [Int], _ size: Int) {
		var i: Int = 0;
		while (i < size) {
			var j: Int = 0;
			while (j < (size - 1) - i) {
				//Swap array element when next node is small

				if (arr[j] > arr[j + 1]) {
					self.swap(&arr, j, j + 1);
				}
				j += 1;
			}
			i += 1;
		}
	}
	//Display array elements
	func print_array(_ arr: [Int], _ size: Int) {
		var i: Int = 0;
		while (i < size) {
			print(" ", arr[i], terminator: "");
			i += 1;
		}
		print("\n", terminator: "");
	}
}
func main() {
	let obj: MySort = MySort();
	var arr: [Int] = [9, 2, 1, 6, -3, 7, -2, 1, -5, 12];
	let size: Int = arr.count;
	print("Before Sort : \n", terminator: "");
	obj.print_array(arr, size);
	obj.bubble_sort(&arr, size);
	print("After Sort : \n", terminator: "");
	obj.print_array(arr, size);
}
main();

Output

Before Sort :
  9  2  1  6  -3  7  -2  1  -5  12
After Sort :
  -5  -3  -2  1  1  2  6  7  9  12


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