From 6796489725e2e72a2886beaa48feae0cae2e2909 Mon Sep 17 00:00:00 2001 From: Eric Patrizio Date: Tue, 25 Jan 2022 15:15:57 +0100 Subject: [PATCH] Add heap sort priority queue example --- .gitignore | 2 ++ Makefile | 6 +++++- README.md | 7 +++++-- examples/sort/main.c | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 examples/sort/main.c diff --git a/.gitignore b/.gitignore index 08e8dc9..8aa7837 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,8 @@ cds cds_test intl !intl/ +sort +!sort/ /tests/lib/ # vsc diff --git a/Makefile b/Makefile index d39e2ce..fd005b0 100644 --- a/Makefile +++ b/Makefile @@ -11,4 +11,8 @@ compile_test: compile_example_intl: gcc -g -o intl -Wall -Wextra \ - src/vector.c src/hash_utils.c src/hash_map.c examples/intl/intl.c examples/intl/main.c \ No newline at end of file + src/vector.c src/hash_utils.c src/hash_map.c examples/intl/intl.c examples/intl/main.c + +compile_example_sort: + gcc -g -o sort -Wall -Wextra \ + src/vector.c src/priority_queue.c examples/sort/main.c \ No newline at end of file diff --git a/README.md b/README.md index 8c8988b..4af6435 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,10 @@ Implementations are inspired by this [blog post](https://www.journaldev.com/3523 Execute "make compile_test" and that's all!\ I use the framework [ctestfmk](https://github.com/epatrizio/ctestfmk). Read the documentation for more informations. -## Example +## Examples -In /example directory, you can find a complete application of HashMap data structure:\ +* In /examples/intl directory, there is a complete application of HashMap data structure:\ A little internationalization component. + +* In /examples/sort directory, there is a classic and easy application of priority queue data structure:\ +The array heap sort. For simplicity, only the int type has been processed. diff --git a/examples/sort/main.c b/examples/sort/main.c new file mode 100644 index 0000000..ddb9e20 --- /dev/null +++ b/examples/sort/main.c @@ -0,0 +1,39 @@ +#include +#include +#include +#include + +#include "../../src/vector.h" +#include "../../src/priority_queue.h" + +/* +This implementation of heapsort is very easy and work well. +But it requires 2 arrays : the initial array to sort and the temp priority queue array. +It's possible to have a more efficient (in memory) implementation with only the initial array : + https://en.wikipedia.org/wiki/Heapsort +*/ +void heap_sort(int a[], size_t size) +{ + int_priority_queue pq = int_priority_queue_create(); + + for (unsigned int i = 0 ; i < size ; i++) + int_priority_queue_push(pq, a[i]); + + for (unsigned int i = 0 ; i < size ; i++) + a[i] = int_priority_queue_pop(pq); + + int_priority_queue_destroy(pq); +} + +int main() +{ + int a[] = { 0, 25, 32, 10, 1, 50, 20, 100, -1, 27, 28, 10, 3, 99, -10, -1, 110, 55, 75, -2, 0 }; + size_t size = sizeof(a) / sizeof(int); + + heap_sort(a,size); + + for (unsigned int i = 0 ; i < size ; i++) + printf("%i:%i\n", i, a[i]); + + return EXIT_SUCCESS; +}