Sort array in descending order using recursion

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

Data requirement:-

   Input Data:-n, arr

  Output Data:-
SumOfEvenElement(arr,n)

Program in C

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

Code:

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

           Decreasing_sort_element(arr, n - 1);
        }

    }

int swap_Element(int arr[],int i,int j)
{
         int temp=arr[i];
         arr[i]=arr[j];
         arr[j]=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]);
    }
    Decreasing_sort_element(arr,n);
    printf("After Decreasing 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 Decreasing order sort Array Elements are:10 8 5 4 3

Program in C++

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

Code:

#include<iostream>
using namespace std;
void swap_Element(int arr[],int i,int j)
{
         int temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
}
void Decreasing_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);
        }

           Decreasing_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];
    }
    Decreasing_sort_element(arr,n);
    cout<<"After Decreasing 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 Decreasing order sort Array Elements are:100 65 55 13 9 2

Program in Java

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

Code:

import java.util.Scanner;
public class SortElementRecusrion {
static void swap_Element(int arr[],int i,int j)
{
         int temp=arr[i];
         arr[i]=arr[j];
         arr[j]=temp;
}
static void Decreasing_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);
        }

           Decreasing_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();
}
Decreasing_sort_element(arr,n);
System.out.print("After Decreasing 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 Decreasing order sort Array Elements are:52 33 13 10 7 5 4 3 

Program in Python

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

Code:

def swap_Element(arr,i,j):
    temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
def Decreasing_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)
        Decreasing_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)
Decreasing_sort_element(arr,n)
print("After Decreasing order sort Array Elements are:")
printArr(arr, n)

Input/Output:
Enter the size of the array: 3
Enter the Element of the array:
4
6
5
After Decreasing order sort Array Elements are:
6 5 4 


Post a Comment

0 Comments