Java Program for Recursive Bubble Sort



In this article, we will learn to implement Bubble Sort using recursion in Java. Our goal is to understand how recursion can be applied to the Bubble Sort algorithm to achieve the same sorting effect as its iterative counterpart.

Problem Statement

Write a Java program to sort an array of integers using the recursive approach of Bubble Sort.

Input

my_arr[] = {45, 67, 89, 31, 63, 0, 21, 12}

Output

The array after implementing bubble sort is
[0, 12, 21, 31, 45, 63, 67, 89]

Bubble Sort Algorithm

Bubble sort algorithm is a simple sorting algorithm to sort elements. It compares each pair of items in the list and swaps them if they're not in the right order.

Algorithm: Sequential-Bubble-Sort (A)
fori ? 1 to length [A] do
for j ? length [A] down-to i +1 do
   if A[A] < A[j-1] then
      Exchange A[j] ? A[j-1]

Steps for the recursive approach of Bubble sort

Following are the steps to sort an array of integers using the recursive approach of Bubble Sort ?

  • First we will start by importing the Arrays class from the java.util package.
  • Define the class name Demo and inside that demo class define a static method named as bubble_sort.
  • A for loop runs through the array, comparing adjacent elements if an element is greater than the next one, the two components are swapped.
  • As the main method is the entry point of the program it will initialize an array with the given values and call the bubble_sort method.
  • At the end when the sorting is completed it prints the sorted array.

Java program for the recursive approach of Bubble sort

Below is the Java program for the recursive approach of Bubble sort ?

import java.util.Arrays;
public class Demo{
   static void bubble_sort(int my_arr[], int len_arr){
      if (len_arr == 1)
      return;
      for (int i=0; i<len_arr-1; i++)
      if (my_arr[i] > my_arr[i+1]){
         int temp = my_arr[i];
         my_arr[i] = my_arr[i+1];
         my_arr[i+1] = temp;
      }
      bubble_sort(my_arr, len_arr-1);
   }
   public static void main(String[] args){
      int my_arr[] = {45, 67, 89, 31, 63, 0, 21, 12};
      bubble_sort(my_arr, my_arr.length);
      System.out.println("The array after implementing bubble sort is ");
      System.out.println(Arrays.toString(my_arr));
   }
}

Output

The array after implementing bubble sort is
[0, 12, 21, 31, 45, 63, 67, 89]

Code Explanation

A function named Demo contains the function to perform bubble sort. If the length of the array is 1, then the array is returned. Otherwise, the array is iterated over and if the element at the first place is greater than the element at the next position, the elements are swapped.

After the first pass, the largest element would have been fixed, and the bubble sort is called on all elements except the largest once. In the main function, the array is defined and it is passed as a parameter to the bubble sort function.

Updated on: 2024-08-27T18:48:46+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements