Recursive bubble sort in C is the sorting algorithm used to arrange a list in a particular form that can be ascending or descending in numerical or lexicographical order. It is among the most-used algorithms in C that include the likes of merge sort and selection sort .
Bubble sort is the simplest sorting algorithm. It is named so because the smaller or larger elements, depending on whether the order is descending or ascending, are “bubbled” to the top of the list.
Recursive Bubble Sort in C
There isn’t much difference between bubble sort and recursive bubble sort. The basics are the same, only the implementation differs. The latter is faster than the former and thus, is preferred more. Let’s start with understanding the basics of bubble sort using recursion.
What is Recursive Bubble Sort?
Recursion refers to repetition itself by using smaller input values. This is done to the current input by applying simple functions on the returned value. There are several ways of performing a bubble sort , including using:
- Arrays ,
- Pointers,
- Functions, and
- Recursion.
When we use recursion to perform the bubble sort, it is called recursive bubble sort. The recursive function calls itself until we get the sorted data.
The advantages and disadvantages of the recursive bubble sort are just the same as that of the bubble sort. Nonetheless, it is a superior alternative to the plain bubble sort in most cases.
As we know that in the bubble sort we use the comparison-based technique to sort an array. First, we compare the two initial elements with each other and put the smaller ones in the very first position. Then in the second comparison or second pass, we start comparing the elements on the 2nd and 3rd positions. This continues until the list is sorted.
To understand the recursive bubble sort in C better, we are here to explain an example. Before diving into the code, let's first understand the algorithm that we will be using here.
Algorithm
- STEP 1: If the array size is 1, then return.
- STEP 2: Do One Pass of normal Bubble Sort on the given array. This will fix the last element of the current subarray.
- STEP 3: Use Recursion for all elements except the last of the current subarray .
Example of Bubble Sort in C Using Recursion
Here is the program which shows how to implement the bubble sort in C using recursion:
#include<stdio.h>
void BubbleSortRecursion(int a[],int num);
main()
{
int i,j,num,temp;
printf("Enter number of elements\n");
scanf("%d",&num);
int a[num];
printf("Enter numbers\n");
for(i=0;i<num;i++)
{
scanf("%d",&a[i]);
}
BubbleSortRecursion(a,num);
printf("Given numbers in Ascending order \n");
for(i=0;i<num;i++)
{
printf("%d\n",a[i]);
}
}
void BubbleSortRecursion(int a[],int num)
{
int i,j,temp;
i=num;
if(i>0)
{
for(j=0;j<num-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
BubbleSortRecursion(a,num-1);
}
else
{
return;
}
}
Example Output:
Enter the number of elements 6
Enter numbers 75 14 98 13 41 26
Given numbers in Ascending order 13 14 26 41 75 98
Press any key to continue…
Example Using C Programming
/* BUBBLE SORT PROGRAM IN C USING RECURSION */
#include <stdio.h>
#include <stdlib.h>
/* To sort the given numbers in ascending order */
void bubbleSort(int *data, int n) {
int i, temp;
if (n > 0) {
for (i = 1; i < n; i++) {
if (data[i - 1] > data[i]) {
temp = data[i];
data[i] = data[i - 1];
data[i - 1] = temp;
}
}
bubbleSort(data, n - 1);
}
return;
}
int main() {
int i, n, *data;
/* Enter the numbers of inputs which is to be sorted */
printf("Enter the number of inputs:");
scanf("%d", &n);
/* To store input values, it allocates dynamic memory */
data = (int *) malloc(sizeof(int) * n);
/* Enter the input data */
for (i = 0; i < n; i++) {
printf("data[%d]: ", i);
scanf("%d", &data[i]);
}
/* sorts the given numbers */
bubbleSort(data, n);
/* print the sorted numbers */
printf("Sorted array:\n");
for (i = 0; i < n; i++) {
printf("%d ", data[i]);
}
printf("\n");
return 0;
}
Example Output :
Enter the number of inputs 34, 99, 11, 25, 12, 21, 64
Sorted array: 11 12 21 25 34 64 99
Conclusion
That sums up this demonstration of implementing a recursive bubble sort in C. Please note that this is not a universal way of implementing a bubble sort using recursion in C. You can, therefore, write code that does the same but is different from the one mentioned above. Do let us know if you do so via the comments section below.
All the best!
People are also reading:
Leave a Comment on this Post