Sort array in ascending order using recursion

Problem statement:- Program to Sort array in ascending order using recursion.

Data requirement:-

   Input Data:-n, arr

  Output Data:-
arr

Program in C

Here is the source code of the C Program to Sort array in ascending order using recursion.

Code:

#include<stdio.h>
void sort_element(int arr[], int n)
{
    int i;
    if(n>0)
    {
        for (i = 0; i < n; i++)
            if (arr[i]>=arr[n - 1])
        {
            swap_Element(arr,i,n-1);
        }

           sort_element(arr, n - 1);
        }

    }

int swap_Element(int arr[],int i,int j)
{
         int temp=arr[j];
         arr[j]=arr[i];
         arr[i]=temp;
}

void printArr(int arr[], int n)
{
    int i;
    for (i=0; i < n; i++)
        printf("%d ", arr[i]);
}
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]);
    }
    sort_element(arr,n);
    printf("After ascending order sort Array Elements are:");
    printArr(arr, n);
    return 0;
}

Input/Output:
Enter your array size:5
Enter the Array Element:4 8 10 3 5
After ascending order sort Array Elements are:3 4 5 8 10

Program in C++

Here is the source code of the C++ Program to Sort array in ascending order using recursion.

Code:

#include<iostream>
using namespace std;
void swap_Element(int arr[],int i,int j)
{
         int temp=arr[j];
         arr[j]=arr[i];
         arr[i]=temp;
}
void sort_element(int arr[], int n)
{
    int i;
    if(n>0)
    {
        for (i = 0; i < n; i++)
            if (arr[i]>=arr[n - 1])
        {
            swap_Element(arr,i,n-1);
        }

           sort_element(arr, n - 1);
        }
    }
void printArr(int arr[], int n)
{
    int i;
    for (i=0; i < n; i++)
        cout<<arr[i]<<" ";
}
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];
    }
    sort_element(arr,n);
    cout<<"After ascending order sort Array Elements are:";
    printArr(arr, n);
    return 0;
}

Input/Output:
Enter your array size:6
Enter the Array Element:9 13 55 65 100 2
After ascending order sort Array Elements are:2 9 13 55 65 100

Program in Java

Here is the source code of the Java Program to Sort array in ascending order using recursion.

Code:

import java.util.Scanner;
public class SortRecursion {
static void swap_Element(int arr[],int i,int j)
{
         int temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
}
static void sort_element(int arr[], int n)
{
    int i;
    if(n>0)
    {
        for (i = 0; i < n; i++)
            if (arr[i]>=arr[n - 1])
        {
            swap_Element(arr,i,n-1);
        }

           sort_element(arr, n - 1);
        }
    }
static void printArr(int arr[], int n)
{
    int i;
    for (i=0; i < n; i++)
       System.out.print(arr[i]+" ");
}
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();
}
sort_element(arr,n);
System.out.print("After ascending order sort Array Elements are:");
    printArr(arr, n);
cs.close();
}
}

Input/Output:
Enter your Array Size:
8
Enter the Array Element:
4 5 10 13 52 33 3 7
After ascending order sort Array Elements are:3 4 5 7 10 13 33 52 

Program in Python

Here is the source code of the Python program to Sort array in ascending order using recursion.

Code:

def swap_Element(arr,i,j):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
def sort_element(arr,n):
    if(n>0):
        for i in range(0,n):
            if (arr[i] >= arr[n - 1]):
                swap_Element(arr, i, n - 1)
        sort_element(arr, n - 1)
def printArr(arr,n):
    for i in range(0, n):
        print(arr[i],end=" ")
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)
sort_element(arr,n)
print("After ascending  order sort Array Elements are:")
printArr(arr, n)

Input/Output:
Enter the size of the array: 3
Enter the Element of the array:
5
9
2
After ascending  order sort Array Elements are:
2 5 9 


Post a Comment

0 Comments