← All resources

Write a program that uses a recursive technique to sort an array using selection sort method

<h3>OUTPUT</h3>
Enter the size of the array:
5
Enter the values for the array:
12
23
11
10
32
Array after selection sort:
32
23
12
11
10
import java.util.*;
public class Selection_Recur
{
    int largest(int a[], int i, int maxp)
    {
        if(i==a.length)
        return maxp;
        if(a[i]>a[maxp])
        maxp=i;
        return largest(a, i+1, maxp);        //Recursive Call
    }
    void selectsort(int a[], int i)
    {
        if(i==a.length)
        {
            return;
        }
        int mp = largest(a, i, i);
        int tmp = a[mp];
        a[mp] = a[i];
        a[i] = tmp;
        selectsort(a, i+1);        //Recursive Call
    }
    public void calling()
    {
        Scanner sc = new Scanner(System.in);
        int arr[], n, i;
        System.out.println("Enter the size of the array:");
        n = sc.nextInt();
        arr = new int[n];
        System.out.println("Enter the values for the array:");
        for(i=0;i < n;i++)
        {
            arr[i] = sc.nextInt();
        }
        selectsort(arr, 0);     //performing selection sort
        System.out.println("Array after selection sort:");
        for(i=0;i < n;i++)
        {
            System.out.println(arr[i]);
        }
    }
    public static void main()
    {
        Selection_Recur obj = new Selection_Recur();
        obj.calling();
    }
}