PLEASE READ ALL :) THANKS Write a Java program in which the recursive version of
ID: 3736254 • Letter: P
Question
PLEASE READ ALL :) THANKS
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)
The 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 shown below:
-------------------------------------------------------------------------------------
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
PROGRAM
import java.io.*;
import java.util.Scanner;
class Select_Recursive // Create main class name Select_Recursive
{
public static void r_selectionSort(int[] array,int s,int t)
{
if(s<t){ // Check condition from low index to high index
int min=s; // assign low to min index
int m=array[s]; // assign array first value to m i.e, minimum
for(int i=s+1;i<=t;i++) // create for loop from first value to last value
{
if(array[i]<m){ // check condition array of second to preceding value
m=array[i]; // then second value to minimum
min=i; // i value to min index
}
}
array[min]=array[s]; // assign first value to mininmum
array[s]=m; // assing preceding index to array of first element
r_selectionSort(array,s+1,t); // call r_selectionSort() recursively
}
}
public static void main(String args[])
{
int n;
Scanner scr=new Scanner(System.in); // Create object of Scanner scr
System.out.println("Input an integer for the size of an array");
n=scr.nextInt(); // Read size of an array
int array[]=new int[n]; // Declare an array with size n
System.out.println("Enter Array Elements:");
for(int i=0;i<n;i++)
array[i]=scr.nextInt(); // reading array elements
System.out.print(n+" numbers are generated and they are ");
for(int i=0;i<n;i++)
System.out.print(array[i]+" ");
r_selectionSort(array,0,array.length-1); // call r_selectionSort() function from 0 to array size
System.out.println();
System.out.println();
// display the Sorted List
System.out.print(array.length+" numbers are sorted by recursive selection-sorting program and they are ");
for(int i=0;i<array.length;i++)
System.out.print(array[i]+ " ");
}
}
OUTPUT
F:>javac Select_Recursive.java
F:>java Select_Recursive
Input an integer for the size of an array
16
Enter Array Elements:
922
602
519
429
776
924
167
941
629
115
842
620
464
31
209
753
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.