Advertisement
urksiful

Secuencial Search

Nov 22nd, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3. import java.util.Scanner;
  4.  
  5. public class SecuencialSearch {
  6.  
  7.     public static void main(String[] args) {
  8.         SecuencialSearch pq = new SecuencialSearch();
  9.         Scanner x = new Scanner(System.in);
  10.         ArrayList<Integer> elements = new ArrayList<Integer>();
  11.         int nc = 0;
  12.         while (nc < 100) {
  13.             elements.add((int) (Math.random() * 200) + 1);
  14.             System.out.print(elements.get(nc) + " ");
  15.             nc++;
  16.         }
  17.         nc = 0;
  18.         System.out.println("");
  19.         int sec = x.nextInt();
  20.         boolean founded = false;
  21.  
  22.         while (nc < 100) {
  23.             if (sec == elements.get(nc)) {
  24.                 founded = true;
  25.                 break;
  26.             }
  27.             nc++;
  28.         }
  29.         if (founded) {
  30.             System.out.println("Se encontro en " + (nc+1) + " comparaciones "
  31.                     + "\n en la posicion " + nc);
  32.         }else{
  33.             System.out.println("Jajajaja :v");
  34.         }
  35.        
  36.        
  37.        
  38.     }
  39.  
  40.  
  41.  
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement