Site icon

Sort integer array using bubble sort algorithm in java (example)

Algorithm to sort given input array using bubble sort

  1. Given an unsorted array of length n.
  2. Start iterating through the array
  3. There will be two loops to sort array using bubble sort algorithm.
    • Outer loop iterate from 0 to n – 1, Let outerindex be its index.
    • Inner loop will iterate from 0 to n – outerIndex – 1, Let innerindex be its index.
    • Compare the adjacent elements of array
      • If current element is larger than next element
        • Then swap element, so that elements are in sorted order
      • Else, no need to do anything, as elements are already sorted.
  4. Once we are out of loops, we will get sorted array.

Example – sort integer array using bubble sort algorithm in java

Fig 2: Compare first pair of element (10 and 7 elements)
Fig 3: Compare second pair of elements (10 and 25)
Fig 4: Compare third pair of elements (25 and 20 elements)
Fig 5: Compare fourth pair of elements (25 and 12 elements)
Fig 6: Compare fourth pair of elements (25 and 9 elements)
Fig 7: Bubble sort iterations

Why it is called bubble sort ?

Complexity of bubble sort algorithm

S. No.Cases  Complexity
1Best case O(n^2)
2Average case O(n^2)
3Worst case O(n^2)

Program – Sort array using bubble sort algorithm in java

private static void bubbleSort(int[] arr) {
		for (int outterIndex = 0; outterIndex < arr.length; outterIndex++) {
			for (int innerIndex = 0; innerIndex < arr.length - outterIndex - 1; innerIndex++) {

				if (arr[innerIndex] > arr[innerIndex + 1]) {
					swap(arr, innerIndex);
				}
			}
		}
		System.out.println(Arrays.toString(arr));
	}
	private static void improvedBubbleSort(int[] arr) {
		boolean isSwapHappened = false;
		for (int outterIndex = 0; outterIndex < arr.length; outterIndex++) {
			isSwapHappened = false;
			for (int innerIndex = 0; innerIndex < arr.length - outterIndex - 1; innerIndex++) {

				if (arr[innerIndex] > arr[innerIndex + 1]) {
					swap(arr, innerIndex);
					isSwapHappened = true;
				}
			}
			if (isSwapHappened == false) {
				// Now array is sorted, no need to swap now
				System.out.println("No swap happened for current iteration");
				break;
			}
		}
		System.out.println(Arrays.toString(arr));
	}

Program – sort given array using bubble sort algorithm in java 

package org.learn.arrays;

import java.util.Arrays;

public class BubbleSort {

	public static void main(String[] args) {
		int[] arr = { 10, 7, 25, 20, 12, 9 };
		System.out.println("Input array : " + Arrays.toString(arr));
		System.out.println("Sorting using bubble sort :");
		bubbleSort(arr);
		System.out.println("Sorting using improved Bubble sort :");
		// Take an example of partially sorted array
		int[] partiallySortedArray = { 25, 20, 12, 7, 9, 10 };
		improvedBubbleSort(partiallySortedArray);
	}

	private static void bubbleSort(int[] arr) {
		for (int outterIndex = 0; outterIndex < arr.length; outterIndex++) {
			for (int innerIndex = 0; innerIndex < arr.length - outterIndex - 1; innerIndex++) {

				if (arr[innerIndex] > arr[innerIndex + 1]) {
					swap(arr, innerIndex);
				}
			}
		}
		System.out.println(Arrays.toString(arr));
	}

	private static void improvedBubbleSort(int[] arr) {
		boolean isSwapHappened = false;
		for (int outterIndex = 0; outterIndex < arr.length; outterIndex++) {
			isSwapHappened = false;
			for (int innerIndex = 0; innerIndex < arr.length - outterIndex - 1; innerIndex++) {

				if (arr[innerIndex] > arr[innerIndex + 1]) {
					swap(arr, innerIndex);
					isSwapHappened = true;
				}
			}
			if (isSwapHappened == false) {
				// Now array is sorted, no need to swap now
				System.out.println("No swap happened for current iteration");
				break;
			}
		}
		System.out.println(Arrays.toString(arr));
	}

	private static void swap(int[] arr, int index) {
		int temp = arr[index];
		arr[index] = arr[index + 1];
		arr[index + 1] = temp;
	}
}

Output – sort array using bubble sort algorithm in java

Input array : [10, 7, 25, 20, 12, 9]
Sorting using bubble sort :
[7, 9, 10, 12, 20, 25]
Sorting using improved Bubble sort :
No swap happened for current iteration
[7, 9, 10, 12, 20, 25]
Exit mobile version