Skip to content

Commit 520e9c3

Browse files
Merge pull request #22 from Ramy-Badr-Ahmed/tests_examples/sorts/recursive_bubble_sort
Modified `modules/sorts/recursive_bubble_sort.f90`
2 parents a361a1a + 405fb37 commit 520e9c3

File tree

2 files changed

+35
-30
lines changed

2 files changed

+35
-30
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
!> Example Program for Recursive Bubble Sort
2+
!!
3+
!! This program demonstrates the use of the `recursive_bubble_sort_module` to sort an array using the recursive bubble sort algorithm.
4+
5+
program recursive_bubble_sort_example
6+
use recursive_bubble_sort_module
7+
implicit none
8+
9+
real :: array(5)
10+
11+
!! Fill the array with random numbers.
12+
call random_number(array)
13+
14+
print*, "Before:", array
15+
16+
!! Bubble sort subroutine call.
17+
call recursive_bubble_sort(array, size(array))
18+
19+
print*, "After:", array
20+
21+
end program recursive_bubble_sort_example
Lines changed: 14 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,35 @@
1-
!> Recursive Bubble Sort
1+
!> Recursive Bubble Sort Module
22
!!
3-
!! This is a simple example for recursive bubble sort algorithm in Fortran.
4-
5-
program recursive_bubble_sort_program
6-
implicit none
7-
8-
real :: array(5)
9-
10-
!! random_number subroutine fills the argument with random numbers.
11-
call random_number(array)
12-
13-
print*, "Before:", array
14-
15-
!! Bubble sort subroutine call.
16-
call recursive_bubble_sort(array, size(array))
17-
18-
print*, "After:", array
3+
!! This module contains a subroutine for sorting a collection using the recursive bubble sort algorithm.
194

5+
module recursive_bubble_sort_module
6+
implicit none
207
contains
218

229
!! This subroutine sorts the collection recursively using bubble sort.
2310
!! Recursive keyword at the start declares that the function is recursive.
24-
recursive subroutine recursive_bubble_sort(collection, collection_size)
25-
real, dimension(:), intent(inout) :: collection !! A collection for elements of type real
11+
recursive subroutine recursive_bubble_sort(collection, collection_size)
12+
real, dimension(:), intent(inout) :: collection !! A collection for elements of type real.
2613
integer, intent(in) :: collection_size !! Collection's size.
2714

2815
integer :: i
2916
real :: temp
3017

31-
do i = 1, collection_size
32-
if (collection(i) .gt. collection(i+1)) then
33-
!! If collection is out of order in [i, i+1] region,
34-
!! swap these values using a temp variable as follows.
35-
18+
!! Perform bubble sort on the collection.
19+
do i = 1, collection_size - 1
20+
if (collection(i) .gt. collection(i + 1)) then
21+
!! Swap values if they are out of order in [i, i + 1] region.
3622
temp = collection(i)
37-
collection(i) = collection(i+1)
38-
collection(i+1) = temp
23+
collection(i) = collection(i + 1)
24+
collection(i + 1) = temp
3925
end if
4026
end do
4127

28+
!! Recursively call the subroutine with (collection_size - 1) if size > 2.
4229
if (collection_size .gt. 2) then
43-
!! If collection's size is greater then 2,
44-
!! this subroutine recursively calls itself with (collection_size - 1) for size argument.
4530
call recursive_bubble_sort(collection, collection_size - 1)
4631
end if
4732

4833
end subroutine recursive_bubble_sort
4934

50-
end program recursive_bubble_sort_program
51-
35+
end module recursive_bubble_sort_module

0 commit comments

Comments
 (0)