-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinsearch.c
More file actions
48 lines (46 loc) · 1.04 KB
/
binsearch.c
File metadata and controls
48 lines (46 loc) · 1.04 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
#include <stdio.h>
#define N 9
/**
* Binaer Suche
* als die Forauszetzung nutzt man ein sortiertes Array
*/
int binsearch (int arr[], int links, int rechts, int wert)
{
int pivot = (rechts+links)/2;
/*Element wurde nicht gefunden */
if (links > rechts)
{
printf("->Fail...\n");
return 0;
}
/* Element wurde getroffen */
if (arr[pivot] == wert)
{
printf("->Bingo!!!\n");
return 0;
}
/* Suchverhalten geht nach links */
if (wert < arr[pivot])
{
printf("->links");
return binsearch(arr, links, pivot - 1, wert);
}
/* Suchverhalten geht nach rechts */
else
{
printf("->rechts");
return binsearch(arr, pivot + 1, rechts, wert);
}
}
int main()
{
int arr[N]={0,2,3,6,7,8,9,12,15};
int i;
for(i=0;i<N;i++)
printf("%d ",arr[i]); /* Ausgabe unseres Array */
printf("\ni=");
scanf("%d",&i);
int size=sizeof arr/sizeof(int); /* eigentlich ist gleich N */
binsearch(arr,0,size-1,i);
return 0;
}