From 6d7011f3a76d014a2c09a23acf49bc150e09f881 Mon Sep 17 00:00:00 2001 From: Divlo Date: Tue, 29 Jun 2021 22:02:52 +0200 Subject: [PATCH] feat(solutions): add `sorting-algorithms/c/merge-sort` --- challenges/sorting-algorithms/README.md | 4 ++ .../solutions/c/merge-sort/README.md | 7 ++ .../solutions/c/merge-sort/merge_sort.h | 10 +++ .../solutions/c/merge-sort/solution.c | 69 +++++++++++++++++++ 4 files changed, 90 insertions(+) create mode 100644 challenges/sorting-algorithms/solutions/c/merge-sort/README.md create mode 100644 challenges/sorting-algorithms/solutions/c/merge-sort/merge_sort.h create mode 100644 challenges/sorting-algorithms/solutions/c/merge-sort/solution.c diff --git a/challenges/sorting-algorithms/README.md b/challenges/sorting-algorithms/README.md index b54f473..957ff6c 100644 --- a/challenges/sorting-algorithms/README.md +++ b/challenges/sorting-algorithms/README.md @@ -9,6 +9,10 @@ We will use the [numerical order](https://en.wikipedia.org/wiki/Numerical_order) Write a function that takes a list of integers and sort them in ascending order. +### Constraints + +- List of integers length <= 25 000 + ## Source - [Wikipedia - Sorting algorithm](https://en.wikipedia.org/wiki/Sorting_algorithm) diff --git a/challenges/sorting-algorithms/solutions/c/merge-sort/README.md b/challenges/sorting-algorithms/solutions/c/merge-sort/README.md new file mode 100644 index 0000000..29bfc08 --- /dev/null +++ b/challenges/sorting-algorithms/solutions/c/merge-sort/README.md @@ -0,0 +1,7 @@ +# sorting-algorithms/c/merge-sort + +Created by [@Divlo](https://github.com/Divlo) on 29 June 2021. + +| Algorithm | Best Case | Average Case | Worst Case | +| ----------------------------------------------------------- | ----------- | ------------ | ----------- | +| [Merge sort](https://wikipedia.org/wiki/Merge_sort) | O(n log(n)) | O(n log(n)) | O(n log(n)) | diff --git a/challenges/sorting-algorithms/solutions/c/merge-sort/merge_sort.h b/challenges/sorting-algorithms/solutions/c/merge-sort/merge_sort.h new file mode 100644 index 0000000..bf97d11 --- /dev/null +++ b/challenges/sorting-algorithms/solutions/c/merge-sort/merge_sort.h @@ -0,0 +1,10 @@ +#ifndef MERGE_SORT_H +#define MERGE_SORT_H + +void merge(int numbers[], int left_index, int middle_index, int right_index); + +void merge_sort_recursive(int numbers[], int left_index, int right_index); + +void merge_sort(int *numbers, const int length); + +#endif diff --git a/challenges/sorting-algorithms/solutions/c/merge-sort/solution.c b/challenges/sorting-algorithms/solutions/c/merge-sort/solution.c new file mode 100644 index 0000000..e74d0da --- /dev/null +++ b/challenges/sorting-algorithms/solutions/c/merge-sort/solution.c @@ -0,0 +1,69 @@ +#include +#include + +#include "merge_sort.h" + +int main() { + int *numbers = malloc(25000 * sizeof(int)); + int current_number; + int index_input = 0; + while (scanf("%d", ¤t_number) != EOF) { + numbers[index_input] = current_number; + index_input += 1; + } + merge_sort(numbers, index_input); + for (int index = 0; index < index_input; index++) { + printf("%d\n", numbers[index]); + } + free(numbers); + return 0; +} + +void merge(int numbers[], int left_index, int middle_index, int right_index) { + int index_left, index_right, index_numbers; + int index_temporary_1 = middle_index - left_index + 1; + int index_temporary_2 = right_index - middle_index; + int left[index_temporary_1], right[index_temporary_2]; + for (index_left = 0; index_left < index_temporary_1; index_left++) { + left[index_left] = numbers[left_index + index_left]; + } + for (index_right = 0; index_right < index_temporary_2; index_right++) { + right[index_right] = numbers[middle_index + 1 + index_right]; + } + index_left = 0; + index_right = 0; + index_numbers = left_index; + while (index_left < index_temporary_1 && index_right < index_temporary_2) { + if (left[index_left] <= right[index_right]) { + numbers[index_numbers] = left[index_left]; + index_left++; + } else { + numbers[index_numbers] = right[index_right]; + index_right++; + } + index_numbers++; + } + while (index_left < index_temporary_1) { + numbers[index_numbers] = left[index_left]; + index_left++; + index_numbers++; + } + while (index_right < index_temporary_2) { + numbers[index_numbers] = right[index_right]; + index_right++; + index_numbers++; + } +} + +void merge_sort_recursive(int numbers[], int left_index, int right_index) { + if (left_index < right_index) { + int middle_index = left_index + (right_index - left_index) / 2; + merge_sort_recursive(numbers, left_index, middle_index); + merge_sort_recursive(numbers, middle_index + 1, right_index); + merge(numbers, left_index, middle_index, right_index); + } +} + +void merge_sort(int numbers[], const int length) { + merge_sort_recursive(numbers, 0, length - 1); +}