Added Quicksort implementation for generic data types.
This commit is contained in:
85
usage.c
85
usage.c
@@ -23,6 +23,7 @@
|
||||
|
||||
static int vector_usage();
|
||||
static int map_usage();
|
||||
static vector_order_t cmp_int_asc(const void *x, const void *y);
|
||||
|
||||
int main(void) {
|
||||
int st;
|
||||
@@ -38,6 +39,20 @@ int main(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
vector_order_t cmp_int_asc(const void *x, const void *y) {
|
||||
int x_int = *(const int*)x;
|
||||
int y_int = *(const int*)y;
|
||||
|
||||
if (x_int < y_int) return VECTOR_ORDER_LT;
|
||||
if (x_int > y_int) return VECTOR_ORDER_GT;
|
||||
|
||||
return VECTOR_ORDER_EQ;
|
||||
}
|
||||
|
||||
vector_order_t cmp_int_desc(const void *x, const void *y) {
|
||||
return cmp_int_asc(y, x);
|
||||
}
|
||||
|
||||
int vector_usage() {
|
||||
// Create a vector of 5 integers
|
||||
vector_result_t res = vector_new(5, sizeof(int));
|
||||
@@ -100,10 +115,80 @@ int vector_usage() {
|
||||
printf("Vector cleared (size should be 0): %zu\n\n", vector_size(vector));
|
||||
}
|
||||
|
||||
// Sort vector in ascending order
|
||||
int values[] = {5, 10, -9, 3, 1, 0, 4};
|
||||
for (size_t idx = 0; idx < 7; idx++) {
|
||||
vector_result_t sort_push_res = vector_push(vector, &values[idx]);
|
||||
if (sort_push_res.status != VECTOR_OK) {
|
||||
printf("Error while adding elements: %s\n", sort_push_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Added new elements. Before sort: ");
|
||||
for (size_t idx = 0; idx < vector_size(vector); idx++) {
|
||||
vector_result_t sort_get_res = vector_get(vector, idx);
|
||||
if (sort_get_res.status != VECTOR_OK) {
|
||||
printf("Cannot retrieve vec[%zu]: %s\n", idx, sort_get_res.message);
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
const int *val = (int*)sort_get_res.value.element;
|
||||
printf("%d ", *val);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
vector_result_t sort_asc_res = vector_sort(vector, cmp_int_asc);
|
||||
if (sort_asc_res.status != VECTOR_OK) {
|
||||
printf("Cannot sort array: %s\n", sort_asc_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("After sort in ascending order: ");
|
||||
for (size_t idx = 0; idx < vector_size(vector); idx++) {
|
||||
vector_result_t sort_get_res = vector_get(vector, idx);
|
||||
if (sort_get_res.status != VECTOR_OK) {
|
||||
printf("Cannot retrieve vec[%zu]: %s\n", idx, sort_get_res.message);
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
int *val = (int*)sort_get_res.value.element;
|
||||
printf("%d ", *val);
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// Sort vector in descending order
|
||||
vector_result_t sort_desc_res = vector_sort(vector, cmp_int_desc);
|
||||
if (sort_desc_res.status != VECTOR_OK) {
|
||||
printf("Cannot sort array: %s\n", sort_desc_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("After sort in descending order: ");
|
||||
for (size_t idx = 0; idx < vector_size(vector); idx++) {
|
||||
vector_result_t sort_get_res = vector_get(vector, idx);
|
||||
if (sort_get_res.status != VECTOR_OK) {
|
||||
printf("Cannot retrieve vec[%zu]: %s\n", idx, sort_get_res.message);
|
||||
|
||||
return 1;
|
||||
} else {
|
||||
int *val = (int*)sort_get_res.value.element;
|
||||
printf("%d ", *val);
|
||||
}
|
||||
}
|
||||
printf("\n\n");
|
||||
|
||||
// Free vector
|
||||
vector_result_t del_res = vector_destroy(vector);
|
||||
if (del_res.status != VECTOR_OK) {
|
||||
printf("Error while destroying the vector: %s\n", del_res.message);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user