Download Lab 6g Searching and Sorting An Integer List

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Java ConcurrentMap wikipedia , lookup

Array data structure wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Lab 6g: Searching and Sorting An Integer List
Lab 6g: Searching and Sorting An Integer List
File IntegerList6g.java contains a Java class representing a list of integers. The following public methods are provided:





IntegerList(int size)—creates a new list of size elements. Elements are initialized to 0.
void randomize()—fills the list with random integers between 1 and 100, inclusive.
void print()—prints the array elements and indices
int search(int target)—looks for value target in the list using a linear (also called sequential) search algorithm.
Returns the index where it first appears if it is found, -1 otherwise.
void selectionSort()—sorts the lists into ascending order using the selection sort algorithm.
File IntegerListTest6g.java contains a Java program that provides menu-driven testing for the IntegerList6g class. Copy both files to
your directory, and compile and run IntegerListTest6g to see how it works. For example, create a list, print it, and search for an
element in the list. Does it return the correct index? Now look for an element that is not in the list. Now sort the list and print it to
verify that it is in sorted order.
Modify the code in these files as follows:
1.
Add replaceFirst() method - Add a method void replaceFirst(int oldVal, int newVal) to the
IntegerList6g class.
a. Replace first occurrence - The method will replace the first occurrence of oldVal in the list with newVal.
b. If not found, do nothing - If oldVal does not appear in the list, it should do nothing (but it's not an error).
c. Only the first, skip the rest - If oldVal appears multiple times, only the first occurrence should be replaced. Note
that you already have a method to find oldVal in the list; use it!
2.
Add replaceAll() method - Add a method void replaceAll(int oldVal, int newVal) to the
IntegerList6g class.
a. Replace all occurrences - Replace all occurrences of oldVal in the list with newVal.
b. If not found, do nothing - If oldVal does not appear in the list, it should do nothing (but it's not an error).
3.
Add sortDecreasing() method - Add a method void sortDecreasing() to the IntegerList6g class.
a. Sort in decreasing order – Sort the list into decreasing (instead of increasing) order.
b. Use the same algorithm - Use the selection sort algorithm, but modify it to sort the other way. Be sure you change
the variable names so they make sense!
4.
Add binarySearchD() method - Add a method int binarySearchD (int target) to the IntegerList6g class.
a. Perform binary search assuming descending – Use a binary search to find the target assuming the list is sorted in
decreasing order.
b. If found, return index where found - If the target is found, the method should return its index; otherwise the
method should return –1. Your algorithm will be a modification of the binary search algorithm in listing 6.11 on
page 316 of the text.
Use class IntegerListTestNew6g to test your new method. While it won’t look EXACTLY like the sample output below, the same
replacements should work
IntegerList6g class
//
//
//
//
//
//
//
//
****************************************************************
IntegerList6g.java [Lab 6g]
Student Name: <name>
Define an IntegerList class with methods to create, fill,
sort, and search in a list of integers.
****************************************************************
public class IntegerList6g
{
int[] iArrList; // values in the list
// ------------------------------------------------------Page 1 of 6
Searching & Sorting
Lab 6g: Searching and Sorting An Integer List
// create a list of the given size
// ------------------------------------------------------public IntegerList6g( int size )
{
iArrList = new int[size];
}
// ------------------------------------------------------// print array elements with indices
// ------------------------------------------------------public void print()
{
for ( int i = 0; i < iArrList.length; i++ )
{
System.out.print( "[" + i + "] = *" + iArrList[i] + "* " );
if ( ( i + 1 ) % 5 == 0 )
{
System.out.println();
}
}
System.out.println();
}
// ------------------------------------------------------// fill array with integers between 1 and 100, inclusive
// ------------------------------------------------------public void randomize()
{
for ( int i = 0; i < iArrList.length; i++ )
{
iArrList[i] = (int) ( Math.random() * 100 ) + 1;
}
}
// ------------------------------------------------------// return the index of the first occurrence of target in the list.
// return -1 if target does not appear in the list
// ------------------------------------------------------public int search( int target )
{
int location = -1;
for ( int i = 0; i < iArrList.length && location == -1; i++ )
{
if ( iArrList[i] == target )
{
location = i;
}
}
return location;
}
// ------------------------------------------------------// sort the list into ascending order using the selection sort algorithm
// ------------------------------------------------------public void selectionSort()
{
int minIndex;
// ------------------------------------------------------// Loop through the list checking each element
// (except the last since it will be sorted)
for ( int i = 0; i < iArrList.length - 1; i++ )
{
// ------------------------------------------------------// Compare each element against the minimum value. If
// a smaller value is found, make it the new minimum value
minIndex = i;
for ( int j = i + 1; j < iArrList.length; j++ )
{
if ( iArrList[j] < iArrList[minIndex] )
{
minIndex = j;
}
}
// ---------------------------------------------// swap list[i] with smallest element
Page 2 of 6
Searching & Sorting
Lab 6g: Searching and Sorting An Integer List
swap( i, minIndex );
}
}
// ------------------------------------------------------// Set value in array at index
// ------------------------------------------------------public void setValue( int iIndex, int iVal )
{
if ( iIndex >= iArrList.length || iIndex < 0 )
{
return;
}
iArrList[iIndex] = iVal;
}
// ------------------------------------------------------// method to swap places in the array
// ------------------------------------------------------public void swap( int indexA, int indexB )
{
int temp = iArrList[indexA];
iArrList[indexA] = iArrList[indexB];
iArrList[indexB] = temp;
}
// ------------------------------------------------------// TODO: #1: replace the first instance
// ------------------------------------------------------void replaceFirst( int oldVal, int newVal )
{
}
// ------------------------------------------------------// TODO: #2: replace all instances
// ------------------------------------------------------void replaceAll( int oldVal, int newVal )
{
}
// ------------------------------------------------------// TODO: #3: Sort in Decreasing order
// ------------------------------------------------------public void sortDecreasing()
{
}
// ------------------------------------------------------// TODO: #4: Binary Search for descending
// ------------------------------------------------------public int binarySearchD( int key )
{
}
}
IntegerListTest6g class
// ****************************************************************
// IntegerListTest6g.java [Lab 6g]
//
Student Name: <name>
//
// Provide a menu-driven tester for the IntegerList6g class.
//
// ****************************************************************
import java.util.Scanner;
public class IntegerListTest6g
{
static IntegerList6g list = new IntegerList6g( 10 );
static Scanner scan = new Scanner( System.in );
// ------------------------------------------------------// Do what the menu item calls for
// ------------------------------------------------------Page 3 of 6
Searching & Sorting
Lab 6g: Searching and Sorting An Integer List
public static void dispatch( int choice )
{
int loc;
switch ( choice )
{
case 0:
System.out.println( "Bye!" );
break;
case 1:
System.out.println( "How big should the list be?" );
int size = scan.nextInt();
list = new IntegerList6g( size );
list.randomize();
break;
case 2:
list.selectionSort();
break;
case 3:
System.out.print( "Enter the value to look for: " );
loc = list.search( scan.nextInt() );
if ( loc != -1 )
{
System.out.println( "Found at location " + loc );
}
else
{
System.out.println( "Not in list" );
}
break;
case 4:
list.print();
break;
default:
System.out.println( "Sorry, invalid choice" );
}
}
// ------------------------------------------------------// Create a list, then repeatedly print the menu and do what the
// user asks until they quit
// ------------------------------------------------------public static void main( String[] args )
{
printMenu();
int choice = scan.nextInt();
while ( choice != 0 )
{
dispatch( choice );
printMenu();
choice = scan.nextInt();
}
}
// ------------------------------------------------------// Print the user's choices
// ------------------------------------------------------public static void printMenu()
{
System.out.println( "\n
Menu
" );
System.out.println( "
====" );
System.out.println( "0: Quit" );
System.out.println( "1: Create a new list (** do this first!! **)" );
System.out.println( "2: Sort the list using selection sort" );
System.out.println( "3: Find an element in the list using linear search" );
System.out.println( "4: Print the list" );
System.out.print( "\nEnter your choice: " );
}
}
IntegerListTestNew6g class
// ****************************************************************
// IntegerListTest6g.java [Lab 6g]
//
Student Name: <name>
//
Page 4 of 6
Searching & Sorting
Lab 6g: Searching and Sorting An Integer List
// Quick tester class to make sure new methods work as expected
//
// ****************************************************************
public class IntegerListTestNew6g
{
public static void main( String[] args )
{
// ------------------------------------------// Create Integer list, randomize, and set
// specific values
IntegerList6g iList = new IntegerList6g( 20 );
iList.randomize();
iList.setValue( 1, 268 );
iList.setValue( 6, 268 );
iList.setValue( 11, 268 );
iList.setValue( 16, 268 );
iList.setValue( 0, 20 );
iList.setValue( 5, 20 );
// ------------------------------------------// Print initial list
System.out.println( "Initial Array:" );
iList.print();
// ------------------------------------------// Replace first 20 with 200 and print
iList.replaceFirst( 20, 200 );
System.out.println( "Replaced FIRST 20 with 200:" );
iList.print();
// ------------------------------------------// Replace all 268 with 9 and print
iList.replaceAll( 268, 9 );
System.out.println( "Replaced ALL 268 with 9:" );
iList.print();
// ------------------------------------------// Sort decreasing and print
iList.sortDecreasing();
System.out.println( "Sorted Decreasing:" );
iList.print();
// ------------------------------------------// Binary Search Decreasing for 20 and print
// resulting index
System.out.println( "Where is 20? " + iList.binarySearchD( 20 ) );
// ------------------------------------------// Binary Search Decreasing for 395 and print
System.out.println( "Where is 395? " + iList.binarySearchD( 395 ) );
}
}
Sample Output #1
Initial Array:
[0] = *20* [1] = *268* [2] = *1* [3] = *72* [4] = *84*
[5] = *20* [6] = *268* [7] = *38* [8] = *83* [9] = *17*
[10] = *6* [11] = *268* [12] = *18* [13] = *68* [14] = *66*
[15] = *21* [16] = *268* [17] = *76* [18] = *95* [19] = *95*
Replaced FIRST 20 with 200:
[0] = *200* [1] = *268* [2] = *1* [3] = *72* [4] = *84*
[5] = *20* [6] = *268* [7] = *38* [8] = *83* [9] = *17*
[10] = *6* [11] = *268* [12] = *18* [13] = *68* [14] = *66*
[15] = *21* [16] = *268* [17] = *76* [18] = *95* [19] = *95*
Replaced ALL 268 with 9:
[0] = *200* [1] = *9* [2] = *1* [3] = *72* [4] = *84*
[5] = *20* [6] = *9* [7] = *38* [8] = *83* [9] = *17*
[10] = *6* [11] = *9* [12] = *18* [13] = *68* [14] = *66*
[15] = *21* [16] = *9* [17] = *76* [18] = *95* [19] = *95*
Page 5 of 6
Searching & Sorting
Lab 6g: Searching and Sorting An Integer List
Sorted Decreasing:
[0] = *200* [1] = *95* [2] = *95* [3] = *84* [4] = *83*
[5] = *76* [6] = *72* [7] = *68* [8] = *66* [9] = *38*
[10] = *21* [11] = *20* [12] = *18* [13] = *17* [14] = *9*
[15] = *9* [16] = *9* [17] = *9* [18] = *6* [19] = *1*
Where is 20? 11
Where is 395? -1
Page 6 of 6
Searching & Sorting