Skip to content
Snippets Groups Projects
Commit a0dc7c37 authored by dario.genga's avatar dario.genga
Browse files

Add array sort by two stacks

The program can now sort an array using the two stacks methods.
parent f6fc779c
Branches
No related tags found
No related merge requests found
...@@ -22,7 +22,6 @@ bool stack_is_empty(stack s) { ...@@ -22,7 +22,6 @@ bool stack_is_empty(stack s) {
return is_empty; return is_empty;
} }
void stack_push(stack *s, int val) { void stack_push(stack *s, int val) {
int next_top = s->top + 1; int next_top = s->top + 1;
...@@ -38,7 +37,6 @@ void stack_pop(stack *s, int *val) { ...@@ -38,7 +37,6 @@ void stack_pop(stack *s, int *val) {
*val = s->data[s->top]; *val = s->data[s->top];
s->data[s->top] = -1; s->data[s->top] = -1;
s->top -= 1; s->top -= 1;
s->size -= 1;
} }
} }
...@@ -54,3 +52,66 @@ void stack_destroy(stack *s) { ...@@ -54,3 +52,66 @@ void stack_destroy(stack *s) {
s->top = -1; s->top = -1;
s->size = -1; s->size = -1;
} }
int *sort(int unsorted_array[], int array_size) {
int *sorted_array = malloc(array_size * sizeof(int));
int *left_peak = malloc(sizeof(int));
int *right_peak = malloc(sizeof(int));
stack left_stack, right_stack;
stack_init(&left_stack, array_size);
stack_init(&right_stack, array_size);
for (int i = 0; i < array_size; i++) {
int value = unsorted_array[i];
// First stack push
if (stack_is_empty(left_stack)) {
stack_push(&left_stack, value);
}
// Add to left stack if the value is lower than the left peek and higher than the right peak
else {
stack_peek(left_stack, left_peak);
if (*left_peak > value && (right_stack.top < 0 || *right_peak < value)) {
stack_push(&left_stack, value);
}
// Else arrange the stacks
else {
while (left_stack.top >= 0 && *left_peak < value) {
stack_pop(&left_stack, left_peak);
stack_push(&right_stack, *left_peak);
stack_peek(left_stack, left_peak);
stack_peek(right_stack, right_peak);
}
while (right_stack.top >= 0 && *right_peak > value) {
stack_pop(&right_stack, right_peak);
stack_push(&left_stack, *right_peak);
stack_peek(right_stack, right_peak);
}
// Then push the value to the left stack
stack_push(&left_stack, value);
}
}
}
// Push to right stack to the left stack
while (right_stack.top >= 0) {
stack_peek(right_stack, right_peak);
stack_pop(&right_stack, right_peak);
stack_push(&left_stack, *right_peak);
}
// Add the stack to the array
for (int i = 0; i < array_size; i++) {
stack_pop(&left_stack, left_peak);
sorted_array[i] = *left_peak;
}
// Free the memory
free(left_peak);
free(right_peak);
stack_destroy(&left_stack);
stack_destroy(&right_stack);
return sorted_array;
}
\ No newline at end of file
...@@ -20,5 +20,6 @@ void stack_push(stack *s, int val); ...@@ -20,5 +20,6 @@ void stack_push(stack *s, int val);
void stack_pop(stack *s, int *val); void stack_pop(stack *s, int *val);
void stack_peek(stack s, int *val); void stack_peek(stack s, int *val);
void stack_destroy(stack *s); void stack_destroy(stack *s);
int *sort(int unsorted_array[], int array_size);
#endif #endif
\ No newline at end of file
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <assert.h> #include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#define ARRAY_SIZE 5
void test_stack_init(stack *s, int size) { void test_stack_init(stack *s, int size) {
printf("Testing stack_init()...\n"); printf("Testing stack_init()...\n");
...@@ -56,6 +57,7 @@ void test_stack_push(stack *s, int val) { ...@@ -56,6 +57,7 @@ void test_stack_push(stack *s, int val) {
stack_push(&stack_full, val); stack_push(&stack_full, val);
assert(stack_full.data[stack_full.top] == value_top_full_stack_value); assert(stack_full.data[stack_full.top] == value_top_full_stack_value);
printf("OK.\n"); printf("OK.\n");
stack_destroy(&stack_full);
} }
void test_stack_pop(stack *s, int *val) { void test_stack_pop(stack *s, int *val) {
...@@ -82,10 +84,25 @@ void test_stack_pop(stack *s, int *val) { ...@@ -82,10 +84,25 @@ void test_stack_pop(stack *s, int *val) {
printf("OK.\n"); printf("OK.\n");
} }
void test_sort(int unsorted_array[], int expected_array[], int array_size) {
printf("Testing sort()...\n");
int* sorted_array = sort(unsorted_array, array_size);
for (int i = 0; i < array_size; i++) {
assert(sorted_array[i] == expected_array[i]);
}
printf("OK.\n");
free(sorted_array);
}
int main() { int main() {
stack s, non_empty_stack; stack s, non_empty_stack;
int size = 10, value_to_add = 7; int size = 10, value_to_add = 7;
int val; int val;
int unsorted_array[ARRAY_SIZE] = { 0, -1, 2, 7, 4 };
int expected_array[ARRAY_SIZE] = { -1, 0, 2, 4, 7 };
printf("Starting the tests...\n"); printf("Starting the tests...\n");
// Test init and destroy // Test init and destroy
test_stack_init(&s, size); test_stack_init(&s, size);
...@@ -103,6 +120,7 @@ int main() { ...@@ -103,6 +120,7 @@ int main() {
test_stack_peek(s, &val); test_stack_peek(s, &val);
test_stack_push(&s, value_to_add); test_stack_push(&s, value_to_add);
test_stack_pop(&s, &val); test_stack_pop(&s, &val);
test_sort(unsorted_array, expected_array, ARRAY_SIZE);
// Free the memory // Free the memory
stack_destroy(&s); stack_destroy(&s);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment