-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
59 lines (55 loc) · 1018 Bytes
/
helpers.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
/**
* helpers.c
*
* Computer Science 50
* Problem Set 3
*
* Helper functions for Problem Set 3.
*/
#include <cs50.h>
#include "helpers.h"
/**
* Returns true if value is in array of n values, else false.
*/
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;
}
else if (values[min + (n - min)/2] < value)
{
min = min + (n - min)/2;
}
else
{
return true;
}
}
return false;
}
/**
* Sorts array of n values.
*/
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;
}