-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlab05.c
executable file
·47 lines (41 loc) · 1.89 KB
/
lab05.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
/** lab05.c
* ===========================================================
* Name: CS229
* Section:
* Project: Linear vs Binary Search (List ADT w/Array)
* ===========================================================
*/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "functs.h"
int main() {
// Allocate mem for the array and also setup timing variables
unsigned long long int *array = malloc(MAX_SIZE * sizeof(unsigned long long));
struct timespec startn, finishn, delta;
// Initialize the array with sorted values
for (unsigned long long int i = 0; i < MAX_SIZE; i++) {
array[i] = i;
}
// Testing Linear Search
printf(" Search n time \n");
printf("--------------------------------\n");
for (unsigned long long int i = MAX_SIZE / 10; i <= MAX_SIZE; i+=MAX_SIZE / 10) {
clock_gettime(CLOCK_REALTIME, &startn); // get a time hack prior to search
linearSearch(array, i, i * .99); // running the search with the item to find near the end
clock_gettime(CLOCK_REALTIME, &finishn); // time hack after search
subTimespec(startn, finishn, &delta); // do the math on the time
printf(" linear %10ld %d.%.9ld\n", (long int)i, (int)delta.tv_sec, delta.tv_nsec);
}
// Testing Binary Search
printf(" Search n time \n");
printf("--------------------------------\n");
for (unsigned long long int i = MAX_SIZE / 10; i <= MAX_SIZE; i+=MAX_SIZE / 10) {
clock_gettime(CLOCK_REALTIME, &startn); // get a time hack prior to search
binarySearch(array, 0, i, i * .99); // get a time hack prior to search
clock_gettime(CLOCK_REALTIME, &finishn); // time hack after search
subTimespec(startn, finishn, &delta); // do the math on the time
printf(" binary %10ld %d.%.9ld\n", (long int)i, (int)delta.tv_sec, delta.tv_nsec);
}
return 0;
}