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

RECURSION java In class, you learned about some algorithms for sorting and searc

ID: 3708692 • Letter: R

Question

RECURSION java

In class, you learned about some algorithms for sorting and searching collections, as well as how to write recursive methods.

The chocolate factory has recently determined that, while making and selling chocolate is nice, They want to hire more people. But they have a problem: they don’t want to hire the same person twice. Whenever they interview a candidate, they have to know whether that candidate has already been hired Your task will be to create a list of the names of the employees that have already been hired and provide a way for a user to determine whether a new candidate’s name is already in the list of hired employees.

First, you will create a list of the names of hired employees using ARRAY LIST . These are the names of the employees so far:

Frank Frayburn

Christine Chriky

Scott Scofield

John Johnson

Catherine Cathright

Sarah Sargasso

Alex Alexander

Ida Id

Kevin Kevilry

Bailey Baines

Zeke Zephyr

Yancy Yankle

Diane DiAnthony

Xavier eXeter

Elaine Elosky

Then, at the console, query the user for a candidate’s name. If the user types 0, exit the program. Otherwise, take the given candidate name and search the list of employees:

Search using a RECURSIVE implementation of binary search.

Binary search requires a sort, so implement a RECURSIVE version of either selection sort, insertion sort, or merge sort.

You must use recursion to implement these methods and you cannot use built-in Java implementations of sort or search

If the candidate name is found in the list of employees, output “Do not hire this employee again!” If the candidate name is not found in the list of employees, output “Hire this person right away!” Continue to query the user for candidate names until the user types 0.

Explanation / Answer

import java.util.Arrays;
import java.util.Scanner;

public class HireEmployee {

   // Declare array of 50 elements which will hold the names of employees
   static String[] employees = new String[50];

   public static void main(String[] args) {
       // Fill Array with empty Strings , To avoid null pointer
       Arrays.fill(employees, "");
       // Take input from user
       String name = null;
       Scanner sc = new Scanner(System.in);
       while (true) {
           System.out.println("Please enter the name of the Employee or 0 to quit");
           name = sc.nextLine();
           if (name.equals("0")) {
               System.exit(0);
           } else {
               System.out.println(addEmployee(name));
           }
       }
   }

   // Add employee to Array
   public static String addEmployee(String employeeName) {
       // first sort the array
       // then search the employee using binary Search , if it is found , Don't add ,
       // else add to array

       bubbleSort(employees, employees.length);
       int found = binarySearch(employees, 0, employees.length - 1, employeeName);

       // if employee is not found the existing array , add to list
       if (found < 0) {
           for (int i = 0; i < employees.length; i++) {
               if (employees[i] == null || employees[i].isEmpty()) {
                   employees[i] = employeeName;
                   break;
               }
           }
           return "Hire this person right away!";
       } else {
           return "Do not hire this employee again!";
       }
   }

   // This method will use binary Search to find the employee in the array
   /***
   *
   * @param arr
   * @param l
   * @param r
   * @param employeeName
   *            first we will check at mid index if data is found then search is
   *            completed otherwise Search recursively in right or left half of
   *            the array
   * @return
   */
   static int binarySearch(String arr[], int l, int r, String employeeName) {
       if (r >= l) {
           int mid = l + (r - l) / 2;
           if (arr[mid].equalsIgnoreCase(employeeName))
               return mid;
           if (arr[mid].compareTo(employeeName) > 0)
               return binarySearch(arr, l, mid - 1, employeeName);
           return binarySearch(arr, mid + 1, r, employeeName);
       }
       return -1;
   }

   // Recursive bubble Sort . To Sort the data in ascending order which will be
   // used by the binary search method to search a particular employee name in the
   // array
   static void bubbleSort(String emp[], int n) {
       if (n == 1)
           return;
       for (int i = 0; i < n - 1; i++)

           if (emp[i].compareTo(emp[i + 1]) > 0) {
               // Swapping
               String temp = emp[i];
               emp[i] = emp[i + 1];
               emp[i + 1] = temp;
           }
       // Recursive Call
       bubbleSort(emp, n - 1);
   }
}

Output

Please enter the name of the Employee or 0 to quit
Waseem
Hire this person right away!
Please enter the name of the Employee or 0 to quit
Waseem
Do not hire this employee again!
Please enter the name of the Employee or 0 to quit
Akram'
Hire this person right away!
Please enter the name of the Employee or 0 to quit
akram
Hire this person right away!
Please enter the name of the Employee or 0 to quit
Sanib
Hire this person right away!
Please enter the name of the Employee or 0 to quit
Sajid
Hire this person right away!
Please enter the name of the Employee or 0 to quit
Sajid
Do not hire this employee again!
Please enter the name of the Employee or 0 to quit
0