-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkf.c
73 lines (69 loc) · 1.43 KB
/
workf.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 <cs50.h>
#include <stdio.h>
#include <stdlib.h>
void sort(int values[], int n);
bool search(int value, int values[], int n);
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./find needle\n");
return -1;
}
int needle = atoi(argv[1]);
int size = 9;
int haystack[65536] = { 2, 4, 5, 7, 9, 1, 3, 6, 8 };
sort(haystack, size);
printf("ok");
if (search(needle, haystack, size))
{
printf("\nFound needle in haystack!\n\n");
return 0;
}
else
{
printf("\nDidn't find needle in haystack.\n\n");
return 1;
}
}
bool search(int value, int values[], int n)
{
int min = 0;
for (int i = 0, in = n/2; i < in; i++)
{
if (values[min + (n-min)/2] > value)
{
n = min + (n - min)/2;
printf("%i n", n);
}
else if (values[min + (n - min)/2] < value)
{
min = min + (n - min)/2;
printf("%i min", min);
}
else
{
return true;
}
}
return false;
}
void sort(int values[], int n)
{
int min = values[0];
int t;
for (int i = 0; i < n; i++)
{
for (int j = i; j < n; j++)
{
if (values[j] < values[min])
{
min = j;
}
}
t = values[i];
values[i] = values[min];
values[min] = t;
}
return;
}