diff --git a/DIRECTORY.md b/DIRECTORY.md index 5cf3c38..240c5d3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) diff --git a/sorts/example_usage_gnome_sort.f90 b/sorts/example_usage_gnome_sort.f90 new file mode 100644 index 0000000..d9cd0c4 --- /dev/null +++ b/sorts/example_usage_gnome_sort.f90 @@ -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 diff --git a/sorts/example_usage_heap_sort.f90 b/sorts/example_usage_heap_sort.f90 new file mode 100644 index 0000000..a394dcc --- /dev/null +++ b/sorts/example_usage_heap_sort.f90 @@ -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 diff --git a/sorts/example_usage_quick_sort.f90 b/sorts/example_usage_quick_sort.f90 new file mode 100644 index 0000000..1c5bae2 --- /dev/null +++ b/sorts/example_usage_quick_sort.f90 @@ -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 diff --git a/sorts/example_usage_radix_sort.f90 b/sorts/example_usage_radix_sort.f90 new file mode 100644 index 0000000..3424060 --- /dev/null +++ b/sorts/example_usage_radix_sort.f90 @@ -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 + diff --git a/sorts/gnome_sort.f90 b/sorts/gnome_sort.f90 new file mode 100644 index 0000000..b92e912 --- /dev/null +++ b/sorts/gnome_sort.f90 @@ -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 diff --git a/sorts/heap_sort.f90 b/sorts/heap_sort.f90 new file mode 100644 index 0000000..9ba1bf5 --- /dev/null +++ b/sorts/heap_sort.f90 @@ -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 \ No newline at end of file diff --git a/sorts/quick_sort.f90 b/sorts/quick_sort.f90 new file mode 100644 index 0000000..fa1bbd4 --- /dev/null +++ b/sorts/quick_sort.f90 @@ -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 diff --git a/sorts/radix_sort.f90 b/sorts/radix_sort.f90 new file mode 100644 index 0000000..840265d --- /dev/null +++ b/sorts/radix_sort.f90 @@ -0,0 +1,89 @@ +!> Radix Sort Algorithm + +!> This module implements the Radix Sort algorithm with configurable base. +!! +!! Radix Sort is a non-comparison-based sorting algorithm that sorts numbers by processing individual digits. +!! It is particularly efficient for sorting large lists of integers with a fixed number of digits. +!! +!! Time Complexity: O(d * (n + k)) where n is the number of elements, d is the number of digits, and k is the range of digits. +!! +!! Input: +!! - An array of non-negative integers. +!! - Base (radix) of the numbers to be sorted. +!! +!! Output: +!! - A sorted array of integers. +module radix_sort_module + implicit none + +contains + + !> Subroutine to perform Radix Sort on an array + subroutine radix_sort(array, n, base) + implicit none + integer, dimension(:), intent(inout) :: array ! Input/output array to be sorted + integer, intent(in) :: n ! Size of the array + integer, intent(in) :: base ! Base (radix) for sorting + integer :: max_digit, exp + + ! Check if base is valid + if (base < 2) then + print *, "Error: Base must be greater than or equal to 2." + return + end if + + ! Find the maximum number to determine the number of digits + max_digit = maxval(array) + exp = 1 + + ! Perform Counting Sort for each digit + do while (max_digit / exp >= 1) + call counting_sort(array, n, exp, base) + exp = exp * base + end do + + end subroutine radix_sort + + !> Subroutine to perform Counting Sort based on the digit represented by exp + subroutine counting_sort(array, n, exp, base) + implicit none + integer, dimension(:), intent(inout) :: array ! Input/output array to be sorted + integer, intent(in) :: n ! Size of the array + integer, intent(in) :: exp ! Exponent for digit place + integer, intent(in) :: base ! Base (radix) for sorting + integer :: i, count_size, digit + integer, dimension(:), allocatable :: count, output + + count_size = base + 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) + count(digit + 1) = count(digit + 1) + 1 + end do + + ! Change count[i] so that count[i] contains the actual position of this digit in output[] + do i = 2, count_size + count(i) = count(i) + count(i - 1) + end do + + ! Build the output array + do i = n, 1, -1 + digit = mod(array(i) / exp, base) + output(count(digit + 1)) = array(i) + count(digit + 1) = count(digit + 1) - 1 + end do + + ! Copy the sorted elements into the original array + array = output + + ! Deallocate temporary arrays + deallocate(count, output) + + end subroutine counting_sort + +end module radix_sort_module \ No newline at end of file diff --git a/sorts/tests_gnome_sort.f90 b/sorts/tests_gnome_sort.f90 new file mode 100644 index 0000000..1e2bdae --- /dev/null +++ b/sorts/tests_gnome_sort.f90 @@ -0,0 +1,93 @@ +!> Test program for the Gnome Sort algorithm + +!! This program provides additional test cases to validate the gnome_sort_module. + +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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + call run_test(array) + + ! Test 8: Empty array + print *, "Test 8: Empty array" + if (allocated(array)) deallocate(array) + allocate(array(0)) + call run_test(array) + +contains + + !> Subroutine to run and print the gnome sort test + subroutine run_test(array) + integer, dimension(:), intent(inout) :: array + integer :: n, i + + n = size(array) + + ! Print original array + print *, "Original array:" + do i = 1, n + print *, array(i) + end do + + ! Call gnome_sort + call gnome_sort(array) + + ! Print sorted array + print *, "Sorted array:" + do i = 1, n + print *, array(i) + end do + + print *, "" + end subroutine run_test + + +end program tests_gnome_sort \ No newline at end of file diff --git a/sorts/tests_heap_sort.f90 b/sorts/tests_heap_sort.f90 new file mode 100644 index 0000000..1961d80 --- /dev/null +++ b/sorts/tests_heap_sort.f90 @@ -0,0 +1,93 @@ +!> Test program for the Heap Sort algorithm + +!! This program provides additional test cases to validate the heap_sort_module. + +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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + call run_test(array) + + ! Test 8: Empty array + print *, "Test 8: Empty array" + if (allocated(array)) deallocate(array) + allocate(array(0)) + call run_test(array) + +contains + + !> Subroutine to run and print the heap sort test + subroutine run_test(array) + integer, dimension(:), intent(inout) :: array + integer :: n, i + + n = size(array) + + ! Print original array + print *, "Original array:" + do i = 1, n + print *, array(i) + end do + + ! Call heap_sort + call heap_sort(array, n) + + ! Print sorted array + print *, "Sorted array:" + do i = 1, n + print *, array(i) + end do + + print *, "" + end subroutine run_test + + +end program tests_heap_sort \ No newline at end of file diff --git a/sorts/tests_quick_sort.f90 b/sorts/tests_quick_sort.f90 new file mode 100644 index 0000000..262f251 --- /dev/null +++ b/sorts/tests_quick_sort.f90 @@ -0,0 +1,93 @@ +!> Test program for the Quick Sort algorithm + +!! This program provides additional test cases to validate the quick_sort_module. + +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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + call run_test(array) + + ! Test 8: Empty array + print *, "Test 8: Empty array" + if (allocated(array)) deallocate(array) + allocate(array(0)) + call run_test(array) + +contains + + !> Subroutine to run and print the quick sort test + subroutine run_test(array) + integer, dimension(:), intent(inout) :: array + integer :: n, i + + n = size(array) + + ! Print original array + print *, "Original array:" + do i = 1, n + print *, array(i) + end do + + ! Call quick_sort + call quick_sort(array, 1, n) ! (1: low bound , n: high bound) of the array + + ! Print sorted array + print *, "Sorted array:" + do i = 1, n + print *, array(i) + end do + + print *, "" + end subroutine run_test + + +end program tests_quick_sort \ No newline at end of file diff --git a/sorts/tests_radix_sort.f90 b/sorts/tests_radix_sort.f90 new file mode 100644 index 0000000..26dcc04 --- /dev/null +++ b/sorts/tests_radix_sort.f90 @@ -0,0 +1,118 @@ +!> Test program for the Radix Sort algorithm + +!! This program provides additional test cases to validate the radix_sort_module. +!! The radix (base) parameter affects the internal digit processing for sorting, but the final output is always in decimal form. + +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 /) + 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 + 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 + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + 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 /) + call run_test(array, base10) + + ! Test 11: Empty array + print *, "Test 11: Empty array" + if (allocated(array)) deallocate(array) + allocate(array(0)) + call run_test(array, base10) + +contains + + !> Subroutine to run and print the radix sort test + subroutine run_test(array, base) + integer, dimension(:), intent(inout) :: array + integer, intent(in) :: base + integer :: n, i + + n = size(array) + + ! Print original array + print *, "Original array:" + do i = 1, n + print *, array(i) + end do + + ! Call radix_sort + ! The parameters specify the array to sort, its size, and the base for sorting. + call radix_sort(array, n, base) + + ! Print sorted array + print *, "Sorted array:" + do i = 1, n + print *, array(i) + end do + + print *, "" + end subroutine run_test + +end program tests_radix_sort