-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartSearchApp.java
More file actions
101 lines (83 loc) · 2.96 KB
/
SmartSearchApp.java
File metadata and controls
101 lines (83 loc) · 2.96 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class SmartSearchApp {
private Scanner scanner = new Scanner(System.in);
private Random random = new Random();
private ArrayList<Integer> tahminListesi = new ArrayList<>();
public static void main(String[] args) {
SmartSearchApp app = new SmartSearchApp();
app.calistir();
}
public void calistir() {
System.out.println("=== AKILLI ARAMA VE JSON KAYIT SİSTEMİ ===");
System.out.print("Dizi kaç elemanlı olsun?: ");
if (!scanner.hasNextInt()) {
System.out.println("Lütfen sayı girin!");
return;
}
int boyut = scanner.nextInt();
int[] sayilar = new int[boyut];
diziyiDoldur(sayilar);
System.out.println("Sıralanıyor...");
bubbleSort(sayilar);
System.out.println("Sıralı Dizi: " + Arrays.toString(sayilar));
System.out.print("Hangi sayıyı arayalım?: ");
int aranan = scanner.nextInt();
tahminListesi.clear();
int sonucIndex = binarySearch(sayilar, aranan);
if (sonucIndex != -1) {
System.out.println("✅ Bulundu! İndeks: " + sonucIndex);
} else {
System.out.println("❌ Sayı dizide yok.");
}
raporuKaydet();
}
public int binarySearch(int[] dizi, int hedef) {
int sol = 0;
int sag = dizi.length - 1;
while (sol <= sag) {
int orta = sol + (sag - sol) / 2;
int tahmin = dizi[orta];
tahminListesi.add(tahmin);
System.out.println("Bilgisayar tahmini: " + tahmin);
if (tahmin == hedef) return orta;
if (tahmin < hedef) sol = orta + 1;
else sag = orta - 1;
}
return -1;
}
public void raporuKaydet() {
try {
FileWriter yazar = new FileWriter("arama_gecmisi.json");
yazar.write("{\n");
yazar.write(" \"algoritma\": \"Binary Search\",\n");
yazar.write(" \"izlenen_yol\": " + tahminListesi.toString() + "\n");
yazar.write("}");
yazar.close();
System.out.println("📄 Rapor 'arama_gecmisi.json' dosyasına yazıldı.");
} catch (IOException e) {
System.out.println("Hata: " + e.getMessage());
}
}
public void diziyiDoldur(int[] dizi) {
for (int i = 0; i < dizi.length; i++) {
dizi[i] = random.nextInt(100);
}
}
public void bubbleSort(int[] dizi) {
int temp;
for (int i = 0; i < dizi.length - 1; i++) {
for (int j = 0; j < dizi.length - i - 1; j++) {
if (dizi[j] > dizi[j + 1]) {
temp = dizi[j];
dizi[j] = dizi[j + 1];
dizi[j + 1] = temp;
}
}
}
}
}