Bubble Sort Using Recursion in C

Cloudytechi
3 min readOct 4, 2021

--

Bubble Sort Overview

Bubble kind is a stable, in-location sorting set of rules named for smaller or large elements “bubble” to the pinnacle of the listing. Although the set of rules is simple, it’s far too gradual and impractical for maximum issues even in comparison to insertion kind, and isn’t always advocated for big input.

The best enormous gain that bubble kind has over maximum different implementations, even Quicksort, however now no longer insertion kind, is the capacity to hit upon if the listing is already sorted. When the listing is already sorted (best-case), the bubble kind runs in linear time.

How does Bubble Sort work?

Each bypass of bubble-type steps thru the listing to be looked after compares every pair of adjoining objects and swaps them if they may be withinside the incorrect order. At the quit of every by skip, the subsequent biggest detail will “Bubble” as much as its accurate position.

These passes thru the listing are repeated till no swaps are needed, which suggests that the listing is looked after. In the worst-case, we’d emerge as making an n-1 bypass, in which n is the entered size.

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

Example Using C

/* 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 enforcing a recursive bubble kind in C. Please be aware that this isn’t a typical manner of enforcing bubble kind the usage of recursion in C. You can, therefore, write code that does the equal, however, isn’t like the only one referred to above. Do allow us to recognize in case you achieve this thru the feedback segment below. All the best!

--

--

Cloudytechi
Cloudytechi

Written by Cloudytechi

A tech guy who is more enthusiastic of programming and love coding.

No responses yet