Advertisement
mrScarlett

Sequential Search Investigation

Nov 27th, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Random;
  3.  
  4. public class sequTest{
  5.     public static void main (String[] args){
  6.         Random rand = new Random();
  7.        
  8.         int n [] = new int[1000000000];
  9.        
  10.         for (int x =0;x<n.length;x++){
  11.             n[x]=rand.nextInt(129) + 1;
  12.         }
  13.         Scanner input = new Scanner(System.in);
  14.         int count =0;
  15.         int x = input.nextInt();
  16.         boolean found=false;
  17.         int searches=0;
  18.         for (int i=0; i<n.length; i++) {
  19.             searches+=1;
  20.             if (n[i]==x){
  21.                 found=true;
  22.                 count+=1;
  23.                
  24.             }
  25.         }
  26.        
  27.         if (found==true){
  28.             System.out.println(x+"found "+count+"times");
  29.         }
  30.         System.out.println("searches"+searches);
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement