-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfib search.c
More file actions
99 lines (75 loc) · 1.48 KB
/
fib search.c
File metadata and controls
99 lines (75 loc) · 1.48 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
#include <stdio.h>
int min(int x, int y)
{
if(x<=y)
return x;
else
return y;
}
int findit(int arr[], int x, int n)
{
int no_of_comp=0;
int no2 = 0;
int no1 = 1;
int no = no2 + no1;
while (no < n)
{
no2 = no1;
no1 = no;
no = no2 + no1;
}
int flag= -1;
while (no> 1)
{
int i = min(flag+no2, n-1);
++no_of_comp;
if (arr[i] < x)
{
no =no1;
no1 = no2;
no2 = no- no1;
flag = i;
}
else if (arr[i] > x)
{
no = no2;
no1 = no1 - no2;
no2 =no- no1;
}
else
{ printf("Element found in array\n");
printf("Element found after %d comparisons\n",no_of_comp);
return 0;
}
}
if(no1 && arr[flag+1]==x)
return flag+1;
return -1;
}
int main(void)
{
int n,i,arr[20],x,j,temp;
printf("Enter number of elements: ");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=1;i<=n;i++)
{
scanf("%d",&arr[i]);
}
for (i = 0; i < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (arr[i] > arr[j])
{
temp= arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("Enter the number to be searched: ");
scanf("%d",&x);
findit(arr, x, n);
return 0;
}