-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfuncts.c
executable file
·49 lines (43 loc) · 1.27 KB
/
functs.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
/** functs.c
* ===========================================================
* Name: CS220
* Section:
* Project: Linear vs Binary Search (List ADT w/Array)
* ===========================================================
*/
#include <stdio.h>
#include <time.h>
#include "functs.h"
int linearSearch(unsigned long long int arr[], unsigned long long int n, unsigned long long int x) {
for(int i = 0; i < n; i++) {
if (arr[i] == x)
return i;
}
return -1; // element not found
}
int binarySearch(unsigned long long int arr[], unsigned long long int l, unsigned long long int r, unsigned long long int x) {
while (l <= r) {
int m = l + (r - l) / 2; // To avoid overflow
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1; // element not found
}
void subTimespec(struct timespec t1, struct timespec t2, struct timespec *td) {
td->tv_nsec = t2.tv_nsec - t1.tv_nsec;
td->tv_sec = t2.tv_sec - t1.tv_sec;
if (td->tv_sec > 0 && td->tv_nsec < 0)
{
td->tv_nsec += NS_PER_SECOND;
td->tv_sec--;
}
else if (td->tv_sec < 0 && td->tv_nsec > 0)
{
td->tv_nsec -= NS_PER_SECOND;
td->tv_sec++;
}
}