diff --git a/DIRECTORY.md b/DIRECTORY.md index 37d0435..0fb17e6 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -6,6 +6,7 @@ ## Searches * [linear_search](/modules/searches/linear_search.f90) * [recursive_linear_search](/modules/searches/recursive_linear_search.f90) +* [ternary_search](/modules/searches/ternary_search_module.f90) ## Sorts * [bubble_sort](/modules/sorts/bubble_sort.f90) * [recursive_bubble_sort](/modules/sorts/recursive_bubble_sort.f90) diff --git a/examples/maths/factorial.f90 b/examples/maths/factorial.f90 index 7d16eb7..af12be6 100644 --- a/examples/maths/factorial.f90 +++ b/examples/maths/factorial.f90 @@ -7,7 +7,7 @@ program factorial_program use factorial_module implicit none - Print*, factorial(5) - Print*, recursive_factorial(5) + Print *, factorial(5) + Print *, recursive_factorial(5) end program factorial_program diff --git a/examples/searches/example_linear_search.f90 b/examples/searches/example_linear_search.f90 index 6e3a24d..2d7425c 100644 --- a/examples/searches/example_linear_search.f90 +++ b/examples/searches/example_linear_search.f90 @@ -8,12 +8,12 @@ program linear_search_program integer, dimension(5) :: array - array = (/ 540, 6, 10, 100, 3 /) + array = (/540, 6, 10, 100, 3/) !! Search for the number 6 in array. - print*, "Target = 6: ", linear_search(array, 6) !! Prints 2. + print *, "Target = 6: ", linear_search(array, 6) !! Prints 2. !! Search for the number 5 in array. - print*, "Target = 5: ", linear_search(array, 5) !! Prints -1 because item 5 is not found. + print *, "Target = 5: ", linear_search(array, 5) !! Prints -1 because item 5 is not found. end program linear_search_program diff --git a/examples/searches/example_ternary_search_array_based.f90 b/examples/searches/example_ternary_search_array_based.f90 new file mode 100644 index 0000000..e53ee3b --- /dev/null +++ b/examples/searches/example_ternary_search_array_based.f90 @@ -0,0 +1,20 @@ +! Example Program: Array-based Ternary Search +! This program demonstrates how to use the array-based ternary search algorithm +! implemented in the `ternary_search` module to find a target element in a sorted array. + +program example_ternary_search_array + use ternary_search + implicit none + integer :: result ! Holds the index of the found target + integer, dimension(10) :: arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] ! Sorted Test Array + integer :: target ! Target value to search for + + target = 17 + result = ternary_search_array(arr, target, 1, size(arr)) + + if (result /= -1) then + print *, "Target found at index:", result + else + print *, "Target not found." + end if +end program example_ternary_search_array diff --git a/examples/searches/example_ternary_search_function_based.f90 b/examples/searches/example_ternary_search_function_based.f90 new file mode 100644 index 0000000..bfa60a2 --- /dev/null +++ b/examples/searches/example_ternary_search_function_based.f90 @@ -0,0 +1,74 @@ +!> Example Program: Function-based Ternary Search for Minimum and Maximum +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #24 +!! https://github.com/TheAlgorithms/Fortran/pull/24 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! This program demonstrates how to use the function-based ternary search algorithm +!! from the `ternary_search` module to find the minimum and maximum of unimodal functions. + +program ternary_search_function_based + use ternary_search + implicit none + + ! Define the variables + integer, parameter :: dp = kind(0.0d0) ! Define double precision kind + real(8) :: result_min, result_max ! Results for minimum and maximum values + real(8) :: min_point, max_point ! Points where minimum and maximum occur + real(8) :: left, right, tol ! Left and right bounds, and tolerance + + interface + ! Function with a minimum (example function - defined externally) + real(8) function f_min(x) + real(8), intent(in) :: x + end function f_min + + ! Function with a maximum (example function - defined externally) + real(8) function f_max(x) + real(8), intent(in) :: x + end function f_max + end interface + + ! The boundary values can vary depending on the problem context. + ! In this example, they are chosen arbitrarily. + left = 0.0d0 + right = 10.0d0 + + ! The tolerance value defines how close the left and right bounds must be for the search to terminate. + tol = 1.0e-6_dp + + ! Call the ternary search to find the minimum point of f_min + min_point = ternary_search_minimum(f_min, left, right, tol) + result_min = f_min(min_point) + + ! Call the ternary search to find the maximum point of f_max + max_point = ternary_search_maximum(f_max, left, right, tol) + result_max = f_max(max_point) + + print *, "Minimum of the function f_min is at x =", min_point, "with value =", result_min + print *, "Maximum of the function f_max is at x =", max_point, "with value =", result_max + +end program ternary_search_function_based + +! Define the unimodal function f_min with a minimum near x = 5.0 +! The quadratic term (x - 5.0)**2 defines a parabola that is concave upward with a minimum at x = 5.0 +! and values increasing as x moves away from 5. +! The cosine term introduces oscillations, affecting the exact location of the minimum slightly away from 5.0. + +real(8) function f_min(x) + real(8), intent(in) :: x + f_min = (x - 5.0d0)**2 + cos(x) ! Example of a quadratic function with a cosine oscillation +end function f_min + +! Define the unimodal function f_max with a maximum near x = 5.0 +! The quadratic term -(x - 5.0)**2 defines a parabola that is concave downward with a maximum at x = 5.0 +! and values decreasing as x moves away from 5. +! The cosine term introduces oscillations, affecting the exact location of the maximum slightly away from 5.0. + +real(8) function f_max(x) + real(8), intent(in) :: x + f_max = -(x - 5.0d0)**2 + cos(x) ! Example of a quadratic function with a cosine oscillation +end function f_max diff --git a/examples/searches/recursive_linear_search.f90 b/examples/searches/recursive_linear_search.f90 index e616ab4..1251ea4 100644 --- a/examples/searches/recursive_linear_search.f90 +++ b/examples/searches/recursive_linear_search.f90 @@ -8,12 +8,12 @@ program recursive_linear_search_example integer, dimension(5) :: array - array = (/ 306, 1005, 5, 62, 0 /) + array = (/306, 1005, 5, 62, 0/) !! Search for the number 62 in the array - print*, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4. + print *, "Target = 62: ", recursive_linear_search(array, size(array), 62) !! Prints 4. !! Search for the number 10 in the array - print*, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found. + print *, "Target = 10: ", recursive_linear_search(array, size(array), 10) !! Prints -1 because item 10 is not found. end program recursive_linear_search_example diff --git a/examples/sorts/example_recursive_bubble_sort.f90 b/examples/sorts/example_recursive_bubble_sort.f90 index 1afcd11..52b2cba 100644 --- a/examples/sorts/example_recursive_bubble_sort.f90 +++ b/examples/sorts/example_recursive_bubble_sort.f90 @@ -11,11 +11,11 @@ program recursive_bubble_sort_example !! Fill the array with random numbers. call random_number(array) - print*, "Before:", array + print *, "Before:", array !! Bubble sort subroutine call. call recursive_bubble_sort(array, size(array)) - print*, "After:", array + print *, "After:", array end program recursive_bubble_sort_example diff --git a/examples/sorts/example_usage_bubble_sort.f90 b/examples/sorts/example_usage_bubble_sort.f90 index 5e9a96b..13d5349 100644 --- a/examples/sorts/example_usage_bubble_sort.f90 +++ b/examples/sorts/example_usage_bubble_sort.f90 @@ -11,11 +11,11 @@ program bubble_sort_example !! Fill the array with random numbers call random_number(array) - print*, "Before:", array + print *, "Before:", array !! Call the bubble_sort subroutine to sort the array call bubble_sort(array) - print*, "After:", array + print *, "After:", array end program bubble_sort_example diff --git a/examples/sorts/example_usage_gnome_sort.f90 b/examples/sorts/example_usage_gnome_sort.f90 index d9cd0c4..5c2cb15 100644 --- a/examples/sorts/example_usage_gnome_sort.f90 +++ b/examples/sorts/example_usage_gnome_sort.f90 @@ -9,7 +9,7 @@ program test_gnome_sort integer :: n, i ! Initialize the test array - array = (/ -5, 2, 9, 1, 5, 6, -7, 8, 15, -20 /) + array = (/-5, 2, 9, 1, 5, 6, -7, 8, 15, -20/) n = size(array) ! Call gnome_sort from the module to sort the array diff --git a/examples/sorts/example_usage_heap_sort.f90 b/examples/sorts/example_usage_heap_sort.f90 index 8f3fd9a..24c705f 100644 --- a/examples/sorts/example_usage_heap_sort.f90 +++ b/examples/sorts/example_usage_heap_sort.f90 @@ -10,7 +10,7 @@ program test_heap_sort integer, dimension(n) :: array(n) ! Test array ! Initialize the test array - array = (/ 12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1 /) + array = (/12, 11, 13, 5, 6, 7, 3, 9, -1, 2, -12, 1/) ! Print the original array print *, "Original array:" diff --git a/examples/sorts/example_usage_merge_sort.f90 b/examples/sorts/example_usage_merge_sort.f90 index 4991e8a..79893c3 100644 --- a/examples/sorts/example_usage_merge_sort.f90 +++ b/examples/sorts/example_usage_merge_sort.f90 @@ -9,7 +9,7 @@ program test_merge_sort integer :: n, i ! Initialize the test array - array = (/ -2, 3, -10, 11, 99, 100000, 100, -200 /) + array = (/-2, 3, -10, 11, 99, 100000, 100, -200/) n = size(array) ! Call merge_sort from the module to sort the array diff --git a/examples/sorts/example_usage_quick_sort.f90 b/examples/sorts/example_usage_quick_sort.f90 index 1c5bae2..c61b6dd 100644 --- a/examples/sorts/example_usage_quick_sort.f90 +++ b/examples/sorts/example_usage_quick_sort.f90 @@ -9,7 +9,7 @@ program test_quick_sort integer :: n, i ! Initialize the test array - array = (/ 10, 7, 8, 9, 1, 5, -2, 12, 0, -5 /) + array = (/10, 7, 8, 9, 1, 5, -2, 12, 0, -5/) n = size(array) ! Print the original array diff --git a/examples/sorts/example_usage_radix_sort.f90 b/examples/sorts/example_usage_radix_sort.f90 index 3424060..811819a 100644 --- a/examples/sorts/example_usage_radix_sort.f90 +++ b/examples/sorts/example_usage_radix_sort.f90 @@ -13,7 +13,7 @@ program test_radix_sort ! Test for base 10 print *, "Testing Radix Sort with base 10:" - array = (/ 170, 45, 75, 90, 802, 24, 2, 66, 15, 40 /) + 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:" @@ -23,7 +23,7 @@ program test_radix_sort ! 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 /) + 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:" @@ -33,7 +33,7 @@ program test_radix_sort ! 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 + 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:" diff --git a/modules/maths/factorial.f90 b/modules/maths/factorial.f90 index 3f6bcf5..c23f259 100644 --- a/modules/maths/factorial.f90 +++ b/modules/maths/factorial.f90 @@ -19,7 +19,7 @@ function factorial(number) result(factorial_number) factorial_number = 1 do while (counter > 1) - factorial_number = factorial_number * counter + factorial_number = factorial_number*counter counter = counter - 1 end do @@ -33,7 +33,7 @@ recursive function recursive_factorial(number) result(factorial_number) if (number .lt. 1) then factorial_number = 1 else - factorial_number = number * recursive_factorial(number - 1) + factorial_number = number*recursive_factorial(number - 1) end if end function recursive_factorial diff --git a/modules/searches/linear_search.f90 b/modules/searches/linear_search.f90 index 05298fb..c314b51 100644 --- a/modules/searches/linear_search.f90 +++ b/modules/searches/linear_search.f90 @@ -8,7 +8,7 @@ module linear_search_module !! This function searches for a target in a given collection. !! Returns the index of the found target or -1 if target is not found. - function linear_search (collection, target) result(target_index) + function linear_search(collection, target) result(target_index) integer, dimension(:), intent(in) :: collection !! A collection for elements of type integer integer, intent(in) :: target !! Target value to be searched. integer :: target_index !! Target's index in the collection to return. diff --git a/modules/searches/recursive_linear_search.f90 b/modules/searches/recursive_linear_search.f90 index 29cfe38..49a21aa 100644 --- a/modules/searches/recursive_linear_search.f90 +++ b/modules/searches/recursive_linear_search.f90 @@ -26,9 +26,9 @@ recursive function recursive_linear_search(collection, index, target) result(tar target_index = index else !! Recursively search in the remaining part of the collection - target_index = recursive_linear_search(collection, index-1, target) + target_index = recursive_linear_search(collection, index - 1, target) end if end function recursive_linear_search -end module recursive_linear_search_module \ No newline at end of file +end module recursive_linear_search_module diff --git a/modules/searches/ternary_search_module.f90 b/modules/searches/ternary_search_module.f90 new file mode 100644 index 0000000..9cd1b67 --- /dev/null +++ b/modules/searches/ternary_search_module.f90 @@ -0,0 +1,160 @@ +!> Ternary Search Algorithm Module +!! +!! This module implements two types of ternary search algorithm: array-based and function-based. +!! +!! Created by: Ramy-Badr-Ahmed (https://github.com/Ramy-Badr-Ahmed) +!! in Pull Request: #24 +!! https://github.com/TheAlgorithms/Fortran/pull/24 +!! +!! Please mention me (@Ramy-Badr-Ahmed) in any issue or pull request +!! addressing bugs/corrections to this file. Thank you! +!! +!! The array-based ternary search is used to find a target element within a sorted array, while the function-based +!! approach is used to find the minimum or maximum of a unimodal function. +!! +!! Array-based ternary search: +!! - Given a sorted array and a target, it splits the array into three parts and recursively searches for the target. +!! +!! Function-based ternary search: +!! - Used for unimodal functions, which have a single peak (maximum) or valley (minimum). +!! This method divides the function’s search space into thirds to converge on the minimum or maximum. +!! + +module ternary_search + implicit none +contains + + !> Array-based ternary search algorithm + !! This recursive function searches for the target value in a sorted array. + !! + !! Input: + !! - arr: The sorted array to search within. + !! - target: The value to search for. + !! - left, right: The range of indices within which to search. + !! + !! Output: + !! - The index of the target element if found, otherwise -1. + recursive integer function ternary_search_array(arr, target, left, right) result(result_index) + implicit none + integer, intent(in) :: arr(:) ! Array to search within + integer, intent(in) :: target ! Target value to search for + integer, intent(in) :: left, right ! Left and right indices + integer :: mid1, mid2 ! Midpoints + integer :: new_left, new_right ! Temporary indices + + ! Base case: if the range is invalid, return -1 (not found) + if (right < left) then + result_index = -1 + return + end if + + ! Divide array into three parts + mid1 = left + (right - left)/3 + mid2 = right - (right - left)/3 + + ! Check if the target is at mid1 or mid2 + if (arr(mid1) == target) then + result_index = mid1 + return + else if (arr(mid2) == target) then + result_index = mid2 + return + end if + + ! Recursive search in the appropriate third of the array + if (target < arr(mid1)) then + new_right = mid1 - 1 + result_index = ternary_search_array(arr, target, left, new_right) + else if (target > arr(mid2)) then + new_left = mid2 + 1 + result_index = ternary_search_array(arr, target, new_left, right) + else + new_left = mid1 + 1 + new_right = mid2 - 1 + result_index = ternary_search_array(arr, target, new_left, new_right) + end if + end function ternary_search_array + + !> Function-based ternary search to find the minimum of a unimodal function + !! This function finds the minimum point of a unimodal function using the ternary search algorithm. + !! + !! Input: + !! - f: The unimodal function to search. + !! - left, right: The range within which to search for the minimum. + !! - tol: The tolerance to determine convergence. + !! + !! Output: + !! - The point at which the function achieves its minimum value. + recursive real(8) function ternary_search_minimum(f, left, right, tol) result(minimum) + implicit none + interface + real(8) function f(x) + real(8), intent(in) :: x + end function f + end interface + real(8), intent(in) :: left, right, tol + real(8) :: mid1, mid2 + real(8) :: l, r + + l = left + r = right + + ! Termination condition based on tolerance + do while (r - l > tol) + mid1 = l + (r - l)/3.0d0 + mid2 = r - (r - l)/3.0d0 + + ! Compare function values at midpoints + if (f(mid1) < f(mid2)) then + r = mid2 + else + l = mid1 + end if + end do + + ! The minimum point is approximately at the midpoint + minimum = (l + r)/2.0d0 + end function ternary_search_minimum + + !> Function-based ternary search to find the maximum of a unimodal function + !! This function finds the maximum point of a unimodal function using the ternary search algorithm. + !! + !! Input: + !! - f: The unimodal function to search. + !! - left, right: The range within which to search for the maximum. + !! - tol: The tolerance to determine convergence. + !! + !! Output: + !! - The point at which the function achieves its maximum value. + recursive real(8) function ternary_search_maximum(f, left, right, tol) result(maximum) + implicit none + interface + real(8) function f(x) + real(8), intent(in) :: x + end function f + end interface + real(8), intent(in) :: left, right, tol + real(8) :: mid1, mid2 + real(8) :: l, r + + l = left + r = right + + ! Termination condition based on tolerance + do while (r - l > tol) + mid1 = l + (r - l)/3.0d0 + mid2 = r - (r - l)/3.0d0 + + ! Compare function values at midpoints + if (f(mid1) > f(mid2)) then + r = mid2 + else + l = mid1 + end if + end do + + ! The maximum point is approximately at the midpoint + maximum = (l + r)/2.0d0 + end function ternary_search_maximum + +end module ternary_search diff --git a/modules/sorts/bubble_sort.f90 b/modules/sorts/bubble_sort.f90 index b9cfb9f..3d29566 100644 --- a/modules/sorts/bubble_sort.f90 +++ b/modules/sorts/bubble_sort.f90 @@ -8,7 +8,7 @@ module bubble_sort_module contains !! This subroutine sorts the collection using bubble sort. - subroutine bubble_sort (collection) + subroutine bubble_sort(collection) real, dimension(:), intent(inout) :: collection !! A collection of real numbers to be sorted integer :: i, j, collection_size @@ -24,11 +24,11 @@ subroutine bubble_sort (collection) swapped = .false. do i = 1, j - if (collection(i) .gt. collection(i+1)) then + if (collection(i) .gt. collection(i + 1)) then !! Swap values if they are out of order in [i, i+1] region temp = collection(i) - collection(i) = collection(i+1) - collection(i+1) = temp + collection(i) = collection(i + 1) + collection(i + 1) = temp swapped = .true. !! Set swapped flag to true end if diff --git a/modules/sorts/heap_sort.f90 b/modules/sorts/heap_sort.f90 index 9ba1bf5..46b05d5 100644 --- a/modules/sorts/heap_sort.f90 +++ b/modules/sorts/heap_sort.f90 @@ -26,7 +26,7 @@ subroutine heap_sort(array, n) integer :: i ! Build the max heap - do i = n / 2, 1, -1 + do i = n/2, 1, -1 call heapify(array, n, i) end do @@ -50,8 +50,8 @@ recursive subroutine heapify(array, n, i) integer :: largest, left, right largest = i - left = 2 * i - right = 2 * i + 1 + left = 2*i + right = 2*i + 1 ! Is Left Child is larger than Root? if (left <= n .and. array(left) > array(largest)) then @@ -84,4 +84,4 @@ subroutine swap(array, i, j) end subroutine swap -end module heap_sort_module \ No newline at end of file +end module heap_sort_module diff --git a/modules/sorts/merge_sort.f90 b/modules/sorts/merge_sort.f90 index 43d3c6b..95971ec 100644 --- a/modules/sorts/merge_sort.f90 +++ b/modules/sorts/merge_sort.f90 @@ -14,7 +14,7 @@ !! - A sorted array of integers. !! module merge_sort_module -implicit none + implicit none contains @@ -23,21 +23,21 @@ 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 :: middle 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 + middle = n/2 ! Allocate space for the two halves - allocate(left_half(middle), right_half(n - middle), sorted_array(n)) + 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) + right_half = array(middle + 1:n) ! Recursively sort each half call merge_sort(left_half, middle) @@ -50,7 +50,7 @@ recursive subroutine merge_sort(array, n) array = sorted_array ! Deallocate the temporary arrays - deallocate(left_half, right_half, sorted_array) + deallocate (left_half, right_half, sorted_array) end subroutine merge_sort diff --git a/modules/sorts/radix_sort.f90 b/modules/sorts/radix_sort.f90 index 840265d..64fbeb6 100644 --- a/modules/sorts/radix_sort.f90 +++ b/modules/sorts/radix_sort.f90 @@ -37,9 +37,9 @@ subroutine radix_sort(array, n, base) exp = 1 ! Perform Counting Sort for each digit - do while (max_digit / exp >= 1) + do while (max_digit/exp >= 1) call counting_sort(array, n, exp, base) - exp = exp * base + exp = exp*base end do end subroutine radix_sort @@ -55,14 +55,14 @@ subroutine counting_sort(array, n, exp, base) integer, dimension(:), allocatable :: count, output count_size = base - allocate(count(count_size), output(n)) + allocate (count(count_size), output(n)) ! Initialize count array count = 0 ! Store count of occurrences do i = 1, n - digit = mod(array(i) / exp, base) + digit = mod(array(i)/exp, base) count(digit + 1) = count(digit + 1) + 1 end do @@ -73,7 +73,7 @@ subroutine counting_sort(array, n, exp, base) ! Build the output array do i = n, 1, -1 - digit = mod(array(i) / exp, base) + digit = mod(array(i)/exp, base) output(count(digit + 1)) = array(i) count(digit + 1) = count(digit + 1) - 1 end do @@ -82,8 +82,8 @@ subroutine counting_sort(array, n, exp, base) array = output ! Deallocate temporary arrays - deallocate(count, output) + deallocate (count, output) end subroutine counting_sort -end module radix_sort_module \ No newline at end of file +end module radix_sort_module diff --git a/tests/sorts/tests_gnome_sort.f90 b/tests/sorts/tests_gnome_sort.f90 index 1e2bdae..04ad524 100644 --- a/tests/sorts/tests_gnome_sort.f90 +++ b/tests/sorts/tests_gnome_sort.f90 @@ -6,60 +6,59 @@ program tests_gnome_sort use gnome_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_gnome_sort \ No newline at end of file +end program tests_gnome_sort diff --git a/tests/sorts/tests_heap_sort.f90 b/tests/sorts/tests_heap_sort.f90 index 1961d80..0b28b2a 100644 --- a/tests/sorts/tests_heap_sort.f90 +++ b/tests/sorts/tests_heap_sort.f90 @@ -6,60 +6,59 @@ program tests_heap_sort use heap_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_heap_sort \ No newline at end of file +end program tests_heap_sort diff --git a/tests/sorts/tests_merge_sort.f90 b/tests/sorts/tests_merge_sort.f90 index 45af1fa..a55ef6c 100644 --- a/tests/sorts/tests_merge_sort.f90 +++ b/tests/sorts/tests_merge_sort.f90 @@ -6,60 +6,59 @@ program tests_merge_sort use merge_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 4, 2, 7, 3, 1, 4, 9, 5, 10, 9, 2, 1/) + array = (/4, 2, 7, 3, 1, 4, 9, 5, 10, 9, 2, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -11, -55, -43, -70, -2, -1, -15, -9 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-11, -55, -43, -70, -2, -1, -15, -9/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 62 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/62/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 4, 4, 4, 4, 4 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/4, 4, 4, 4, 4/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 10, 2000, 20, 888, 30, 798 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/10, 2000, 20, 888, 30, 798/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_merge_sort \ No newline at end of file +end program tests_merge_sort diff --git a/tests/sorts/tests_quick_sort.f90 b/tests/sorts/tests_quick_sort.f90 index 262f251..547970a 100644 --- a/tests/sorts/tests_quick_sort.f90 +++ b/tests/sorts/tests_quick_sort.f90 @@ -6,60 +6,59 @@ program tests_quick_sort use quick_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array ! Test 1: Repeated elements print *, "Test 1: Array with repeated elements" - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array) ! Test 2: Already sorted array print *, "Test 2: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array) ! Test 3: Reverse sorted array print *, "Test 3: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array) ! Test 4: Array with all negative numbers print *, "Test 4: Array with all negative numbers" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array) ! Test 5: Single element array print *, "Test 5: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array) ! Test 6: Array with identical elements print *, "Test 6: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array) ! Test 7: Array with alternating high and low values print *, "Test 7: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array) ! Test 8: Empty array print *, "Test 8: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array) contains @@ -89,5 +88,4 @@ subroutine run_test(array) print *, "" end subroutine run_test - -end program tests_quick_sort \ No newline at end of file +end program tests_quick_sort diff --git a/tests/sorts/tests_radix_sort.f90 b/tests/sorts/tests_radix_sort.f90 index 26dcc04..22c04a2 100644 --- a/tests/sorts/tests_radix_sort.f90 +++ b/tests/sorts/tests_radix_sort.f90 @@ -6,84 +6,83 @@ program tests_radix_sort use radix_sort_module implicit none - integer :: i integer, dimension(:), allocatable :: array integer, parameter :: base10 = 10, base2 = 2, base16 = 16 ! Test 1: Base 10 print *, "Test 1: Radix Sort with base 10" - if (allocated(array)) deallocate(array) - allocate(array(10)) - array = (/ 170, 45, 75, 90, 802, 24, 2, 66, 15, 40 /) + if (allocated(array)) deallocate (array) + allocate (array(10)) + array = (/170, 45, 75, 90, 802, 24, 2, 66, 15, 40/) call run_test(array, base10) ! Test 2: Base 2 print *, "Test 2: Radix Sort with base 2" - if (allocated(array)) deallocate(array) - allocate(array(10)) - array = (/ 10, 13, 9, 14, 2, 5, 15, 6, 8, 1 /) ! Binary values as decimal + if (allocated(array)) deallocate (array) + allocate (array(10)) + array = (/10, 13, 9, 14, 2, 5, 15, 6, 8, 1/) ! Binary values as decimal call run_test(array, base2) ! Test 3: Base 16 print *, "Test 3: Radix Sort with base 16" - if (allocated(array)) deallocate(array) - allocate(array(10)) - array = (/ 171, 31, 61, 255, 16, 5, 211, 42, 180, 0 /) ! Hexadecimal values as decimal + if (allocated(array)) deallocate (array) + allocate (array(10)) + array = (/171, 31, 61, 255, 16, 5, 211, 42, 180, 0/) ! Hexadecimal values as decimal call run_test(array, base16) ! Test 4: Repeated elements print *, "Test 4: Array with repeated elements" - if (allocated(array)) deallocate(array) - allocate(array(12)) - array = (/ 5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(12)) + array = (/5, 3, 8, 3, 1, 5, 7, 5, 10, 7, 3, 1/) call run_test(array, base10) ! Test 5: Already sorted array print *, "Test 5: Already sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 1, 2, 3, 4, 5, 6, 7, 8 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/1, 2, 3, 4, 5, 6, 7, 8/) call run_test(array, base10) ! Test 6: Reverse sorted array print *, "Test 6: Reverse sorted array" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ 8, 7, 6, 5, 4, 3, 2, 1 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/8, 7, 6, 5, 4, 3, 2, 1/) call run_test(array, base10) ! Test 7: Array with all negative numbers (Note: Radix Sort only handles non-negative integers) print *, "Test 7: Array with all negative numbers (handled as base 10)" - if (allocated(array)) deallocate(array) - allocate(array(8)) - array = (/ -1, -5, -3, -7, -2, -12, -15, -4 /) + if (allocated(array)) deallocate (array) + allocate (array(8)) + array = (/-1, -5, -3, -7, -2, -12, -15, -4/) call run_test(array, base10) ! Test 8: Single element array print *, "Test 8: Single element array" - if (allocated(array)) deallocate(array) - allocate(array(1)) - array = (/ 42 /) + if (allocated(array)) deallocate (array) + allocate (array(1)) + array = (/42/) call run_test(array, base10) ! Test 9: Array with identical elements print *, "Test 9: Array with identical elements" - if (allocated(array)) deallocate(array) - allocate(array(5)) - array = (/ 7, 7, 7, 7, 7 /) + if (allocated(array)) deallocate (array) + allocate (array(5)) + array = (/7, 7, 7, 7, 7/) call run_test(array, base10) ! Test 10: Array with alternating high and low values print *, "Test 10: Array with alternating high and low values" - if (allocated(array)) deallocate(array) - allocate(array(6)) - array = (/ 1, 1000, 2, 999, 3, 998 /) + if (allocated(array)) deallocate (array) + allocate (array(6)) + array = (/1, 1000, 2, 999, 3, 998/) call run_test(array, base10) ! Test 11: Empty array print *, "Test 11: Empty array" - if (allocated(array)) deallocate(array) - allocate(array(0)) + if (allocated(array)) deallocate (array) + allocate (array(0)) call run_test(array, base10) contains