# Lab 8 recursion java | Computer Science homework help

FIND A SOLUTION AT Academic Writers Bay

CSE 1302 J
Fall 2014
Lab 8

Recursion in array handling

•         Iterative Linear Search (given)
•         Recursive Linear Search
•         Add recursive linear search logic to linearSearchR
•         Make case 5 in main a call linearSearchRec.
•         Iterative Binary Search
•         Add a new method to your IntegerList, called intbinarySearch (int target) which will binary search the array iteratively
•         Make case 6 in main a call to binarySearch
•         Recursive Binary Search
•         Add recursive logic to binarySearchR
•         Make case 7 in main a call binarySearchRec

Turn in the final source and output that does the following:
•         Create an array of 10 positions and print it.
•         Search for first item, last item, a middle item in the list, and an item not in list, using linear search, iterative version, case 3
•         Search for first item, last item, a middle item in the list, and an item not in list, using linear search, recursive version, case 5
•         Now sort the array and print it.
•         Search for first item, last item, a middle item in the list, and an item not in list, using binary search, iterative version, case 6
•         Search for first item, last item, middle item in the list, and an item not in list, using binary search, recursive version, case 7

Recursive Linear Search

File IntegerList.java contains a class IntegerList that represents a list of integers (you may have used a version of this in an earlier lab); IntegerListTest.java contains a simple menu-driven test program that lets the user create, sort, and print a list and search for an element using a linear search.

Many list processing tasks, including searching, can be done recursively. The base case typically involves doing something with a limited number of elements in the list (say the first element), then the recursive step involves doing the task on the rest of the list. Think about how linear search can be viewed recursively; if you are looking for an item in a list starting at index i:

•         If iexceeds the last index in the list, the item is not found (return -1).
•         If the item is at list[i], return i.
•         If the is not at list[i], do a linear search starting at index i+1.

Fill in the body of the method linearSearchR in the IntegerList class. The method should do a recursive linear search of a list starting with a given index (parameter lo). Note that the IntegerList class contains another method linearSearchRec that does nothing but call your method (linearSearchR). This is done because the recursive method (linearSearchR) needs more information (the index to start at) than you want to pass to the top-level search routine (linearSearchRec), which just needs the thing to look for.

// ****************************************************************
//   IntegerList.java
//
//   Defines an IntegerList class with methods to create, fill,
//   sort, and search in a list of integers. (Version S #8211;
//   for use in the linear search exercise.)
//
// ****************************************************************

public class IntegerList
{
int[] list; //values in the list

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
//   Creates a list of the given size
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
publicIntegerList (int size)
{
list = new int[size];
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
//   Fills the array with integers between 1 and 100, inclusive
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
public void randomize()
{
for (inti=0; ilt;list.length; i++)
list[i] = (int)(Math.random() * 100) + 1;
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;-
//   Prints array elements with indices
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;-
public void print()
{
for (inti=0; ilt;list.length; i++)
System.out.println(i + #8220;:t#8221; + list[i]);
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
//   Returns the index of the first occurrence of target in the list.
//   Returns -1 if target does not appear in the list.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
publicintlinearSearch(int target)
{
int location = -1;
for (inti=0; ilt;list.length;; location == -1; i++)
if (list[i] == target)
location = i;
return location;
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
//   Returns the index of an occurrence of target in the list, -1
//   if target does not appear in the list.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
publicintlinearSearchRec(int target)
{
returnlinearSearchR (target, 0);
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
//   Recursive implementation of the linear search #8211; searches
//   for target starting at index lo.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
privateintlinearSearchR (int target, int lo)
{
// fill in code for linear search recursive
return -1;
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
//   Returns the index of the occurrence of target in the list.
//   Returns -1 if target does not appear in the list.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
publicintbinarySearch(int target)
{
// fill in code for iterative binary search

}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
//   Returns the index of an occurrence of target in the list, -1
//   if target does not appear in the list.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
publicintbinarySearchRec(int target)
{
returnbinarySearchR (target, 0, list.length-1);
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
//   Recursive implementation of the binary search algorithm.
//   If the list is sorted the index of an occurrence of the
//   target is returned (or -1 if the target is not in the list).
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8211;
privateintbinarySearchR (int target, int lo, int hi)
{
int index;

// fill in code for the binary search recursive

return index;
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
//  Sorts the list into ascending order using the selection sort algorithm.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
public void selectionSort()
{
intminIndex;
for (inti=0; ilt; list.length-1; i++)
{
//find smallest element in list starting at location i
minIndex = i;
for (int j = i+1; j lt;list.length; j++)
if (list[j] lt; list[minIndex])
minIndex = j;

//swap list[i] with smallest element
int temp = list[i];
list[i] = list[minIndex];
list[minIndex] = temp;
}
}
}

// ****************************************************************
//    IntegerListSTest.java
//
//    Provide a menu-driven tester for the IntegerList class.
//    (Version S #8211; for use in the linear search lab exercise).
//
// ****************************************************************
importjava.util.Scanner;

public class IntegerListTest
{
staticIntegerList list = new IntegerList(10);
static Scanner scan = new Scanner(System.in);

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
//   Creates a list, then repeatedly print the menu and do what the
//   user asks until they quit.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;
public static void main(String[] args)
{
int choice = scan.nextInt();
while (choice != 0)
{
dispatch(choice);
choice = scan.nextInt();
}
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;-
//  Does what the menu item calls for.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;-
public static void dispatch(int choice)
{
intloc;
switch(choice)
{
case 0:
System.out.println(#8220;Bye!#8221;);
break;
case 1:
System.out.println(#8220;How big should the list be?#8221;);
int size = scan.nextInt();
list = new IntegerListS(size);
list.randomize();
break;
case 2:
list.selectionSort();
break;
case 3:
System.out.print(#8220;Enter the value to look for: #8220;);
loc = list.linearSearch(scan.nextInt());
if (loc != -1)
System.out.println(#8220;Found at location #8221; + loc);
else
System.out.println(#8220;Not in list#8221;);
break;
case 4:
list.print();
break;

//add case 5 and 6 and 7
default:
System.out.println(#8220;Sorry, invalid choice#8221;);
}
}

// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;-
//   Prints the menu of user#8217;s choices.
// #8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;#8212;-
{
System.out.println(#8220;   ====#8221;);
System.out.println(#8220;0: Quit#8221;);
System.out.println(#8220;1: Create new list elements (** do this first!! **)#8221;);
System.out.println(#8220;2: Sort the list using selection sort#8221;);
System.out.println(#8220;3: Find an element in the list using linear search#8221;);
System.out.println(#8220;4: Print the list#8221;);
}

}
Recursive Binary Search

The binary search algorithm from Chapter 9 is a very efficient algorithm for searching an ordered list.  The algorithm (in pseudocode) is as follows:

highIndex #8211; the maximum index of the part of the list being searched
lowIndex #8211; the minimum index of the part of the list being searched
target #8212; the item being searched for

//look in the middle
middleIndex = (highIndex + lowIndex) / 2
if the list element at the middleIndex is the target
return the middleIndex
else
if the list element in the middle is greater than the target
search the first half of the list
else
search the second half of the list

Notice the recursive nature of the algorithm.  It is easily implemented recursively. Note that three parameters are needed—the target and the indices of the first and last elements in the part of the list to be searched. To #8220;search the first half of the list#8221; the algorithm must be called with the high and low index parameters representing the first half of the list. Similarly, to search the second half the algorithm must be called with the high and low index parameters representing the second half of the list. The file IntegerListB.java contains a class representing a list of integers (the same class that has been used in a few other labs); the file IntegerListBTest.java contains a simple menu-driven test program that lets the user create, sort, and print a list and search for an item in the list using a linear search or a binary search. Your job is to complete the binary search algorithm (method binarySearchR). The basic algorithm is given above but it leaves out one thing: what happens if the target is not in the list? What condition will let the program know that the target has not been found? If the low and high indices are changed each time so that the middle item is NOT examined again (see the diagram of indices below) then the list is guaranteed to shrink each time and the indices #8220;cross#8221;—that is, the high index becomes less than the low index. That is the condition that indicates the target was not found.

lo                 middle                     high

lo         middle-1     middle+1              high

^             ^            ^                    ^
|             |            |                    |
#8212;#8212;#8212;#8212;-              #8212;#8212;#8212;#8212;#8212;#8212;#8211;

first half                      last half

Fill in the blanks below, then type your code in. Remember when you test the search to first sort the list.

privateintbinarySearchR (int target, int lo, int hi)
{
int index;
index = -1;
else
{
int mid = (lo + hi)/2;
if ( ______________________________ ) // found it!
index = mid;
else if (target lt; list[mid])
// fill in the recursive call to search the first half
// of the list
index = _______________________________________________;
else
// search the last half of the list
index = _______________________________________________;
}
return index;
}

QUALITY: ORIGINAL PAPER NO PLAGIARISM - CUSTOM PAPER

## Why Choose Us?

• non-plagiarized Papers
• Affordable Prices
• Any Paper, Urgency, and Subject
• Will complete your papers in 6 hours
• On-time Delivery
• Money-back and Privacy guarantees
• Unlimited Amendments upon request
• Satisfaction guarantee

## How It Works

• Click on the “Place Your Order” tab at the top menu or “Order Now” icon at the bottom and a new page will appear with an order form to be filled.
• Fill in your paper’s requirements in the "PAPER DETAILS" section.