Bubble sort in Java

Bubble sort in java is one of the sorting algorithms used to sort the elements as per our wish. Sorting algorithms help in rearranging the list of elements or an array into ascending, descending or required form. We use comparison operator to decide the new order of the list of elements. For example if we have a list of such as: A5, A1, A4, A3, A2 and we have to arrange this list into ascending order. The order will be A1>A2>A3>A4>A5 if sorted in ascending order.

There are many algorithms in java which perform this rearrangement such as bubble sort, bucket sort, merge sort, radix sort, insertion sort, selection sort, heap sort, quick sort, counting sort, comb sort etc. Bubble sort is one of the simplest sorting algorithms amongst all these. We will traverse the array from the first elements to the last element In this algorithm. we will put the current element in comparison with the next element and swap both if the current one is greater than the next element.

Here is a step wise process of how bubble sort is performed; say we have to sort the list in ascending order

Step 1: we take a list of unsorted elements or an array

For example the list is: 16, 43, 22,78

Step 2: bubble sort takes the first 2 elements 16, 43 and will check for the bigger elements. In this case, 43 is the bigger one and they are already in the sorted locations so the pointer will move forward.

Step 3: now we compare 43 and 22 where 22 is smaller than 43 so the positions will be swapped. Now the list becomes 16, 22, 43, 78

Step 4: As we compare 43 and 78 now, it is again in the sorted fashion and doesn’t require to be swapped.

Step 5: As a result, we get out final list of sorted elements: 16, 22, 43,78

For better understanding, here is a simple bubble sort program in Java:

class DeveloperHelps
{
    void bubbleSort(int arr[])
    {
        int x = arr.length;
        for (int i = 0; i < x-1; i++)
            for (int j = 0; j < x-i-1; j++)
                if (arr[j] > arr[j+1])
                {
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
    }
 
    void printArray(int arr[])
    {
        int x = arr.length;
        for (int i=0; i<x; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
 
 
    public static void main(String args[])
    {
        DeveloperHelps obj = new DeveloperHelps();
        int arr[] = {100, 300, 200, 500, 220, 601, 90};
        obj.bubbleSort(arr);
        System.out.println("The sorted array via bubble sort algorithm is");
        obj.printArray(arr);
    }
}

The output of this program will be:

The sorted array via bubble sort algorithm is 
90, 100, 200, 220, 300, 500, 601

The worst case and average case complexity of bubble sort algorithm is O(n^2), hence this algorithm is not very suitable for large datasets. Here n refers to the number of elements in the list. Worst case is the scenario when the list of elements is sorted reverse and best case scenario is when the list of elements is already sorted. Auxiliary space of bubble sort is O(1).

Leave a comment

Your email address will not be published. Required fields are marked *