栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Recursive Bubble Sort using C programming

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Recursive Bubble Sort using C programming

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
void BubbleSortRecursion(int a[],int num);
main()
{
int i,j,num,temp;
printf("Enter number of elementsn");
scanf("%d",&num);
printf("Enter numbersn");
for(i=0;i0)
  {
       for(j=0;ja[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:

#include 
#include 
  
  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;
        
        printf("Enter the number of inputs:");
        scanf("%d", &n);
        
        data = (int *) malloc(sizeof(int) * n);
        
        for (i = 0; i < n; i++) {
                printf("data[%d]: ", i);
                scanf("%d", &data[i]);
        
        bubbleSort(data, n);
        
        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!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/296291.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号