Bubble Sort Java Algorithm | JavaSorting

Total Views : 55
Zoom In Zoom Out Read Later Print

we will discuss two different ways to implement bubble sorting In Java Application. The project download URL has been added so that you can easily execute the given program, with appropriate examples and samples outputs added for bubble sort In Java.

Here we are using two ways for sorting but they are not different from each other. Only difference is how we get input from the user and store it. Sorting of the element will be done in same way.

We will use following 2 ways to implement Bubble Sort in Java.

1. Using Buffered Reader.
2. Using Array.

Bubble Sort Using Buffered Reader

SortBufferReader.java

package com.manjeet.prodevsblog.bubble;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SortBufferReader {

    public static void main(String[] args) throws IOException {

        System.out.println("Sorting Using Buffered Reader....");

        int n, i;
        BufferedReader b = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter Number of Elements (n):");
        n = Integer.parseInt(b.readLine());
        int[] array = new int[n];

        System.out.println("Enter "+n+" Elements (Press Enter After Each Element) ");

        for( i = 0; i < n; i++) {
            array[i] = Integer.parseInt(b.readLine());
        }

        System.out.println( "Elements in Array Before Sorting...");
        printArray(array);

        sortArray(array);

        System.out.println( "Elements in Array After Sorting");
        printArray(array);
    }

    public static void sortArray(int[] array) {
        int n = array.length, i, j, p, temp;
        for (i = 0;i < n-1; i++) {

            for (j=0; j<n-i-1; j++) {
                if(array[j+1] < array[j]) {
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

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

Output:

Sorting Using Buffered Reader....
Enter Number of Elements (n):5
Enter 5 Elements (Press Enter After Each Element) 
2
78
33
1
44
Elements in Array Before Sorting...
2 78 33 1 44 
Elements in Array After Sorting
1 2 33 44 78 

Process finished with exit code 0

Bubble Sort Using Array & Scanner

SortArray.java

package com.manjeet.prodevsblog.bubble;

import java.util.Scanner;

public class SortArray {

    public static void main(String[] args) {
        
        System.out.println("Sorting Using Array & Scanner....");

        int n, i;
        Scanner s = new Scanner(System.in);

        System.out.print("Enter Number of Elements (n):");
        n = s.nextInt();
        int[] array = new int[n];

        System.out.println("Enter "+n+" Elements (Press Enter After Each Element) ");

        for( i = 0; i < n; i++) {
            array[i] = s.nextInt();
        }

        System.out.println( "Elements in Array Before Sorting...");
        printArray(array);

        sortArray(array);

        System.out.println( "Elements in Array After Sorting");
        printArray(array);
    }

    public static void sortArray(int[] array) {
        int n = array.length, i, j, p, temp;
        for (i = 0;i < n-1; i++) {

            for (j=0; j<n-i-1; j++) {
                if(array[j+1] < array[j]) {
                    temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = temp;
                }
            }
        }
    }

    public static void printArray(int[] array) {
        for (int value : array) {
            System.out.print(value + ", ");
        }
        System.out.println();
    }
}

Output:

Sorting Using Array & Scanner....
Enter Number of Elements (n):5
Enter 5 Elements (Press Enter After Each Element) 
9
8
7
6
5
Elements in Array Before Sorting...
9, 8, 7, 6, 5, 
Elements in Array After Sorting
5, 6, 7, 8, 9, 

Process finished with exit code 0

Sample Loop Iteration :

Elements in Array Before Sorting...
9, 8, 7, 6, 5, 
Loop 0 Array Start----------------
8, 9, 7, 6, 5, 
8, 7, 9, 6, 5, 
8, 7, 6, 9, 5, 
8, 7, 6, 5, 9, 
Array After Loop 0----------------
8, 7, 6, 5, 9, 
Loop 0 Array End----------------
Loop 1 Array Start----------------
7, 8, 6, 5, 9, 
7, 6, 8, 5, 9, 
7, 6, 5, 8, 9, 
Array After Loop 1----------------
7, 6, 5, 8, 9, 
Loop 1 Array End----------------
Loop 2 Array Start----------------
6, 7, 5, 8, 9, 
6, 5, 7, 8, 9, 
Array After Loop 2----------------
6, 5, 7, 8, 9, 
Loop 2 Array End----------------
Loop 3 Array Start----------------
5, 6, 7, 8, 9, 
Array After Loop 3----------------
5, 6, 7, 8, 9, 
Loop 3 Array End----------------
Elements in Array After Sorting
5, 6, 7, 8, 9, 

1. The numbers enterd by the user will be stored in the Array array[].

2. The function printArray() prints the array elements, function sortArray() will sort the array elements using bubble sort.

3. For each iteration of i, the inner loop compare the adjacent numbers and moves the large number towards the right and the large number will be placed in the proper position.

4. The given series is 9, 8, 7, 6, 5.

5. For the first iteration of i

5.a. The inner loop will compare 9,8. 9 is the large number compared with 8, so 9 shifted to the right. Now the series is 8, 9, 7, 6, 5.

5.b. Compare 9 with 7 and shift 9 to right. The series is 8, 7, 9, 6, 5.

5.c. Compare 9 with 6 and shift 9 to right. The series is 8, 7, 6, 9, 5.

5.d. Compare 9 with 5 and shift 9 to right. The series is 8, 7, 6, 5, 9.

5.e After 5st iteration, 9 will be shifted to the last index. Now the series is 8, 7, 6, 5, 9.

6. For the 6nd iteration of i

6.a. The inner loop compares 8,7 and shift 8 to right. The series is 7, 8, 6, 5, 9.

6.b. Compare 8,6 and shift 8 to right, series is 7, 6, 8, 5, 9.

6.c. Compare 8,5 and shift 8 to right, series is 7, 6, 5, 8, 9.

7. For the 7rd iteration of i

7.a. The inner loop will compare 7,6 and shift the number 7 to right, series is 6, 7, 5, 8, 9.

7.b. Compare 7 with 5 ,shift 7 to right, series is 6, 5, 7, 8, 9.

8. For 8th iteration

8.a. Inner loop compares 6,5 and shifts 6 to right, the series is 5, 6, 7, 8, 9.

After all iterations, the sorted array is 5, 6, 7, 8, 9.

You can download the Complete project from below link.

See More

Latest Photos