Problem statement:- Program to Implement Bubble sort using recursion.
Data requirement:-
Input Data:-n, arr
Output Data:-String Output
Output Data:-String Output
Program in C
Here is the source code of the C Program to Implement Bubble sort using recursion.
Code:
#include<stdio.h>
void BubbleSort(int arr[], int n)
{
int i,temp;
if(n>0)
{
for (i = 0; i < n-1; i++)
if (arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
BubbleSort(arr, n - 1);
}
}
int main()
{
int n,j;
printf("Enter your array size:");
scanf("%d",&n);
int arr[n];
printf("Enter the Array Element:");
for(j=0;j<n;j++)
{
scanf("%d",&arr[j]);
}
BubbleSort(arr,n);
printf("After Sorting Array Elements are:");
for(j=0;j<n;j++)
printf("%d ",arr[j]);
return 0;
}
Enter your array size:5
Enter the Array Element:4 8 10 3 5
After Sorting Array Elements are:3 4 5 8 10
Program in C++
Here is the source code of the C++ Program to Implement Bubble sort using recursion.
Code:
#include<iostream>
using namespace std;
void BubbleSort(int arr[], int n)
{
int i,temp;
if(n>0)
{
for (i = 0; i < n-1; i++)
if (arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
BubbleSort(arr, n - 1);
}
}
int main()
{
int n,j;
cout<<"Enter your array size:";
cin>>n;
int arr[n];
cout<<"Enter the Array Element:";
for(j=0;j<n;j++)
{
cin>>arr[j];
}
BubbleSort(arr,n);
cout<<"After Sorting Array Elements are:";
for(j=0;j<n;j++)
cout<<arr[j]<<" ";
return 0;
}
Enter your array size:6
Enter the Array Element:9 13 55 65 100 2
After Sorting Array Elements are:2 9 13 55 65 100
Program in Java
Here is the source code of the Java Program to Implement Bubble sort using recursion.
Code:
import java.util.Scanner;
public class BubbleSortRecursion {
static void BubbleSort(int arr[], int n)
{
int i,temp;
if(n>0)
{
for (i = 0; i < n-1; i++)
if (arr[i]>arr[i+1])
{
temp=arr[i];
arr[i]=arr[i+1];
arr[i+1]=temp;
}
BubbleSort(arr, n - 1);
}
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i;
System.out.println("Enter your Array Size:");
n=cs.nextInt();
int arr[]=new int[n];
System.out.println("Enter the Array Element:");
for(i=0;i<n;i++)
{
arr[i]=cs.nextInt();
}
BubbleSort(arr,n);
System.out.print("After Sorting Array Elements are:");
for(i=0;i<n;i++)
System.out.print(arr[i]+" ");
cs.close();
}
}
Enter your Array Size:
5
Enter the Array Element:
4 5 6 3 8
After Sorting Array Elements are:3 4 5 6 8
Program in Python
Here is the source code of the Python program to Implement Bubble sort using recursion.
Code:
def BubbleSort(arr,n):
if(n>0):
for i in range(0,n):
if (arr[i]>arr[i+1]):
temp = arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
BubbleSort(arr, n - 1)
arr=[]
n = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,n):
num = int(input())
arr.append(num)
BubbleSort(arr, n - 1)
print("After Sorting Array Elements are:")
for i in range(0,n):
print(arr[i],end=" ")
Enter the size of the array: 4
Enter the Element of the array:
6
7
11
3
After Sorting Array Elements are:
3 6 7 11
0 Comments
Please do not Enter any spam link in the comment box