Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
65f3b3d
Implemented Heap Sort Algorithm
Ramy-Badr-Ahmed Sep 10, 2024
8643287
Implemented Gnome Sort Algorithm
Ramy-Badr-Ahmed Sep 10, 2024
7653cc9
Implemented Quick Sort Algorithm
Ramy-Badr-Ahmed Sep 10, 2024
786456a
Implemented Radix Sort Algorithm (with different bases)
Ramy-Badr-Ahmed Sep 10, 2024
22b6e66
Merge branch 'TheAlgorithms:main' into feature/heap_sort_implementation
Ramy-Badr-Ahmed Sep 12, 2024
76c96a8
Merge branch 'TheAlgorithms:main' into feature/quick_sort_implementation
Ramy-Badr-Ahmed Sep 12, 2024
adc8a4e
Merge branch 'TheAlgorithms:main' into feature/gnome_sort_implementation
Ramy-Badr-Ahmed Sep 12, 2024
6a874df
Merge branch 'TheAlgorithms:main' into feature/radix_sort_implementation
Ramy-Badr-Ahmed Sep 12, 2024
5828f83
Added more test cases for heap_sort.f90 module.
Ramy-Badr-Ahmed Sep 12, 2024
c43fd2f
Added more test cases for gnome_sort.f90 module.
Ramy-Badr-Ahmed Sep 12, 2024
9bd8ef6
Merge branch 'feature/heap_sort_implementation' into feature/gnome_so…
Ramy-Badr-Ahmed Sep 12, 2024
e124185
Corrected DIRECTORY.md
Ramy-Badr-Ahmed Sep 12, 2024
afbdedf
Merge branch 'refs/heads/feature/gnome_sort_implementation' into feat…
Ramy-Badr-Ahmed Sep 12, 2024
d24917c
Added more test cases for quick_sort.f90. Corrected DIRECTORY.md.
Ramy-Badr-Ahmed Sep 12, 2024
de9fb49
Removed duplicate comments
Ramy-Badr-Ahmed Sep 12, 2024
4a02455
Minor change to test numbers
Ramy-Badr-Ahmed Sep 12, 2024
192fe17
Merge branch 'feature/heap_sort_implementation' into feature/gnome_so…
Ramy-Badr-Ahmed Sep 12, 2024
173bc30
Minor change to test numbers
Ramy-Badr-Ahmed Sep 12, 2024
2c077d7
Merge branch 'feature/gnome_sort_implementation' into feature/quick_s…
Ramy-Badr-Ahmed Sep 12, 2024
fd05015
Merge branch 'feature/heap_sort_implementation' into feature/radix_so…
Ramy-Badr-Ahmed Sep 12, 2024
e35c887
Merge branch 'feature/gnome_sort_implementation' into feature/radix_s…
Ramy-Badr-Ahmed Sep 12, 2024
f4e4c0f
Merge branch 'feature/quick_sort_implementation' into feature/radix_s…
Ramy-Badr-Ahmed Sep 12, 2024
ce93203
Added more test cases for radix_sort.f90. Corrected DIRECTORY.md.
Ramy-Badr-Ahmed Sep 12, 2024
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
12 changes: 12 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@
* [recursive_bubble_sort](/sorts/recursive_bubble_sort.f90)
* [merge_sort](/sorts/merge_sort.f90)
* [example_usage_merge_sort](/sorts/example_usage_merge_sort.f90)
* [heap_sort](/sorts/heap_sort.f90)
* [example_usage_heap_sort](/sorts/example_usage_heap_sort.f90)
* [tests_heap_sort](/sorts/tests_heap_sort.f90)
* [gnome_sort](/sorts/gnome_sort.f90)
* [example_usage_gnome_sort](/sorts/example_usage_gnome_sort.f90)
* [tests_gnome_sort](/sorts/tests_gnome_sort.f90)
* [quick_sort](/sorts/quick_sort.f90)
* [example_usage_quick_sort](/sorts/example_usage_quick_sort.f90)
* [tests_quick_sort](/sorts/tests_quick_sort.f90)
* [radix_sort](/sorts/gnome_sort.f90)
* [example_usage_radix_sort](/sorts/example_usage_radix_sort.f90)
* [tests_radix_sort](/sorts/tests_radix_sort.f90)
22 changes: 22 additions & 0 deletions sorts/example_usage_gnome_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
!> Test program for the Gnome Sort algorithm

!! This program demonstrates the use of the gnome_sort_module by sorting an array of integers.

program test_gnome_sort
use gnome_sort_module
implicit none
integer, dimension(10) :: array ! Test array
integer :: n, i

! Initialize the test array
array = (/ -5, 2, 9, 1, 5, 6, -7, 8, 15, -20 /)
n = size(array)

! Call gnome_sort from the module to sort the array
call gnome_sort(array)

print *, "Sorted array:"
do i = 1, n
print *, array(i)
end do
end program test_gnome_sort
31 changes: 31 additions & 0 deletions sorts/example_usage_heap_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
!> Example program for the Heap Sort algorithm
!!
!! This program demonstrates the use of the heap_sort_module by sorting an array of integers.

program test_heap_sort
use heap_sort_module
implicit none
integer, parameter :: n = 12
integer :: i
integer, dimension(n) :: array(n) ! Test array

! Initialize the test array
array = (/ 12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1 /)
n = size(array)

! Print the original array
print *, "Original array:"
do i = 1, n
print *, array(i)
end do

! Call heap_sort from the module to sort the array
call heap_sort(array, n)

! Print the sorted array
print *, "Sorted array:"
do i = 1, n
print *, array(i)
end do

end program test_heap_sort
30 changes: 30 additions & 0 deletions sorts/example_usage_quick_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
!> Example program for the Quick Sort algorithm
!! This program demonstrates the use of the quick_sort_module by sorting an array of integers.

program test_quick_sort
use quick_sort_module
implicit none

integer, dimension(10) :: array ! Test array
integer :: n, i

! Initialize the test array
array = (/ 10, 7, 8, 9, 1, 5, -2, 12, 0, -5 /)
n = size(array)

! Print the original array
print *, "Original array:"
do i = 1, n
print *, array(i)
end do

! Call quick_sort from the module to sort the array
call quick_sort(array, 1, n) ! (1: low bound , n: high bound) of the array

! Print the sorted array
print *, "Sorted array:"
do i = 1, n
print *, array(i)
end do

end program test_quick_sort
45 changes: 45 additions & 0 deletions sorts/example_usage_radix_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
!> Test program for the Radix Sort algorithm

!! This program demonstrates the use of the radix_sort_module by sorting an array of integers.
!! The base parameter affects the internal digit processing but does not change the final sorted order
!! of decimal integers. The output is always in decimal form.

program test_radix_sort
use radix_sort_module
implicit none
integer, dimension(10) :: array
integer :: n, i
integer, parameter :: base10 = 10, base2 = 2, base16 = 16

! Test for base 10
print *, "Testing Radix Sort with base 10:"
array = (/ 170, 45, 75, 90, 802, 24, 2, 66, 15, 40 /)
n = size(array)
call radix_sort(array, n, base10)
print *, "Sorted array in base 10:"
do i = 1, n
print *, array(i)
end do

! Test for base 2
print *, "Testing Radix Sort with base 2:"
array = (/ 1010, 1101, 1001, 1110, 0010, 0101, 1111, 0110, 1000, 0001 /) ! Binary values whose decimal: (/ 10, 13, 9, 14, 2, 5, 15, 6, 8, 1 /)
n = size(array)
call radix_sort(array, n, base2)
print *, "Sorted binary array in Decimal:"
do i = 1, n
print *, array(i)
end do

! Test for base 16
print *, "Testing Radix Sort with base 16:"
array = (/ 171, 31, 61, 255, 16, 5, 211, 42, 180, 0 /) ! Hexadecimal values as decimal
n = size(array)
call radix_sort(array, n, base16)
print *, "Sorted hexadecimal array in Decimal:"
do i = 1, n
print *, array(i)
end do

end program test_radix_sort

54 changes: 54 additions & 0 deletions sorts/gnome_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
!> Gnome Sort Algorithm

!> This module implements the Gnome Sort algorithm.
!!
!! 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
87 changes: 87 additions & 0 deletions sorts/heap_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
!> ## Heap Sort Algorithm
!>
!! This module implements the Heap Sort algorithm.
!!
!! Heap Sort is a comparison-based sorting algorithm that uses a binary heap data structure.
!! It first builds a max heap from the input data and then repeatedly extracts the maximum
!! element from the heap and reconstructs the heap until the array is sorted.
!!
!! 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 heap_sort_module
implicit none

contains

!> Subroutine to perform heap sort on an array
subroutine heap_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 :: i

! Build the max heap
do i = n / 2, 1, -1
call heapify(array, n, i)
end do

! Extract elements one by one from the heap
do i = n, 2, -1
! Move the current root to the end
call swap(array, 1, i)

! Call max heapify on the reduced heap
call heapify(array, i - 1, 1)
end do

end subroutine heap_sort

!> Subroutine to maintain the heap property
recursive subroutine heapify(array, n, i)
implicit none
integer, dimension(:), intent(inout) :: array ! Input/output array to be heapified
integer, intent(in) :: n ! Size of the heap
integer, intent(in) :: i ! Index of the root
integer :: largest, left, right

largest = i
left = 2 * i
right = 2 * i + 1

! Is Left Child is larger than Root?
if (left <= n .and. array(left) > array(largest)) then
largest = left
end if

! Is Right Child larger than Largest so far?
if (right <= n .and. array(right) > array(largest)) then
largest = right
end if

! Swap and heapify if Root is not the Largest
if (largest /= i) then
call swap(array, i, largest)
call heapify(array, n, largest)
end if

end subroutine heapify

!> Subroutine helper to swap two elements in an array
subroutine swap(array, i, j)
implicit none
integer, dimension(:), intent(inout) :: array ! Input/output array in which elements are swapped
integer, intent(in) :: i, j ! Indices of the elements to be swapped
integer :: temp

temp = array(i)
array(i) = array(j)
array(j) = temp

end subroutine swap

end module heap_sort_module
75 changes: 75 additions & 0 deletions sorts/quick_sort.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
!> Quick Sort Algorithm
!! This module implements the Quick Sort algorithm, a highly efficient
!! sorting technique that uses the divide-and-conquer strategy.
!!
!! Quick Sort works by selecting a pivot element and partitioning the
!! array into elements less than the pivot and elements greater than the pivot.
!!
!! Time Complexity: O(n log n) on average, though it can degrade to O(n^2)
!! in the worst case (depending on the pivot choice).
!!
!! Input:
!! - An array of integers.
!!
!! Output:
!! - A sorted array of integers.
!!
module quick_sort_module
implicit none

contains

!> Subroutine to sort an array using Quick Sort
recursive subroutine quick_sort(array, low, high)
implicit none
integer, dimension(:), intent(inout) :: array ! Input/output array to be sorted
integer, intent(in) :: low, high ! Indices of the array

integer :: pivot_index

if (low < high) then
! Partition the array and get the pivot index
pivot_index = partition(array, low, high)

! Recursively sort elements before and after partition
call quick_sort(array, low, pivot_index - 1)
call quick_sort(array, pivot_index + 1, high)
end if

end subroutine quick_sort

!> Subroutine to partition the array based on the pivot element
function partition(array, low, high) result(pivot_index)
implicit none
integer, dimension(:), intent(inout) :: array ! Input/output array to be partitioned
integer, intent(in) :: low, high ! Indices of the array
integer :: pivot, pivot_index, i, j

pivot = array(high)
i = low - 1

do j = low, high - 1
if (array(j) <= pivot) then
i = i + 1
call swap(array, i, j)
end if
end do

call swap(array, i + 1, high)
pivot_index = i + 1

end function partition

!> Helper subroutine to swap two elements in the array
subroutine swap(array, i, j)
implicit none
integer, dimension(:), intent(inout) :: array
integer, intent(in) :: i, j
integer :: temp

temp = array(i)
array(i) = array(j)
array(j) = temp
end subroutine swap

end module quick_sort_module
Loading