Recursive Bubble Sort
Bubble sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares adjacent elements and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.
The Recursive Bubble Sort is a variation of the traditional Bubble Sort algorithm. Bubble Sort is a simple comparison-based sorting algorithm that repeatedly steps through the list to be sorted, compares adjacent elements, and swaps them if they are in the wrong order. The process is repeated until the entire list is sorted.
Recursive Bubble Sort, as the name suggests, employs a recursive approach to perform the sorting. In the recursive version, we perform one pass of the Bubble Sort and then call the recursive function on the reduced subarray without the last element (since it is already in its correct position). This process continues until the entire array is sorted.
Problem Statement
Given an array of integers, we want to sort it in ascending order using Recursive Bubble Sort.
Example
Let's take an example to illustrate the Recursive Bubble Sort process:
Given Array: [5, 2, 8, 1, 6]
- Initial array: [5, 2, 8, 1, 6]
- Recursive pass 1: [2, 5, 1, 6, 8]
- Recursive pass 2: [2, 1, 5, 6, 8]
- Recursive pass 3: [1, 2, 5, 6, 8]
The array is now sorted in ascending order.
Pseudocode
The pseudocode for Recursive Bubble Sort is as follows:
// Function for Recursive Bubble Sort
recursive_bubble_sort(record[], size):
if size <= 0:
return
for i = 0 to size - 2:
if record[i] > record[i + 1]:
swap record[i] and record[i + 1]
recursive_bubble_sort(record, size - 1)
Algorithm Explanation:
- Start with the given array and its size.
- Call the
recursive_bubble_sort
function with the array and its size. - In the
recursive_bubble_sort
function, check if the size of the array is less than or equal to 0. If so, return (base case). - Run a loop from 0 to size - 2 (size - 1 is excluded to avoid out-of-bounds access).
- Inside the loop, compare adjacent elements at indices i and i + 1. If the current element is greater than the next element, swap them.
- After completing one pass of the Bubble Sort, call the
recursive_bubble_sort
function recursively with the array and size - 1, effectively excluding the last element from further sorting. - The recursion continues until the size becomes 1, and the array is sorted.
Explanation of the Code:
- The code starts with the
main
function where the initial arrayrecord
is defined. - The size of the array is calculated using
sizeof(record) / sizeof(record[0])
. - The
display
function is defined to print the elements of the array. - The unsorted array is displayed before sorting using
display(record, size)
. - The
bubble_sort
function is called to sort the array. - The sorted array is displayed after sorting using
display(record, size)
.
Program Solution
//C Program
//Recursive Bubble Sort
#include <stdio.h>
//Display array elements
void display(int record[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", record[i]);
}
printf("\n");
}
//Perform bubble sort in given array
void bubble_sort(int record[], int size)
{
if (size <= 0)
{
//When have no more than one element
return;
}
int temp = 0;
for (int i = 0; i < size - 1; ++i)
{
//Compare two adjacent elements
if (record[i] > record[i + 1])
{
//When need to swap two adjacent elements
temp = record[i];
record[i] = record[i + 1];
record[i + 1] = temp;
}
}
bubble_sort(record, size - 1);
}
int main()
{
//Assume given array elements
int record[] = {
2 , 8 , 1 , 5 , 0 , 9 , 3 , 7 , 2
};
//Get the size of array
int size = sizeof(record) / sizeof(record[0]);
//Before sort
display(record, size);
bubble_sort(record, size);
//After sort
display(record, size);
return 0;
}
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
//Java Program
//Recursive Bubble Sort
class MySort
{
//Display array elements
public void display(int[] record, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + record[i]);
}
System.out.print("\n");
}
//Perform bubble sort in given array
public void bubble_sort(int[] record, int size)
{
if (size <= 0)
{
//When have no more than one element
return;
}
int temp = 0;
for (int i = 0; i < size - 1; ++i)
{
//Compare two adjacent elements
if (record[i] > record[i + 1])
{
//When need to swap two adjacent elements
temp = record[i];
record[i] = record[i + 1];
record[i + 1] = temp;
}
}
bubble_sort(record, size - 1);
}
public static void main(String[] args)
{
MySort obj = new MySort();
//Assume given array elements
int[] record = {
2,
8,
1,
5,
0,
9,
3,
7,
2
};
//Get the size of array
int size = record.length;
//Before sort
obj.display(record, size);
obj.bubble_sort(record, size);
//After sort
obj.display(record, size);
}
}
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
//Include header file
#include <iostream>
using namespace std;
//C++ Program
//Recursive Bubble Sort
class MySort
{
public:
//Display array elements
void display(int record[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << record[i];
}
cout << "\n";
}
//Perform bubble sort in given array
void bubble_sort(int record[], int size)
{
if (size <= 0)
{
//When have no more than one element
return;
}
int temp = 0;
for (int i = 0; i < size - 1; ++i)
{
//Compare two adjacent elements
if (record[i] > record[i + 1])
{
//When need to swap two adjacent elements
temp = record[i];
record[i] = record[i + 1];
record[i + 1] = temp;
}
}
this->bubble_sort(record, size - 1);
}
};
int main()
{
MySort obj = MySort();
int record[] = {
2 , 8 , 1 , 5 , 0 , 9 , 3 , 7 , 2
};
//Get the size of array
int size = sizeof(record) / sizeof(record[0]);
//Before sort
obj.display(record, size);
obj.bubble_sort(record, size);
//After sort
obj.display(record, size);
return 0;
}
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
//Include namespace system
using System;
//C# Program
//Recursive Bubble Sort
class MySort
{
//Display array elements
public void display(int[] record, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + record[i]);
}
Console.Write("\n");
}
//Perform bubble sort in given array
public void bubble_sort(int[] record, int size)
{
if (size <= 0)
{
//When have no more than one element
return;
}
int temp = 0;
for (int i = 0; i < size - 1; ++i)
{
//Compare two adjacent elements
if (record[i] > record[i + 1])
{
//When need to swap two adjacent elements
temp = record[i];
record[i] = record[i + 1];
record[i + 1] = temp;
}
}
bubble_sort(record, size - 1);
}
public static void Main(String[] args)
{
MySort obj = new MySort();
int[] record = {
2 , 8 , 1 , 5 , 0 , 9 , 3 , 7 , 2
};
//Get the size of array
int size = record.Length;
//Before sort
obj.display(record, size);
obj.bubble_sort(record, size);
//After sort
obj.display(record, size);
}
}
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
<?php
//Php Program
//Recursive Bubble Sort
class MySort
{
//Display array elements
public function display( $record, $size)
{
for ($i = 0; $i < $size; ++$i)
{
echo " ". $record[$i];
}
echo "\n";
}
//Perform bubble sort in given array
public function bubble_sort( & $record, $size)
{
if ($size <= 0)
{
//When have no more than one element
return;
}
$temp = 0;
for ($i = 0; $i < $size - 1; ++$i)
{
//Compare two adjacent elements
if ($record[$i] > $record[$i + 1])
{
//When need to swap two adjacent elements
$temp = $record[$i];
$record[$i] = $record[$i + 1];
$record[$i + 1] = $temp;
}
}
$this->bubble_sort($record, $size - 1);
}
}
function main()
{
$obj = new MySort();
//Assume given array elements
$record = array(2, 8, 1, 5, 0, 9, 3, 7, 2);
//Get the size of array
$size = count($record);
//Before sort
$obj->display($record, $size);
$obj->bubble_sort($record, $size);
//After sort
$obj->display($record, $size);
}
main();
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
//Node Js Program
//Recursive Bubble Sort
class MySort
{
//Display array elements
display(record, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + record[i]);
}
process.stdout.write("\n");
}
//Perform bubble sort in given array
bubble_sort(record, size)
{
if (size <= 0)
{
//When have no more than one element
return;
}
var temp = 0;
for (var i = 0; i < size - 1; ++i)
{
//Compare two adjacent elements
if (record[i] > record[i + 1])
{
//When need to swap two adjacent elements
temp = record[i];
record[i] = record[i + 1];
record[i + 1] = temp;
}
}
this.bubble_sort(record, size - 1);
}
}
function main()
{
var obj = new MySort();
//Assume given array elements
var record = [2, 8, 1, 5, 0, 9, 3, 7, 2];
//Get the size of array
var size = record.length;
//Before sort
obj.display(record, size);
obj.bubble_sort(record, size);
//After sort
obj.display(record, size);
}
main();
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
# Python 3 Program
# Recursive Bubble Sort
class MySort :
# Display array elements
def display(self, record, size) :
i = 0
while (i < size) :
print(" ", record[i], end = "")
i += 1
print("\n", end = "")
# Perform bubble sort in given array
def bubble_sort(self, record, size) :
if (size <= 0) :
# When have no more than one element
return
temp = 0
i = 0
while (i < size - 1) :
# Compare two adjacent elements
if (record[i] > record[i + 1]) :
# When need to swap two adjacent elements
temp = record[i]
record[i] = record[i + 1]
record[i + 1] = temp
i += 1
self.bubble_sort(record, size - 1)
def main() :
obj = MySort()
# Assume given array elements
record = [2, 8, 1, 5, 0, 9, 3, 7, 2]
# Get the size of array
size = len(record)
# Before sort
obj.display(record, size)
obj.bubble_sort(record, size)
# After sort
obj.display(record, size)
if __name__ == "__main__": main()
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
# Ruby Program
# Recursive Bubble Sort
class MySort
# Display array elements
def display(record, size)
i = 0
while (i < size)
print(" ", record[i])
i += 1
end
print("\n")
end
# Perform bubble sort in given array
def bubble_sort(record, size)
if (size <= 0)
# When have no more than one element
return
end
temp = 0
i = 0
while (i < size - 1)
# Compare two adjacent elements
if (record[i] > record[i + 1])
# When need to swap two adjacent elements
temp = record[i]
record[i] = record[i + 1]
record[i + 1] = temp
end
i += 1
end
self.bubble_sort(record, size - 1)
end
end
def main()
obj = MySort.new()
# Assume given array elements
record = [2, 8, 1, 5, 0, 9, 3, 7, 2]
# Get the size of array
size = record.length
# Before sort
obj.display(record, size)
obj.bubble_sort(record, size)
# After sort
obj.display(record, size)
end
main()
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
//Scala Program
//Recursive Bubble Sort
class MySort
{
//Display array elements
def display(record: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + record(i));
i += 1;
}
print("\n");
}
//Perform bubble sort in given array
def bubble_sort(record: Array[Int], size: Int): Unit = {
if (size <= 0)
{
//When have no more than one element
return;
}
var temp: Int = 0;
var i: Int = 0;
while (i < size - 1)
{
//Compare two adjacent elements
if (record(i) > record(i + 1))
{
//When need to swap two adjacent elements
temp = record(i);
record(i) = record(i + 1);
record(i + 1) = temp;
}
i += 1;
}
bubble_sort(record, size - 1);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MySort = new MySort();
//Assume given array elements
var record: Array[Int] = Array(2, 8, 1, 5, 0, 9, 3, 7, 2);
//Get the size of array
var size: Int = record.length;
//Before sort
obj.display(record, size);
obj.bubble_sort(record, size);
//After sort
obj.display(record, size);
}
}
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
//Swift Program
//Recursive Bubble Sort
class MySort
{
//Display array elements
func display(_ record: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", record[i], terminator: "");
i += 1;
}
print("\n", terminator: "");
}
//Perform bubble sort in given array
func bubble_sort(_ record: inout[Int], _ size: Int)
{
if (size <= 0)
{
//When have no more than one element
return;
}
var temp: Int = 0;
var i: Int = 0;
while (i < size - 1)
{
//Compare two adjacent elements
if (record[i] > record[i + 1])
{
//When need to swap two adjacent elements
temp = record[i];
record[i] = record[i + 1];
record[i + 1] = temp;
}
i += 1;
}
self.bubble_sort(&record, size - 1);
}
}
func main()
{
let obj: MySort = MySort();
//Assume given array elements
var record: [Int] = [2, 8, 1, 5, 0, 9, 3, 7, 2];
//Get the size of array
let size: Int = record.count;
//Before sort
obj.display(record, size);
obj.bubble_sort(&record, size);
//After sort
obj.display(record, size);
}
main();
Output
2 8 1 5 0 9 3 7 2
0 1 2 2 3 5 7 8 9
Resultant Output Explanation:
For the given input array [2, 8, 1, 5, 0, 9, 3, 7, 2], the output of the code will be:
Before sort: 2 8 1 5 0 9 3 7 2
After sort: 0 1 2 2 3 5 7 8 9
Time Complexity
The time complexity of Recursive Bubble Sort is the same as that of the traditional Bubble Sort, which is O(n^2) in the worst case. The worst case occurs when the array is sorted in reverse order, and every element needs to be compared and swapped in each pass. However, the average case time complexity is also O(n^2). Although Recursive Bubble Sort provides an interesting recursive approach, it doesn't improve the time complexity of the algorithm. Therefore, it is considered inefficient for large arrays and is mainly used for educational purposes or when dealing with very small datasets.
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment