Bubble Sort In Java

Sorting is the algorithm that can put the elements of a list in a certain order. In fact, the Sorting is used to specify the most used orders such as numerical order and lexicographical order. Rather, this algorithm is important for optimizing the use of other algorithms which includes input data that are to be sorted list. Most often, it is used for canonicalizing the data and for producing human-readable output. Most probably, it can be used to satisfy two conditions such as,

  1. The output is in nondecreasing order
  2. The output is a permutation of the input

Bubble Sorting:

This is an alternate way of putting the largest element at the highest index in the array uses an algorithm called bubble sort. Bubble sort is the most often used sorting technique. This method compares the two consecutive elements of the list and if they are not in order the two elements will be interchanged immediately. if they are in order the two elements will not be interchanged. this process continues for (N-1) times for sorting an array of size N.

Algorithm :

Input : a[] is the array of elements

output: a[0] <= a[1] <= -------- <= a[n-1]

for(i=0; i < n-1;i++)
     for(j=0;j<n-i-1;j++)
         if(a[i] > a[j+1])
            swap (a[j],a[j+1])
         end if
     end for
end for

after the first phase, the largest element will be in the Nth position. in the second phase the second largest element in the N-1th position and so on.. thus in each pass an element an element with the largest value in the remaining unsorted element will be “bubble up” and hence the name is bubble sort.

[java]

import java.util.Scanner;

public class BubbleSortDemo {

    int a[];
    int n;

    public BubbleSortDemo(int size) {
        n = size;
        a = new int[n];
    }

    public void readArray() {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter " + n + " Elements");
        for (int i = 0; i < n; i++) {
            a[i] = s.nextInt();
        }
    }

    public void showArray() {
        System.out.println("");
        for (int i = 0; i < n; i++) {
            System.out.print(a[i] + "  ");
        }
        System.out.println("");
    }

    public void bubbleSort() {
        for (int i = 0; i < n – 1; i++) {
            for (int j = 0; j < n – i – 1; j++) {
                if (a[j] > a[j + 1]) {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
            System.out.println("After " + (i + 1) + " Pass");
            showArray();
        }
    }

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter Array Size : ");
        int n = s.nextInt();
        BubbleSortDemo bubbleSortDemo = new BubbleSortDemo(n);
        bubbleSortDemo.readArray();
        System.out.println("Before Bubble Sort");
        bubbleSortDemo.showArray();
        bubbleSortDemo.bubbleSort();
        System.out.println("After Bubble Sort");
        bubbleSortDemo.showArray();

    }

}

[/java]

Output :

Enter Array Size : 5 
Enter 5 Elements : 9 5 1 6 0 
Before Bubble Sort : 9  5  1  6  0 
After 1 Pass : 5  1  6  0  9 
After 2 Pass : 1  5  0  6  9 
After 3 Pass : 1  0  5  6  9 
After 4 Pass : 0  1  5  6  9 
After Bubble Sort : 0  1  5  6  9

Happy Learning 🙂