-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-search.c
73 lines (61 loc) · 1.44 KB
/
binary-search.c
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
#include <stdio.h>
#include <stdlib.h>
void selection(int sSort[1000], int len){
int i, j, minIndex, temp, smallest;
for(i = 0; i < len; i++){
smallest = sSort[i];
minIndex = i;
for(j = i + 1; j < len; j++){
if(sSort[j] < smallest){
smallest = sSort[j];
minIndex = j;
}
}
if(minIndex > i){
sSort[minIndex] = sSort[i];
sSort[i] = smallest;
}
}
}
/**
* Ikili arama algoritmasiyla eleman bulma
* @dizi : arama yapilacak dizi
* @N : dizinin uzunlugu
* @ara : aranacak eleman
*/
int ikiliarama(int dizi[1000], int N, int ara){
int bas = 0;
int son = N-1;
int orta;
while(bas <= son){
orta = (son + bas) / 2;
if(dizi[orta] == ara){
return orta;
}else if(dizi[orta] > ara){
son = orta-1;
}else{
bas = orta+1;
}
}
return -1;
}
int main()
{
int d[1000], i, N, sonuc;
printf("Dizi kac elemanli olacak > ");
scanf("%d", &N);
for(i = 0; i < N; i++){
printf("%d. eleman > ", (i + 1));
scanf("%d", &d[i]);
}
printf("Aranacak elemani girin > ");
scanf("%d", &i);
selection(d, N);
sonuc = ikiliarama(d, N, i);
if(sonuc == -1){
printf("Aradiginiz eleman dizide bulunamadi.\n");
}else{
printf("Aradiginiz elemannin indexi: %d\n.", sonuc);
}
return 0;
}