From c7232dc22bc1518413fa346d4be84f3549ed9879 Mon Sep 17 00:00:00 2001 From: baz Date: Mon, 29 Jan 2024 16:42:31 +0000 Subject: [PATCH] Implement Bubble Sort --- bubblesort/main.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 2 +- 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 bubblesort/main.c diff --git a/bubblesort/main.c b/bubblesort/main.c new file mode 100644 index 0000000..378c05a --- /dev/null +++ b/bubblesort/main.c @@ -0,0 +1,49 @@ +#include +#include + +#define ARRAYLENGTH 100 + +void generateRandomArray(int *array) +{ + for (size_t i = 0; i < ARRAYLENGTH; i++) + { + array[i] = rand(); + } +} + +void printArrayContent(int *array) +{ + for (size_t i = 0; i < ARRAYLENGTH; i++) + { + printf("%d ", array[i]); + } + + printf("\n"); +} + +void sortArray(int *array) +{ + for (size_t i = 0; i < ARRAYLENGTH - 1; i++ ) + { + for (size_t j = 0; j < ARRAYLENGTH - 1 - i; j++) + { + if (array[j] > array[j+1]) + { + int temp = array[j+1]; + array[j+1] = array[j]; + array[j] = temp; + } + } + } +} + +int main(int argc, char *argv[]) +{ + int array[ARRAYLENGTH]; + + generateRandomArray(array); + sortArray(array); + printArrayContent(array); + + return 1; +} \ No newline at end of file diff --git a/readme.md b/readme.md index adc2807..fbd6134 100644 --- a/readme.md +++ b/readme.md @@ -11,4 +11,4 @@ For my own personal revision, all implementations were initially attemped using ### Algorithms -- TBA +- [Bubble Sort](bubblesort/main.c)