forked from KaisenAmin/c_std
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement algorithm_merge with 2 example in readme
- Loading branch information
1 parent
3cd26aa
commit 77f1d87
Showing
4 changed files
with
181 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,41 @@ | ||
#include "algorithm/algorithm.h" | ||
#include "vector/vector.h" | ||
#include "array/array.h" | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
bool less_int(const void *a, const void *b) { | ||
return *(int *)a < *(int *)b; | ||
} | ||
typedef struct { | ||
int x; | ||
int y; | ||
} Point; | ||
|
||
int main() { | ||
Vector* vec = vector_create(sizeof(int)); | ||
int compare_points(const void* a, const void* b) { | ||
Point point1 = *(const Point*)a; | ||
Point point2 = *(const Point*)b; | ||
|
||
for (size_t index = 1; index <= 10; index++) { | ||
vector_push_back(vec, &index); | ||
} | ||
return (point1.x > point2.x) - (point1.x < point2.x); | ||
} | ||
|
||
struct timespec start, end; | ||
clock_gettime(CLOCK_MONOTONIC, &start); | ||
do { | ||
void print_point(void* p) { | ||
Point* point = (Point*)p; | ||
printf("(%d, %d) ", point->x, point->y); | ||
} | ||
|
||
} while (algorithm_next_permutation(vector_begin(vec), vector_end(vec), sizeof(int), less_int)); | ||
clock_gettime(CLOCK_MONOTONIC, &end); | ||
int main() { | ||
Point first[] = {{1, 2}, {3, 4}, {5, 6}}; | ||
Point second[] = {{7, 8}, {9, 10}, {11, 12}}; | ||
size_t size_first = sizeof(first) / sizeof(first[0]); | ||
size_t size_second = sizeof(second) / sizeof(second[0]); | ||
Array* arr = array_create(sizeof(Point), size_first + size_second); | ||
|
||
algorithm_sort(first, size_first, sizeof(Point), compare_points); | ||
algorithm_sort(second, size_second, sizeof(Point), compare_points); | ||
algorithm_merge(first, size_first, second, size_second, sizeof(Point), array_begin(arr), compare_points); | ||
|
||
double elapsed = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9; | ||
printf("%lf", elapsed); | ||
for (size_t i = 0; i < array_size(arr); i++) { | ||
print_point(array_at(arr, i)); | ||
} | ||
|
||
array_deallocate(arr); | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters