From 27aaeabc9f8827d63c97319da1b0ed10e1e3efef Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Mon, 9 Sep 2024 19:01:26 +0200 Subject: [PATCH 1/2] Implemented Merge Sort Algorithm --- .gitignore | 3 + sorts/example_usage_merge_sort.f90 | 22 +++++++ sorts/merge_sort.f90 | 97 ++++++++++++++++++++++++++++++ 3 files changed, 122 insertions(+) create mode 100644 sorts/example_usage_merge_sort.f90 create mode 100644 sorts/merge_sort.f90 diff --git a/.gitignore b/.gitignore index 259148f..974748c 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ *.exe *.out *.app + +.idea/ + diff --git a/sorts/example_usage_merge_sort.f90 b/sorts/example_usage_merge_sort.f90 new file mode 100644 index 0000000..4991e8a --- /dev/null +++ b/sorts/example_usage_merge_sort.f90 @@ -0,0 +1,22 @@ +!> Test program for the Merge Sort algorithm + +!! This program demonstrates the use of the merge_sort_module by sorting an array of integers. + +program test_merge_sort + use merge_sort_module + implicit none + integer, dimension(8) :: array ! Test array + integer :: n, i + + ! Initialize the test array + array = (/ -2, 3, -10, 11, 99, 100000, 100, -200 /) + n = size(array) + + ! Call merge_sort from the module to sort the array + call merge_sort(array, n) + + print *, "Sorted array:" + do i = 1, n + print *, array(i) + end do +end program test_merge_sort diff --git a/sorts/merge_sort.f90 b/sorts/merge_sort.f90 new file mode 100644 index 0000000..43d3c6b --- /dev/null +++ b/sorts/merge_sort.f90 @@ -0,0 +1,97 @@ +!> Merge Sort Algorithm + +!> This module implements the Merge Sort algorithm. +!! +!! Merge Sort is a divide-and-conquer algorithm. It divides the input array into two halves, recursively sorts them, +!! and then merges the two sorted halves. +!! +!! Time Complexity: O(n log n) where n is the number of elements in the input array. +!! +!! Input: +!! - An array of integers. +!! +!! Output: +!! - A sorted array of integers. +!! +module merge_sort_module +implicit none + +contains + + !> Recursive subroutine to sort an array using merge sort + recursive subroutine merge_sort(array, n) + implicit none + integer, dimension(:), intent(inout) :: array ! Input/output array to be sorted + integer, intent(in) :: n ! Size of the array + integer :: middle, i + integer, dimension(:), allocatable :: left_half, right_half, sorted_array + + ! Base case: return if the array has 1 or fewer elements + if (n <= 1) return + + ! Calculate the middle point to split the array + middle = n / 2 + + ! Allocate space for the two halves + allocate(left_half(middle), right_half(n - middle), sorted_array(n)) + + ! Split array into two halves + left_half = array(1:middle) + right_half = array(middle+1:n) + + ! Recursively sort each half + call merge_sort(left_half, middle) + call merge_sort(right_half, n - middle) + + ! Merge the sorted halves + call merge(left_half, middle, right_half, n - middle, sorted_array) + + ! Copy the sorted array back + array = sorted_array + + ! Deallocate the temporary arrays + deallocate(left_half, right_half, sorted_array) + + end subroutine merge_sort + + !> Subroutine to merge two sorted halves of an array + subroutine merge(left_half, n_left, right_half, n_right, sorted_array) + implicit none + integer, dimension(:), intent(in) :: left_half, right_half ! Input sorted halves + integer, dimension(:), intent(out) :: sorted_array ! Output sorted array + integer, intent(in) :: n_left, n_right ! Sizes of the input halves + integer :: i, j, k ! Loop counters + + i = 1 + j = 1 + k = 1 + + ! Merge the two halves + do while (i <= n_left .and. j <= n_right) + if (left_half(i) < right_half(j)) then + sorted_array(k) = left_half(i) + i = i + 1 + else + sorted_array(k) = right_half(j) + j = j + 1 + end if + k = k + 1 + end do + + ! Copy remaining elements of left_half, if any + do while (i <= n_left) + sorted_array(k) = left_half(i) + i = i + 1 + k = k + 1 + end do + + ! Copy remaining elements of right_half, if any + do while (j <= n_right) + sorted_array(k) = right_half(j) + j = j + 1 + k = k + 1 + end do + + end subroutine merge + +end module merge_sort_module From e02553db3c862e24240dd5efbf6a03d204ffb89a Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Mon, 9 Sep 2024 19:26:11 +0200 Subject: [PATCH 2/2] Added the merge sort to DIRECTORY.md --- DIRECTORY.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 4f2236b..5cf3c38 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -12,3 +12,5 @@ ## Sorts * [bubble_sort](/sorts/bubble_sort.f90) * [recursive_bubble_sort](/sorts/recursive_bubble_sort.f90) +* [merge_sort](/sorts/merge_sort.f90) +* [example_usage_merge_sort](/sorts/example_usage_merge_sort.f90)