-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram_v2.py
43 lines (36 loc) · 1.08 KB
/
program_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# Proses Pencarian Sequential Search
def sequential_search(nama, data):
for i in range(len(data)):
if data[i][0] == nama:
return data[i][1]
return "Tidak ditemukan"
# Proses Pencarian Binary Search
def binary_search(nama, data):
low = 0
high = len(data) - 1
while low <= high:
mid = (low + high) // 2
if data[mid][0] == nama:
return data[mid][1]
elif data[mid][0] < nama:
low = mid + 1
else:
high = mid - 1
return "Tidak ditemukan"
# Contoh Data
data = [["budi", "LULUS"],
["andi", "TIDAK LULUS"],
["tini", "LULUS"],
["cindi", "LULUS"],
["tono", "TIDAK LULUS"],
["joko", "LULUS"]]
# Sorting Data (Wajib untuk Binary Search)
data.sort(key=lambda x: x[0])
# Contoh Pencarian
nama = "joko"
# Output Pencarian Sequential Search
result_seq = sequential_search(nama, data)
print(f"Hasil Pencarian Sequential Search: {result_seq}")
# Output Pencarian Binary Search
result_bin = binary_search(nama, data)
print(f"Hasil Pencarian Binary Search: {result_bin}")