diff --git a/stack.c b/stack.c index f92a89b3f51572ccdc75de875bee8a73d66b9500..5ae98bf16ee7162d1bb3df3e99dbb1159d40bfd6 100644 --- a/stack.c +++ b/stack.c @@ -22,7 +22,6 @@ bool stack_is_empty(stack s) { return is_empty; } - void stack_push(stack *s, int val) { int next_top = s->top + 1; @@ -38,7 +37,6 @@ void stack_pop(stack *s, int *val) { *val = s->data[s->top]; s->data[s->top] = -1; s->top -= 1; - s->size -= 1; } } @@ -54,3 +52,66 @@ void stack_destroy(stack *s) { s->top = -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 diff --git a/stack.h b/stack.h index d93292841b46cca2fae7ddea7b2160435f064e4b..16a072d2eeb42de5840a0a6b5dc9dc4a08383045 100644 --- a/stack.h +++ b/stack.h @@ -20,5 +20,6 @@ void stack_push(stack *s, int val); void stack_pop(stack *s, int *val); void stack_peek(stack s, int *val); void stack_destroy(stack *s); +int *sort(int unsorted_array[], int array_size); #endif \ No newline at end of file diff --git a/test.c b/test.c index 5faf01a13a4e8032cdb51b8dfec57473867da730..3ac1960cbce150f9bae3160a35ff6dc9a328a100 100644 --- a/test.c +++ b/test.c @@ -6,6 +6,7 @@ #include <assert.h> #include <stdbool.h> #include <stdio.h> +#define ARRAY_SIZE 5 void test_stack_init(stack *s, int size) { printf("Testing stack_init()...\n"); @@ -56,6 +57,7 @@ void test_stack_push(stack *s, int val) { stack_push(&stack_full, val); assert(stack_full.data[stack_full.top] == value_top_full_stack_value); printf("OK.\n"); + stack_destroy(&stack_full); } void test_stack_pop(stack *s, int *val) { @@ -82,10 +84,25 @@ void test_stack_pop(stack *s, int *val) { 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() { stack s, non_empty_stack; int size = 10, value_to_add = 7; 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"); // Test init and destroy test_stack_init(&s, size); @@ -103,6 +120,7 @@ int main() { test_stack_peek(s, &val); test_stack_push(&s, value_to_add); test_stack_pop(&s, &val); + test_sort(unsorted_array, expected_array, ARRAY_SIZE); // Free the memory stack_destroy(&s);