What does a Recursive Bubble Sort mean?
Recursion refers to repetition with itself through the use of smaller enter values. This is achieved to the modern enter through making use of easy features at the again value. There are numerous methods of acting a bubble sort, which includes:
- Arrays,
- Pointers,
- Functions, and
- Recursion
When we use recursion to carry out the bubble kind, it's miles known as the recursive bubble kind. The recursive feature calls itself till we get the looked-after data. The blessings and drawbacks of the recursive bubble kind are simply similar to that of the bubble kind. Nonetheless, it's miles a an advanced opportunity to the apparent bubble kind in maximum cases.
As we recognize that withinside the bubble kind we use the evaluation-primarily based totally approach to kind an array. First, we examine the 2 preliminary factors with every difference and positioned the smaller ones withinside the first actual position. Then withinside the 2nd evaluation or 2nd pass, we begin evaluating the factors at the 2d and third positions. This keeps till the listing is looked after.
To apprehend the recursive bubble type in C better, we're right here to provide an explanation for an example. Before diving into the code, let’s first apprehend the set of rules that we can be the usage of right 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
#include<stdio.h> void BubbleSortRecursion(int a[],int num); main() { int i,j,num,temp; printf("Enter number of elements\n"); scanf("%d",&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 bubble sort function in 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 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; }
Conclusion
That sums up this demonstration of enforcing a recursive bubble kind in C. Please observe that this isn't a regular manner of enforcing bubble kind the usage of recursion in C.
You can, therefore, write code that does the identical however isn't the same as the only cited above. Do allow us to recognize in case you achieve this thru the feedback segment below. All the best!