Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
*.exe
*.out
*.app

.idea/

2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 22 additions & 0 deletions sorts/example_usage_merge_sort.f90
Original file line number Diff line number Diff line change
@@ -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
97 changes: 97 additions & 0 deletions sorts/merge_sort.f90
Original file line number Diff line number Diff line change
@@ -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