2012420062, Yudha S.P, Tugas 14 (Binary Search), Semester 3

2012420062, Yudha S.P, Tugas 14 (Binary Search), Semester 3

import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Administrator
 */
public class binsearch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
     
      String input=JOptionPane.showInputDialog(null, "Masukkan Inputan");
      String []y=input.split(",");
      String z=JOptionPane.showInputDialog(null, "Cari Nilai Data");
        int awal  = 0;
        int n=y.length;
        int lihat=Integer.parseInt(z);
 
        int akhir   = n - 1;
   int tengah = (awal + akhir)/2;
    while( awal <= akhir ){
      if ( Integer.parseInt(y[tengah]) < lihat)
        awal=tengah + 1;   
     else if ( Integer.parseInt(y[tengah]) == lihat ) {
          System.out.println("Nilai "+lihat+ " Ada di Index " + (tengah + 1));
        break;
        }
         else
         akhir = tengah - 1;
      tengah = (awal + akhir)/2;
   }
   if ( awal > akhir ){
       System.out.println("Nilai "+lihat+" Not Found");
    }
    }        
    }















share this article to: Facebook Twitter Google+ Linkedin Technorati Digg
Posted by arek informatika, Published at 13.27 and have 0 komentar

Tidak ada komentar:

Posting Komentar