Algorithma Search Linier,...
you can to try
package myPackage;
import java.util.Scanner;
//class node for found index
class Node {
int data, index;
Node next;
public Node(int data, int index) {
this.data = data;
this.index = index;
}
}
// i'm use linklist
class LinkedList {
Node head, tail;
void addLast(int nilai, int id) {
Node input = new Node(nilai, id);
if (head == null) {
head = tail = input;
} else {
tail.next = input;
tail = input;
}
}
void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
void searching(int key) {
Node temp = head;
System.out.println("\nProses Pencarian Angka " + key);
while (temp != null) {
if (temp.data == key) {
System.out.println("Status : ditemukan di indeks ke-"
+ temp.index);
break;
} else
temp = temp.next;
}
if (temp == null)
System.out.println("Status : tak ditemukan!!");
}
}
public class LinearSearch {
public static void main(String[] args) {
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
System.out.println(" Linear Search\n ");
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
LinkedList list = new LinkedList();
Scanner input = new Scanner(System.in);
System.out.print("Jumlah angka = ");
int jumlah = input.nextInt();
input = new Scanner(System.in);
System.out.println("Masukkan angka :");
int angka;
for (int i = 0; i < jumlah; i++) {
System.out.print(">> ");
angka = input.nextInt();
list.addLast(angka, i); // nama team disimpan dalam queue
}
list.display();
System.out.print("\nDicari : ");
int cari = input.nextInt();
list.searching(cari);
System.out.println();
}
}
good luck for you
No comments:
Post a Comment