Java Program to Implement Bubble Sort algorithm

program to implement bubble sort in java

What is  a Sorting?

In this Article, we will write a program to implement bubble sort algorithm in Java.
Sorting algorithms are used to rearrange the items in a list or array so that they are in a specific order. There are many different sorting algorithms, each with its own set of trade-offs in terms of efficiency, speed, and complexity. Some common sorting algorithms include bubble sort, insertion sort, selection sort, merge sort, quick sort, and heap sort.

Sorting in Java :

In Java, there are several ways to sort elements:

  • Using the sort() method of the java.util.Arrays class: This method sorts the elements of an array in ascending order. It can be used to sort arrays of primitive data types (such as int, char, etc.) as well as arrays of objects (such as String, etc.).
  • Using the Collections.sort() method: This method sorts the elements of a List in ascending order. It can be used to sort Lists of objects (such as ArrayList, LinkedList, etc.).
  • Using a sorting algorithm: There are several sorting algorithms that you can use to sort elements in Java. Some of the commonly used sorting algorithms are:
    1. Bubble sort
    2. Insertion sort
    3. Selection sort
    4. Merge sort
    5. Quick sort

Example:

int[] numbers = {5, 2, 7, 1, 3};
Arrays.sort(numbers);

// The array is now sorted and will be [1, 2, 3, 5, 7]

Bubble Sort :

Bubble sort is a simple sorting algorithm that repeatedly iterates through a list of elements, compares adjacent elements, and swaps them if they are in the wrong order. It is called bubble sort because the elements “bubble” up to the top of the list like bubbles in a glass of soda.

Pseudo Code for Bubble Sort:

public static void bubbleSort(int[] array) {
  // Initializing ans variable
  boolean sorted = false;
  int temp;

  // loop to sort the array
  while (!sorted) {
    sorted = true;
    for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i + 1]) {
            temp = array[i];
            array[i] = array[i + 1];
            array[i + 1] = temp;
            sorted = false;
        }
    }
  }
}

Java Program to Implement Bubble Sort algorithm:

Run

import java.util.*;

public class Main{
    public static void bubbleSort(int[] array){
        // Flag to track if the list is already sorted
        boolean sorted = false;
        // Loop through the list until it is sorted
        while (!sorted) {
            sorted = true;
            // Loop through the list and compare adjacent items
            for (int i = 0; i < array.length - 1; i++) { if (array[i] > array[i + 1]) {
                    // Swap the items if they are in the wrong order
                    int temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                    // Set the sorted flag to false
                    sorted = false;
                }
            }
        }
    }
 
    public static void main(String[] args) {
        int[] array = {5, 2, 4, 1, 3};
        System.out.print("Array Before Sorting is : ");
        for(int i = 0;i < array.length;i++){
            System.out.print(array[i] + " ");
        }
        System.out.println();
        System.out.print("Array After Sorting is : ");
        bubbleSort(array);
        // Print the sorted array
        for (int i : array) {
            System.out.print(i + " ");
        }
    }
}
Output:

Array Before Sorting is : 5 2 4 1 3 
Array After Sorting is : 1 2 3 4 5

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription