Java search - } a[loc] = num; num ++; } //=----------Search...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
/** * @(#)Test_Search.java * * * @author * @version 1.00 2008/4/21 */ import java.util.*; public class Test_Search { public static void main(String[] args){ int loc; //range from 0 to 999 int num; //range from 0 to 999 int final EMPTY = -1; int a[] = new int[1000]; //EMPTY the array. for(int rw = 0; rw<a.length; rw++;){ //process each row a[rw] = EMPTY } //Fill the array with number 0-999 while (/*I still have numbers to place*/ ){ while(/*while the location is not empty*/ a[loc] != EMPTY){ //Find a random location that is empty loc= Math.floor(Math.random() * a.length);
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } a[loc] = num; num ++; } //=----------Search for the location of a number System.out.println("The location of number = 500 is at: " + linearSearch(500)); //--------------Search for a number that is not there System.out.println("The location of number = 500 is : " + linearSearch(a, 1500)); //Linear Search Method public int linearSearch(int a, int num){ location = -1; for(int col = 0; col<a.length; col++;){ if(a[col] == num){ location = col; } } return location; } } }...
View Full Document

This note was uploaded on 05/01/2008 for the course ECE 172 taught by Professor Olabe during the Spring '08 term at Christian Brothers.

Page1 / 2

Java search - } a[loc] = num; num ++; } //=----------Search...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online