Question: Lab : Use recursion to nd the kth smallest number in an array You will write a program that allows the user to recursively nd
Lab : Use recursion to nd the kth smallest number in an array You will write a program that allows the user to recursively nd the kth smallest element from an array. We have provided a Lab.java le with code to read in a data le of integers into an array of Integers and print the number of elements read in, N. Complete the recursive function kSmall which, given an argument k between 1 and N and an array of Comparable elements, returns the kth smallest element in the array. You also need to add a loop in the main method that repeatedly prompts the user to input an argument between 1 and N. The program should loop until an index less than 1 or greater than N is entered as the index. For a valid index, k, call kSmall to compute the kth smallest integer read in and then write out an appropriate message. Submit only the le Lab.java. We will use our own data les to test your work.
----------------------------------------------------------------------------
import java.util.Scanner; import java.io.IOException; import java.io.File; import java.util.Vector;
public class Lab{ /** * Partitions an array for quickSort. * @param first is the index of the first element to sort with * first <= last. * @param last is the index of the last element to sort with * first <= last. * @param theArray is the array to be sorted: the element * between first and last (with * first <= last)will be sorted. * @return the index of the pivot element of * theArray[first..last]. Upon completion of the method, this will * be the index value lastS1 such that S1 = * theArray[first..lastS1-1] < pivot theArray[lastS1] == pivot S2 = * theArray[lastS1+1..last] >= pivot */ private static
public static
/* TO COMPLETE */ }
public static void main(String[] args){ try{ Scanner console = new Scanner(System.in); System.out.println("Enter the name of the file containing the data"); String filename = console.next(); // read the data in the file Vector
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
