-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
gnome_sort.f90
62 lines (53 loc) · 1.56 KB
/
gnome_sort.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
!> Gnome Sort Algorithm
!!
!! This module implements the Gnome Sort algorithm.
!!
!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed)
!! in Pull Request: #09
!! https://github.com/TheAlgorithms/Fortran/pull/9
!!
!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request
!! addressing bugs/corrections to this file. Thank you!
!!
!! Gnome Sort is a simple comparison-based sorting algorithm.
!! It iterates through the array, comparing and swapping elements if needed.
!!
!! Time Complexity: O(n^2) where n is the number of elements in the input array.
!!
!! Input:
!! - An array of integers.
!!
!! Output:
!! - A sorted array of integers.
!!
module gnome_sort_module
implicit none
contains
!> Subroutine to sort an array using Gnome Sort
subroutine gnome_sort(array)
implicit none
integer, dimension(:), intent(inout) :: array ! Input/output array to be sorted
integer :: i, n
n = size(array)
i = 1
! Gnome Sort algorithm
do while (i <= n)
if (i == 1 .or. array(i) >= array(i - 1)) then
i = i + 1
else
! Swap elements
call swap(array(i), array(i - 1))
i = i - 1
end if
end do
end subroutine gnome_sort
!> Helper subroutine to swap two elements in an array
subroutine swap(x, y)
implicit none
integer, intent(inout) :: x, y
integer :: temp
temp = x
x = y
y = temp
end subroutine swap
end module gnome_sort_module