Program Sequential Search - JAVA

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Random;

public class Sequential_Search
{
    public static void main(String[] args) throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader dataIn = new BufferedReader(isr);
        
        System.out.print("Masukkan jumlah Data : ");   
        int jlh_data = Integer.parseInt(dataIn.readLine());
        
        int[] data = new int[jlh_data];
        
        System.out.println("\nPENGISIAN DATA");
        System.out.println("1. Di input oleh user");
        System.out.print("2. Di input oleh program --> Pilihan [1 / 2]: ");
        int isi = Integer.parseInt(dataIn.readLine());
        switch(isi)
        {
            case 1    :
               
                System.out.println();
                for(int a = 0; a < jlh_data; a++)
                   {
                      System.out.print("Data ke-"+(a+1)+" : ");       
                      data[a] = Integer.parseInt(dataIn.readLine());
                   }
                   break;
                            
            case 2    :
               
                for(int a = 0; a < jlh_data; a++)
                data[a] = new Random().nextInt(201);        
               
                System.out.println("\nTampilkan Data");
                for(int a = 0; a < jlh_data; a++)
                System.out.println("Data ke-"+(a+1)+" : "+data[a]);
                        
                break;
                            
           default    :
              
               System.out.println("Pilihan tidak tersedia");
        }
        
        System.out.print("\nInput Data yang dicari : ");   
        int cari = Integer.parseInt(dataIn.readLine());
        
        boolean temu = false;
        int urutan = 0;
        for(int a = 0; a < jlh_data; a++)
        {
            System.out.print(data[a]+" == "+cari);
            if(data[a] == cari)
            {
                System.out.println("    --> Data ketemu");
                urutan = a+1;
                temu = true;
                break;
            }
            else
            System.out.println("    --> Data belum ketemu");
            }
       
        if(temu == true)
        System.out.println("\nData ditemukan pada urutan ke-"+(urutan));
        
        else
        System.out.println("\nData tidak ditemukan");
    }
}

BACK

No comments:

Post a Comment