# Bubble Sort in JAVA

IN this tutorial we will learn how to implement **bubble sort in Java with** the explanation.

**‘Sorting’ **in programming refers to the proper arrangement of the elements of an array (in ascending or descending order).

*Note: ‘array’ is a collection of variables of the same data type which are accessed by a single name.*

**‘Bubble Sort’** uses the following algorithm to sort the elements of an array:

*let the array be -> {1,5,3,2}*

- compare the first 2 elements and then swap their positions if the element at the 2
^{nd}position is smaller (or greater) than the element at the 1^{st}position, to sort in ascending (or descending) order.

**1<5**, so array -> {**1,5**,3,2} - repeat step 1 for the elements at the 2
^{nd}and the 3^{rd}position.

**3<5**, so array -> {1,**3,5**,2} - repeat step 1 for all the other consecutive-pair elements of the array.

**2<5**, so array -> {1,3,**2,5**} - reduce the effective length of the array by 1 and go back to step 1 if at least one comparison can still be made.

**5**is now ignored as the effective length is reduced by 1

**1<3**, so array -> {**1,3**,2,5}

**2<3**, so array -> {1,**2,3**,5}

**3**is now ignored as the effective length is reduced by 1 again

**1<2**, so array -> {**1,2**,3,5}

**2**is now ignored as the effective length is reduced by 1 again

*Since no further comparisons can be made, the sorted array is ->***{1,2,3,5}**

## Code to implement Bubble Sort in Java

import java.util.Scanner; public class BubbleSort { void inputArray(int array[],int length) { Scanner scan=new Scanner(System.in); System.out.print("Input "+length+" (integer) elements: "); //note, in the for loop, the intitializing statement is decreasing the value of length by 1 because in arrays, counting starts from 0 for(length--;length>=0;length--){ array[length]=scan.nextInt(); } } void printArray(int array[],int length) { System.out.print("The elements of the given array are: "); for(int i=0;i<length;i++)System.out.print(array[i]+" "); } void bubbleSort(int array[], int length) { for(int i = 0; i < length-1; i++){ //note, before every 'i' iteration of the first for loop, the 'i' number of elements from the end are perfectly sorted, so no need to execute the second for loop for an extra 'i' number of times for(int j=0;j<length-i-1;j++) if(array[j]>array[j+1]){//swapping array[j]+=array[j+1]; array[j+1]=array[j]-array[j+1]; array[j]-=array[j+1]; } } } public static void main(String args[]) { BubbleSort ob = new BubbleSort(); int array[]=new int[4];//creating an integer array ob.inputArray(array,4);//taking input in the array ob.bubbleSort(array,4);//sorting the array with the desired sorting algorithm ob.printArray(array,4);//printing the array after sorting } }//class ends

**Output:**

Input 4 (integer) elements: 1 5 3 2 The elements of the given array are: 1 2 3 5

### Summary: Bubble sort

- an
**object ‘ob’**of the class is created to call the required functions - an array
**‘array[]’ is initialized**with length 4 **ob.inputArray(int[],int)**is called to take input integer elements from the user and store them in the array**ob.bubbleSort(int[],int)**is called to sort the elements of the array with the algorithm explained above**ob.printArray(int[],int)**is called to print the elements of the array (after sorting)

*Note:*

*the value ‘4’ can be changed to any other value in order to get the desired array-length**since (in JAVA) arrays are passed by reference, therefore the changes made in bubbleSort(int[],int) stay permanently**to sort in descending order, just change the sign from ‘>’ to ‘<‘ in the logical condition inside the 2*^{nd}for loop

Also learn:

## Leave a Reply