Skip to content

Commit

Permalink
implement algorithm_merge with 2 example in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
KaisenAmin committed Jan 8, 2024
1 parent 3cd26aa commit 77f1d87
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 19 deletions.
118 changes: 118 additions & 0 deletions algorithm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3822,4 +3822,122 @@ int main () {

return 0;
}
```

## Example 124 : How to use `algorithm_merge`
C Algorithm sort time: 0.000002 seconds

```c
#include "algorithm/algorithm.h"
#include "array/array.h"
#include <stdio.h>
#include <time.h>

int compare_ints(const void* a, const void* b) {
int one = *(const int*)a;
int two = *(const int*)b;

return (one > two) - (one < two);
}

void print_int(void* number) {
printf("%d ", *(int*)number);
}

int main() {
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);

int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
size_t size_first = sizeof(first) / sizeof(first[0]);
size_t size_second = sizeof(second) / sizeof(first[0]);
Array* arr = array_create(sizeof(int), 10);

algorithm_sort(first, size_first, sizeof(int), compare_ints);
algorithm_sort(second, size_second, sizeof(int), compare_ints);
algorithm_merge(first, size_first, second, size_second, sizeof(int), array_begin(arr), compare_ints);

clock_gettime(CLOCK_MONOTONIC, &end);

double timeTaken = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;
printf("C Algorithm sort time: %f seconds\n", timeTaken);

array_deallocate(arr);
return 0;
}
```
`C++ Time Take : 4.375e-06 seconds`
```cpp
#include <iostream> // For std::cout
#include <algorithm> // For std::merge, std::sort
#include <vector> // For std::vector
#include <chrono> // For std::chrono
int main () {
auto start = std::chrono::high_resolution_clock::now();
int first[] = {5,10,15,20,25};
int second[] = {50,40,30,20,10};
std::vector<int> v(10);
std::sort (first, first + 5);
std::sort (second, second + 5);
std::merge (first, first + 5, second, second + 5, v.begin());
auto stop = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = stop - start;
std::cout << "Time taken: " << duration.count() << " seconds\n";
return 0;
}
```

## Example 125 : Point Struct with Using `algorithm_merge`

```c
#include "algorithm/algorithm.h"
#include "array/array.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
int x;
int y;
} Point;

int compare_points(const void* a, const void* b) {
Point point1 = *(const Point*)a;
Point point2 = *(const Point*)b;

return (point1.x > point2.x) - (point1.x < point2.x);
}

void print_point(void* p) {
Point* point = (Point*)p;
printf("(%d, %d) ", point->x, point->y);
}

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);

for (size_t i = 0; i < array_size(arr); i++) {
print_point(array_at(arr, i));
}

array_deallocate(arr);
return 0;
}

```
33 changes: 32 additions & 1 deletion algorithm/algorithm.c
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ void algorithm_rotate(void *first, void *middle, void *last, size_t size) {
swap(first, next, size);
first = (char *)first + size;
next = (char *)next + size;

if (next == (char *)last) {
next = (char *)middle;
}
Expand Down Expand Up @@ -723,4 +723,35 @@ void algorithm_rotate_copy(const void *first, const void *middle, const void *la
result_ptr += size;
first_ptr += size;
}
}

void algorithm_merge(const void *base1, size_t num1, const void *base2, size_t num2, size_t size, void *result, CompareFunc comp) {
size_t i = 0, j = 0, k = 0;
const char *a = (const char *)base1;
const char *b = (const char *)base2;
char *res = (char *)result;

while (i < num1 && j < num2) {
if (comp(a + i * size, b + j * size) <= 0) {
memcpy(res + k * size, a + i * size, size);
i++;
}
else {
memcpy(res + k * size, b + j * size, size);
j++;
}
k++;
}

while (i < num1) {
memcpy(res + k * size, a + i * size, size);
i++;
k++;
}

while (j < num2) {
memcpy(res + k * size, b + j * size, size);
j++;
k++;
}
}
47 changes: 30 additions & 17 deletions main.c
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;
}
}
2 changes: 1 addition & 1 deletion vector/vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Vector* vector_create(size_t itemSize) {
vec->capacitySize = 32; // Initial capacity
vec->itemSize = itemSize;

size_t initialPoolSize = 10000;
size_t initialPoolSize = 1000;
vec->pool = memory_pool_create(initialPoolSize);
if (!vec->pool) {
free(vec);
Expand Down

0 comments on commit 77f1d87

Please sign in to comment.