Recursive Bubble Sort using C programming

本文详细介绍了如何使用递归实现冒泡排序,包括递归冒泡排序的算法步骤、示例代码以及C语言中的具体实现。通过递归,将原本的冒泡排序过程分解为更小的子问题,提高了代码的可读性。示例代码展示了两种不同的递归实现方式,帮助读者理解递归冒泡排序的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值