How to write Bubble Sort in Java ?

DeeKay September 16, 2014 at 12:58 am · JAVA API, sorting

We all have learnt sorting techniques in our college academic curriculum. Out of those sorting techniques the simplest technique we learnt is Bubble Sort Implementation in Java. The Bubble sort takes an array of numbers and sort them within the array indexes in either ascending or descending order. Bubble sort is simplest of all sorting techniques but it not efficient as other sorting methods.

Bubble Sort Implementation in Java

As the name suggest in Bubble sort smallest or largest number, based on whether you want o sort ascending or descending order, it bubbles up the number towards start or end of array. We will need “N” numbers of iterations through the array, where “N” is the length of array, to sort an array of numbers. At the end of each iteration one number is placed at its proper order.

First thing you need to do it in Bubble Sort is to create an array of numbers. You can create array of 2 numbers or thousands of numbers and can start sorting by taking first number in the array and comparing it with the number adjacent to it and start swapping if the number is less than or greater than based on the order of sorting i.e ascending or descending. You can either start comparing from the start or end of the array. In our example we are comparing from start of the element.

Below picture shows you first iteration of sorting an element in the array. As it is always said that picture speaks more than the thousand words.
Bubble Sorting

Java code shows example for Bubble sort in ascending and descending order

/****************************************************************************************
 * Created on Sept 15, 2014 Copyright(c) http://kodehelp.com All Rights Reserved.
 ****************************************************************************************/
package com.kodehelp.java;

import java.lang.reflect.Array;
import java.util.Arrays;

/**
 * Created by http://kodehelp.com
 */
public class BubbleSortExample {

    public static void main(String args[]){
        int [] numbers = {5,9,8,2,6,3,7,10};
        print(numbers);
        int[] ascNumbers = sortAscending(numbers);
        print(ascNumbers);
        int[] dscNumbers = sortDescending(numbers);
        print(dscNumbers);

    }

    public static void print(int[] numbers){
        for (int i:numbers) {
            System.out.print(i + " ");
        }
        System.out.println(" ");
    }

    public static int[] sortAscending(int[] numbers){
        int temp;
        for (int i = 0; i < numbers.length ; i++) {
            for (int j = 0; j <numbers.length-1 ; j++) {
                if(numbers[j]>numbers[j+1]) {
                    temp = numbers[j + 1];
                    numbers[j + 1] = numbers[j];
                    numbers[j] = temp;
                }
            }
        }
        return numbers;
    }

    public static int[] sortDescending(int[] numbers){
        int temp;
        for (int i = 0; i <numbers.length ; i++) {
            for (int j = 0; j < numbers.length - 1; j++) {
                if (numbers[j] < numbers[j + 1]) {
                    temp = numbers[j + 1];
                    numbers[j + 1] = numbers[j];
                    numbers[j] = temp;
                }
            }
        }
        return numbers;
    }
}