Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a Java program in which the recursive version of the selection sorting met

ID: 3605139 • Letter: W

Question

Write a Java program in which the recursive version of the selection sorting method with the following signature public static void r selectionSort(int[ ] array, int s, int t) should be defined. That method arranges a collection of integers in an integer array with beginning index s and terminating index t in ascending order. Your program will ask the user to input a positive integer for the size of an integer array, fill in the array with integers between 0 and 999 inclusively, display the integers in the array, invoke the method r_selectionSort, and display the integers in the array again. A sample run of your program is as follows. Input an integer for the size of an array 16 16 numbers are generated and they are 922 602 519 429 776 924 167 941 629 115 842 620 464 31 209 753 16 numbers are sorted by recursive selection-sorting program and they are 31 115 167 209 429 464 519 602 620 629 753 776 842 922 924 941

Explanation / Answer

import java.util.*;
public class selectionSort
{
   public static void main(String []args)
   {
       int n;
       Scanner s = new Scanner(System.in);
      
       System.out.println("Input the integer for the size of an array: ");
       n = s.nextInt();

       int arr[] = new int[n];
       Random r = new Random();

       for(int i = 0; i<n; i++)
       {
           arr[i] = r.nextInt(1000);
       }
      
       System.out.println(n + " numbers are generated and they are: ");

       for(int j = 0; j < n; j++)
       {
           System.out.println(arr[j]);
       }

       //calling the function
       r_selectionSort(arr, 0, n-1);

      
       //printing the sorted array
       System.out.println(n + " numbers are sorted by recursive selection-sorting program and they are:");

       for(int k = 0; k < n; k++)
       {
           System.out.println(arr[k]);
       }

  
   }

   public static void r_selectionSort(int[] arr, int s, int t)
   {
       //return if starting and terminating index is same
       if(s == t)
           return;

       //calling minimum index function to find the index of the minimum element
       int k = minIndex(arr, s, t);

       //swapping the first element with the minimum element
       if(k != s)
       {
           //swap
           int temp = arr[k];
        arr[k] = arr[s];
        arr[s] = temp;
       }

       // Recursively calling selection sort function
   r_selectionSort(arr,s + 1, t);

      

   }

   //Returns minimum element index
   static int minIndex(int a[], int i, int j)
   {
       if(i == j)
           return i;
       else
       {
           int min = a[i];
           int index = i;
           for(int k = i+1; k<= j; k++)
           {
               if(min>a[k])
               {
                   min = a[k];
                   index = k;
               }
           }
           return index;
       }  
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote