int[] array = { 55, 33, 77, 88, 22 };
target
i :=
array[i]
System.out.println(array[i]);
if
for
int i; for ( i = 0; i < array.length && array[i] != target; i++ ) ; if ( i < array.length ) // this condition implies |array[i]| must be |target| ... // use |array[i]|
int i; for ( i = array.length - 1; i >= 0 && array[i] != target; i-- ) ; if ( ) // this condition implies |array[i]| must be |target| ... // use |array[i]|
i < array.length
i >= 0
$ java searching data: [ ] Enter value: not already among data; adding it data: [ 55 ] Enter value: not already among data; adding it data: [ 55 33 ] Enter value: not already among data; adding it data: [ 55 33 77 ] Enter value: already among data data: [ 55 33 77 ] Enter value: not already among data; adding it data: [ 55 33 77 88 ] Enter value: not already among data; adding it data: [ 55 33 77 88 22 ] Enter value: already among data data: [ 55 33 77 88 22 ] Enter value: That's all, eh? $
import java.util.Scanner; public class searching { static int search(int[] a, int target) { int result; for ( result = a.length - 1 ; result >= 0 && a[result] != target ; result-- ) ; return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] data = new int[0]; for ( ; ; ) { System.out.print("\ndata: "); display_array(data); System.out.print("Enter value: "); if ( !scanner.hasNextInt() ) { System.out.println("\nThat's all, eh?"); break; } int value = scanner.nextInt(); int s = search(data, value); if ( s >= 0 ) { // this condition implies |array[i]| must be |value| System.out.println("already among data"); continue; } // else { // didn't encounter |value| in array System.out.println("not already among data; adding it"); int[] newspace = new int[data.length + 1]; /* for ( int i = 0; i < data.length; i++ ) newspace[i] = data[i]; */ System.arraycopy(data, 0, newspace, 0, data.length); newspace[] = value; data = newspace; /* * |ArrayList| could handle this processing. * But someone has to develop, maintain |ArrayList| code; * what if it's you? * I'm presenting this material in case you might have to * work on such code. * (Think code never changes? Java 1.5 added type parameters * to |ArrayList| etc.) */ // } } } static void display_array(int[] a) { System.out.print("[ "); for ( int v : a ) System.out.println(v + " "); System.out.println(']'); } }
$ java searching_s array: [ ] Enter value: 55 not already in array; adding it array: [ 55 ] Enter value: 33 not already in array; adding it array: [ 33 55 ] Enter value: 77 not already in array; adding it array: [ 33 55 77 ] Enter value: 33 already in array array: [ 33 55 77 ] Enter value: 88 not already in array; adding it array: [ 33 55 77 88 ] Enter value: 22 not already in array; adding it array: [ 22 33 55 77 88 ] Enter value: 77 already in array array: [ 22 33 55 77 88 ] Enter value: 44 already in array array: [ 22 33 44 55 77 88 ] Enter value: That's all, eh? $
import java.util.Scanner; public class searching_s { static int search_s(int[] a, int target) { int result; for ( result = a.length - 1 ; result >= 0 && a[result] target ; result-- ) ; return result; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int[] data = new int[0]; for ( ; ; ) { // 'forever' System.out.print("\ndata: "); display_array(data); System.out.print("Enter value: "); if ( !scanner.hasNextInt() ) { System.out.println("\nThat's all, eh?"); break; } int value = scanner.nextInt(); int s = search_s(data, value); if ( s >= 0 && array[s] == value ) { System.out.println("already among data"); continue; } // else { System.out.println("not already in array; adding it"); // maintaining array sorted // insert |value| // int[] newspace = new int[data.length + 1]; // First, copy elements through position |s| // because |array[s] < value|: /* for ( int i = 0; i s; i++ ) newspace[i] = data[i]; */ System.arraycopy(data, 0, newspace, 0, ); newspace[] = value; /* for ( int i = s + 1; i < data.length; i++ ) newspace[i + 1] = data[i]; */ System.arraycopy( data, s + 1, newspace, (s + 1) + 1, data.length - (s + 1) ); data = newspace; // |ArrayList| could further handle such insertion // in middle of sequence; but I'm showing you... // } } } static void display_array(int[] a) { System.out.print("[ "); for ( int i = 0; i < a.length; i++ ) System.out.print(a[i] + " "); System.out.println(']'); } }
** ** ** ** ** ** ** ** ** ** ** ** ** ** **
** ** ** ** ** ** ** 55 ** ** ** ** ** ** **
** ** ** ** ** ** **
** ** ** 77 ** ** **
** ** **
/** * Performs the standard binary search. * @return index where item is found, or -1 if not found * @author Mark Allen Weiss and Hugh McGuire */ static int binary_search(int[] a, int target) { int middle = -1; for ( int low = 0, high = a.length - 1; low <= high; ) { middle = (low + high) / 2; if ( target < a[middle] ) high = middle - 1; else if ( a[middle] < target ) { low = middle + 1; middle = middle + 1; /* * adjust |middle| in case will stop if this new |low| * is greater than |high| * so insertion index |middle| will be * past element less than |target| */ } else // must have |a[middle] == target| --- found it! break; } return middle; }
binary_search(array, value)
search_s(array, value)
a.length
**********************************************************
a[middle] < target
*****************************
**************
/** * Performs the standard binary search. * @return index where item is found, or -1 if not found * @author Mark Allen Weiss and Hugh McGuire */ static int binary_search(int[] a, int low, int high, int target) { if ( low > high ) return -1; int middle = (low + high) / 2; if ( target < a[middle] ) return binary_search(a, low, middle - 1, target); if ( a[middle] < target ) return binary_search(a, middle + 1, high, target); return middle; }