# bubble sort recursive c with code examples

Bubble sort is a popular and simple sorting algorithm that is often taught as an introductory sorting algorithm in computer science courses. It is a comparison-based algorithm in which each pair of adjacent elements in an array is compared and swapped if they are in the wrong order. Bubble sort has a worst-case and average-case time complexity of O(n^2), where n is the number of elements in the array. In this article, we will discuss bubble sort's recursive implementation, which is less common than the iterative implementation, but it can provide a better understanding of the algorithm.

Recursive Implementation of Bubble Sort

Bubble sort recursive implementation works by recursively iterating through the array and swapping adjacent elements until the array is sorted. The recursion stops when there are no more elements to swap. Here is the pseudocode for bubble sort recursive implementation:

1. Base case: if array length is 1, return
2. For i = 0 to n-2, if array[i] > array[i+1], swap(array[i], array[i+1])
3. Recursively call bubbleSortRecursive(array, n-1)

In the first step, we check for the base case, which is when the array length is 1. If the base case is true, then we return because there are no more elements to sort. In the second step, we iterate through the array from the first to the second to last element, comparing each adjacent element, and swapping them if they are not in the correct order. In the third step, we invoke the bubbleSortRecursive function again, but with the array of size n-1. The recursive call will continue until the entire array is sorted.

Bubble Sort Recursive C Code Example

Let's take a look at the C code implementation of bubble sort recursive.

```#include <stdio.h>

void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}

void bubbleSortRecursive(int arr[], int n)
{
if (n == 1)
return;

for (int i = 0; i < n - 1; i++)
if (arr[i] > arr[i + 1])
swap(&arr[i], &arr[i + 1]);

bubbleSortRecursive(arr, n - 1);
}

void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("
");
}

int main()
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");
printArray(arr, n);

bubbleSortRecursive(arr, n);

printf("Sorted array: ");
printArray(arr, n);
return 0;
}
```

In the code example, we first define the swap function that swaps two integers. Then, we define the bubbleSortRecursive function, which takes an array and its size as input. If the array length is one, then the function returns. Otherwise, it iterates through the array and swaps adjacent elements if they are not in the correct order. Then, it calls itself with the array of size n-1.

In the main function, we declare an array and its size. First, we print the original array. Then, we call the bubbleSortRecursive function, which sorts the array. Finally, we print the sorted array.

Conclusion

Bubble sort recursive implementation is a simple and easy-to-understand algorithm. Recursive implementation of bubble sort is less efficient than its iterative counterpart but can provide a better understanding of the algorithm's mechanics. By implementing bubble sort recursively, we can practice and master recursion in computer science.

I'm sorry, but I need you to specify which previous topics you would like me to write more about.

## Popular questions

Certainly, here are five questions and their answers regarding bubble sort recursive implementation in C:

1. What is bubble sort?

Bubble sort is an algorithm for sorting an array of elements. It works by repeatedly iterating through the array, comparing adjacent elements, and swapping them if they are in the wrong order.

1. What is the time complexity of bubble sort?

Bubble sort has a worst-case and average-case time complexity of O(n^2), where n is the number of elements in the array.

1. What is the difference between iterative and recursive implementations of bubble sort?

In an iterative implementation, the outer loop iterates over the entire array, and the inner loop iterates from the beginning of the array up to the current position of the outer loop index. In a recursive implementation, the function repeatedly calls itself with a smaller array until the base case is reached.

1. What is the base case in the bubble sort recursive function?

The base case in the bubble sort recursive function is when the length of the array to be sorted is one.

1. What is the purpose of the swap function in the bubble sort recursive implementation?

The swap function in the bubble sort recursive implementation swaps two elements in an array. It is called by the bubbleSortRecursive function when two adjacent elements are out of order and need to be swapped to sort the array.

### Tag

BubblesortRecursion

##### Ahmed Zakaria
I am a driven and diligent DevOps Engineer with demonstrated proficiency in automation and deployment tools, including Jenkins, Docker, Kubernetes, and Ansible. With over 2 years of experience in DevOps and Platform engineering, I specialize in Cloud computing and building infrastructures for Big-Data/Data-Analytics solutions and Cloud Migrations. I am eager to utilize my technical expertise and interpersonal skills in a demanding role and work environment. Additionally, I firmly believe that knowledge is an endless pursuit.
Posts created 3229

## Discover how to write efficient code with practical examples!

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top