SHOW:
|
|
- or go back to the newest paste.
1 | - | #include<stdio.h> |
1 | + | #include <stdio.h> |
2 | - | main() |
2 | + | |
3 | - | { |
3 | + | int main() |
4 | - | int array[100], search, c, number; |
4 | + | { |
5 | - | |
5 | + | int c, first, last, middle, n, search, array[100]; |
6 | - | printf("Enter the number of elements in array: \n"); |
6 | + | |
7 | - | scanf("%d",&number); |
7 | + | printf("Enter number of elements :\n"); |
8 | - | |
8 | + | scanf("%d",&n); |
9 | - | printf("Enter %d numbers : \n", number); |
9 | + | |
10 | - | for ( c = 0 ; c < number ; c++ ) |
10 | + | printf("Enter %d integers :\n", n); |
11 | - | scanf("%d",&array[c]); |
11 | + | |
12 | - | |
12 | + | for (c = 0; c < n; c++) |
13 | - | printf("Enter the number to Search : \n"); |
13 | + | scanf("%d",&array[c]); |
14 | - | scanf("%d",&search); |
14 | + | |
15 | - | |
15 | + | printf("Enter value to find:\n"); |
16 | - | for ( c = 0 ; c < number ; c++ ) |
16 | + | scanf("%d", &search); |
17 | - | { |
17 | + | |
18 | - | if ( array[c] == search ) |
18 | + | first = 0; |
19 | - | { |
19 | + | last = n - 1; |
20 | - | printf("%d - is present at location - %d\n", search, c+1); |
20 | + | middle = (first+last)/2; |
21 | - | break; |
21 | + | |
22 | - | } |
22 | + | while (first <= last) { |
23 | if (array[middle] < search) | |
24 | - | |
24 | + | first = middle + 1; |
25 | - | if ( c == number ) |
25 | + | else if (array[middle] == search) { |
26 | - | printf("%d - is not present in array.\n", search); |
26 | + | printf("%d - found at location - %d.\n", search, middle+1); |
27 | break; | |
28 | - | |
28 | + | } |
29 | - | } |
29 | + | else |
30 | last = middle - 1; | |
31 | ||
32 | middle = (first + last)/2; | |
33 | } | |
34 | if (first > last) | |
35 | printf("Not found! %d - is not present in the list.\n", search); | |
36 | ||
37 | return 0; | |
38 | } |