# Bubble Sort Program In C Using Recursion

Bubble Sort In C Using Recursion – If you are looking for a bubble sort program in C with recursion example, this C programming tutorial will help you to learn how to write a program for bubble sort in C. Just go through this C programming example to learn about bubble sort, we are sure that you will be able to write a C program for bubble sort using recursion.

## Bubble Sort in C

### What is Bubble Sort?

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order.

### Bubble Sort Example

This below image illustrates what is Bubble Sort in C.

## Bubble Sort in C Using Recursion

Learn how to write a  bubble sort in c using recursion. Writing a Bubble Sort C Program can be done using various techniques like an array, pointers, recursion, function but here in this program, we show how to write a bubble sort program in c using recursion in a proper way.

### Bubble Sort Program in C Using Recursion – Source Code

You can copy paste the below bubble sort program in c compiler to check how the source code work. Or write your own Bubble Sort in C Using Function with the help of this below c program for bubble sort.

```/* BUBBLE SORT PROGRAM IN C USING RECURSION - BUBBLESORT.C */

#include <stdio.h>
#include <stdlib.h>

/* sorts 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;

/* get the number of inputs from the user */
printf("Enter the number of inputs:");
scanf("%d", &n);

/* dynamically allocate memory to store i/p values */
data = (int *) malloc(sizeof(int) * n);

/* get the input data from the user */
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("Data After Bubble Sort:\n");

for (i = 0; i < n; i++) {

printf("%d ", data[i]);

}

printf("\n");

return 0;

}```

#### C PROGRAM FOR BUBBLE SORT – OUTPUT

After you compile and run the above bubble sort program in c using recursion, your C compiler asks you to enter elements for bubble sort. After you enter elements, the program will be executed and give output like below expected output using bubble sort functionality.