Home > Java, Ren's Free Time > Algoritma Linear Search dengan Java

Algoritma Linear Search dengan Java

September 20, 2012 Leave a comment Go to comments

Algoritma Linear Search adalah algoritma pencarian yang paling sederhana. Linear Search bekerja dengan membandingkan nilai yang dicari dengan setiap element pada array ( pada umumnya ) secara sequen. Berikut ini implementasi algoritma linear search pada bahasa pemrograman Java yang terdiri dari 2 file .java

Nama file : LinearGenerator.java

package linearsearch;

import java.util.Random;

/**
 *
 * @author Taeyeon
 */
public class LinearGenerator {

    private int [] data;
    static private Random randomValue = new Random();
    
    public LinearGenerator(int sizeArray)
    {
        data = new int [ sizeArray];
        for(int i=0;i<sizeArray;i++)
        {
            data[i]= 1 + randomValue.nextInt(125);
        }
    }
    
    public int linearSearch(int searchKey)
    {
        for(int index=0; index < data.length; index++)
            if(data[index]== searchKey)
                return index;
        return -1;
    }
    
    @Override
    public String toString()
    {
        StringBuffer temp = new StringBuffer();
        for(int element:data)
        
            temp.append(element + " ");
            temp.append("\n");
            
            return temp.toString();
        
    }
}

Nama file : Main.java

package linearsearch;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
/**
 *
 * @author Taeyeon
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        
        
        int searchKey;
        int position;
        int arrayLength;
        
        arrayLength = 25;
        
        LinearGenerator searchArray = new LinearGenerator(arrayLength);
        System.out.println(searchArray);
        
        System.out.println("Masukkan nilai yang akan dicari:");
        searchKey = Integer.parseInt(input.readLine());
        
        while (searchKey != -1)
        {   
            position = searchArray.linearSearch(searchKey);
            
            if(position == -1)
                System.out.println("Nilai tidak ditemukan");
            else
                System.out.println("Nilai "+searchKey+" ditemukan di index "+(position+1));
            
            System.out.println("input -1 untuk keluar");
            searchKey = Integer.parseInt(input.readLine());
        }
    }

}

 

Contoh keluar program

Linear Search

Semoga bermanfaat.😀

Referensi : Dari berbagai sumber.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: