C Program for Recursive Bubble Sort

CServer Side ProgrammingProgramming

Bubble Sort is one of the simplest sorting algorithms used to sort data by comparing the adjacent elements. All the elements are compared in phases. The first phase places the largest value at the end, the second phase places the second largest element at the second last position and so on till the complete list is sorted.

Bubble Sort Algorithm

  • int arr[5]= { 5,4,2,1,3 };

  • int i, j ;

  • Traverse from index i=0 to i<array size

    • Traverse from index j=0 to array size - i - 1

    • If arr[i]>arr[j] swap arr[i] with arr[j]

  • End

Recursive Bubble Sort

  • If array length is 1 then return

  • Traverse array once and fix largest element at the end

  • Recursively perform step 2 for rest of the array except last element

Examples

Input − Arr[] = { 5,7,2,3,1,4 }; length=6

Output − Sorted array: 1 2 3 4 5 7

Explanation 

First Pass
5 7 2 3 1 4 → swap → 5 2 7 3 1 4
5 2 7 3 1 4 → swap → 5 2 3 7 1 4
5 2 3 7 1 4 → swap → 5 2 3 1 7 4
5 2 3 1 7 4 → swap → 5 2 3 1 4 7
Second Pass
5 2 3 1 4 7 → swap → 2 5 3 1 4 7
2 5 3 1 4 7 → swap → 2 3 5 1 4 7
2 3 5 1 4 7 → swap → 2 3 1 5 4 7
2 3 1 5 4 7 → swap → 2 3 1 4 5 7
Third Pass
2 3 1 4 5 7 → swap → 2 1 3 4 5 7
2 1 3 4 5 7 no swap
Fourth Pass
2 1 3 4 5 7 → swap → 1 2 3 4 5 7
1 2 3 4 5 7 no swap in further iterations

Input − Arr[] = { 1, 2, 3, 3, 2 };

Output − Sorted array: 1 2 2 3 3

Explanation

First Pass
1 2 3 3 2 → swap → 1 2 3 2 3
1 2 3 2 3 → swap → 1 2 2 3 3
1 2 2 3 3 no swap in further iterations
Second Pass
1 2 2 3 3 no swap in further iterations

Approach used in the below program is as follows

In the recursive approach of Bubble sort, the base case is array length = 1. Otherwise traverse the array using single for loop and swap elements accordingly.

  • Take input array Arr[] and length as number of elements in it.

  • Function recurbublSort(int arr[], int len) takes the array and its length and sorts the array recursively using bubble sort.

  • Take a variable temp.

  • If array length is 1 then return void.

  • Else traverse the array using single for loop and for each element arr[i]>arr[i+1], swap those elements.

  • Set temp=arr[i], arr[i]=arr[i+1] and arr[i+1]=temp.

  • Now decrement length by 1 as the previous loop placed the largest element at the last position.

  • Do a recursive call to recurbublSort(arr,len).

  • At the end of all calls, when len becomes 1 we will come out of recursion and the array will be sorted.

  • Print the sorted array inside main.

Example

#include <stdio.h>
void recurbublSort(int arr[], int len){
   int temp;

   if (len == 1){
      return;
   }
   for (int i=0; i<len-1; i++){
      if (arr[i] > arr[i+1]){
         temp=arr[i];
         arr[i]=arr[i+1];
         arr[i+1]=temp;
      }
   }
   len=len-1;
   recurbublSort(arr, len);
}
int main(){
   int Arr[] = {21, 34, 20, 31, 78, 43, 66};
   int length = sizeof(Arr)/sizeof(Arr[0]);

   recurbublSort(Arr, length);

   printf("Sorted array : ");
   for(int i=0;i<length;i++){
      printf("%d ",Arr[i]);
   }

   return 0;
}

Output

If we run the above code it will generate the following Output

Sorted array: 20 21 31 34 43 66 78
raja
Published on 02-Nov-2021 07:44:54
Advertisements