Last active
July 19, 2018 18:40
-
-
Save cyrilCodePro/0fc60d0046784e17bad6b16ee965f265 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Arrays; | |
public class LinearArray | |
{ | |
private int[] data; // array of values | |
private static final Random generator = new Random(); | |
public LinearArray( int size ) | |
{ | |
data = new int[ size ]; | |
for ( int i = 0; i < size; i++ ) | |
data[ i ] = 10 + generator.nextInt( 90 ); | |
} | |
public int linearSearch( int searchKey ) | |
{ | |
// loop through array sequentially | |
for ( int index = 0; index < data.length; index++ ) | |
if ( data[ index ] == searchKey ) | |
return index; | |
// return index of integer | |
return -1; | |
// integer was not found | |
} | |
public String toString() | |
{ | |
return Arrays.toString( data ); | |
} // end method toString | |
} // | |
import java.util.Scanner; | |
public class LinearSearchTest | |
public static void main( String[] args ) | |
{ | |
// create Scanner object to input data | |
Scanner input = new Scanner( System.in ); | |
int searchInt; | |
// search key | |
int position; | |
// create array and output it | |
LinearArray searchArray = new LinearArray( 10 ); | |
System.out.println( searchArray + "\n" ) | |
// get input from user | |
System.out.print( | |
"Please enter an integer value (-1 to quit): " ); | |
searchInt = input.nextInt(); // read first int from user | |
// repeatedly input an integer; -1 terminates the program | |
while ( searchInt != -1 ) | |
{ | |
// perform linear search | |
position = searchArray.linearSearch( searchInt ); | |
if ( position == -1 ) // integer was not found | |
System.out.println( "The integer " + searchInt + | |
" was not found.\n" ); | |
else // integer was found | |
System.out.println( "The integer " + searchInt + | |
" was found in position " + position + ".\n" ); | |
// get input from user | |
System.out.print( | |
"Please enter an integer value (-1 to quit): " ); | |
searchInt = input.nextInt(); // read next int from user | |
} // end while | |
} // end main | |
} // end class LinearSear | |
import java.util.Random; | |
import java.util.Arrays; | |
public class BinaryArray | |
{ | |
private int[] data; // array of values | |
private static final Random generator = new Random(); | |
// create array of given size and fill with random integers | |
public BinaryArray( int size ) | |
{ | |
data = new int[ size ]; // create space for array | |
// fill array with random ints in range 10-99 | |
for ( int i = 0; i < size; i++ ) | |
data[ i ] = 10 + generator.nextInt( 90 ); | |
Arrays.sort( data ); | |
} // end BinaryArray constructor | |
// perform a binary search on the data | |
public int binarySearch( int searchElement ) | |
{ | |
// print remaining elements of array | |
System.out.print( remainingElements( low, high ) ); | |
// output spaces for alignment | |
for ( int i = 0; i < middle; i++ ) | |
System.out.print( " " ); | |
System.out.println( " * " ); // indicate current middle | |
// method to output certain values in array | |
public String remainingElements( int low, int high ) | |
{ | |
StringBuilder temporary = new StringBuilder(); | |
// output spaces for alignment | |
for ( int i = 0; i < low; i++ ) | |
temporary.append( " " ); | |
// output elements left in array | |
for ( int i = low; i <= high; i++ ) | |
temporary.append( data[ i ] + " " ); | |
temporary.append( "\n" ); | |
return temporary.toString(); | |
} // end method remainingElements | |
// method to output values in array | |
public String toString() | |
{ | |
return remainingElements( 0, data.length - 1 ); | |
} // end method toString | |
} / | |
import java.util.Scanner; | |
public class BinarySearchTest | |
{ | |
public static void main( String[] args ) | |
{ | |
// create Scanner object to input data | |
Scanner input = new Scanner( System.in ); | |
int searchInt; // search key | |
int position; // location of search key in array | |
// create array and output it | |
BinaryArray searchArray = new BinaryArray( 15 ); | |
System.out.println( searchArray ); | |
// get input from user | |
System.out.print( | |
"Please enter an integer value (-1 to quit): " ); | |
searchInt = input.nextInt(); // read an int from user | |
System.out.println(); | |
// repeatedly input an integer; -1 terminates the program | |
while ( searchInt != -1 ) | |
{ | |
// use binary search to try to find integer | |
position = searchArray.binarySearch( searchInt ); | |
// return value of -1 indicates integer was not found | |
if ( position == -1 ) | |
System.out.println( "The integer " + searchInt + | |
" was not found.\n" ); | |
else | |
System.out.println( "The integer " + searchInt + | |
" was found in position " + position + ".\n" ); | |
// get input from user | |
System.out.print( | |
"Please enter an integer value (-1 to quit): " ); | |
searchInt = input.nextInt(); // read an int from user | |
System.out.println(); | |
} // end while | |
} // end main | |
} // end class BinarySearchTest |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment