From 65f3b3d20742573d2a444a6c6aa2ff86ea66afcf Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Tue, 10 Sep 2024 12:38:33 +0200 Subject: [PATCH 01/10] Implemented Heap Sort Algorithm --- DIRECTORY.md | 2 + sorts/example_usage_heap_sort.f90 | 29 +++++++++++ sorts/heap_sort.f90 | 87 +++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) create mode 100644 sorts/example_usage_heap_sort.f90 create mode 100644 sorts/heap_sort.f90 diff --git a/DIRECTORY.md b/DIRECTORY.md index 5cf3c38..76ebe28 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -14,3 +14,5 @@ * [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) diff --git a/sorts/example_usage_heap_sort.f90 b/sorts/example_usage_heap_sort.f90 new file mode 100644 index 0000000..5259d0e --- /dev/null +++ b/sorts/example_usage_heap_sort.f90 @@ -0,0 +1,29 @@ +!> Test 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, dimension(12) :: array ! Test array + integer :: n, i + + ! 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/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 From 86432872009a690fe6c9dfa18aa0fa8df89152df Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Tue, 10 Sep 2024 13:12:40 +0200 Subject: [PATCH 02/10] Implemented Gnome Sort Algorithm --- DIRECTORY.md | 2 ++ sorts/example_usage_gnome_sort.f90 | 22 ++++++++++++ sorts/gnome_sort.f90 | 54 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 sorts/example_usage_gnome_sort.f90 create mode 100644 sorts/gnome_sort.f90 diff --git a/DIRECTORY.md b/DIRECTORY.md index 76ebe28..33464f1 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -16,3 +16,5 @@ * [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) +* [gnome_sort](/sorts/gnome_sort.f90) +* [example_usage_gnome_sort](/sorts/example_usage_gnome_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/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 From 7653cc965ab8f2a3a42067a954a1bf0d28e45c11 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Tue, 10 Sep 2024 20:05:16 +0200 Subject: [PATCH 03/10] Implemented Quick Sort Algorithm --- DIRECTORY.md | 2 + sorts/example_usage_quick_sort.f90 | 30 ++++++++++++ sorts/quick_sort.f90 | 75 ++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+) create mode 100644 sorts/example_usage_quick_sort.f90 create mode 100644 sorts/quick_sort.f90 diff --git a/DIRECTORY.md b/DIRECTORY.md index 33464f1..a075e76 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -18,3 +18,5 @@ * [example_usage_heap_sort](/sorts/example_usage_heap_sort.f90) * [gnome_sort](/sorts/gnome_sort.f90) * [example_usage_gnome_sort](/sorts/example_usage_gnome_sort.f90) +* [quick_sort](/sorts/quick_sort.f90) +* [example_usage_quick_sort](/sorts/example_usage_quick_sort.f90) diff --git a/sorts/example_usage_quick_sort.f90 b/sorts/example_usage_quick_sort.f90 new file mode 100644 index 0000000..c94fe50 --- /dev/null +++ b/sorts/example_usage_quick_sort.f90 @@ -0,0 +1,30 @@ +!> Test 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) + + ! 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/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 From 5828f8306aaaec04249d9076eaeb3254b55c2efe Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Thu, 12 Sep 2024 14:08:22 +0200 Subject: [PATCH 04/10] Added more test cases for heap_sort.f90 module. --- DIRECTORY.md | 1 + sorts/example_usage_heap_sort.f90 | 8 ++- sorts/tests_heap_sort.f90 | 93 +++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 sorts/tests_heap_sort.f90 diff --git a/DIRECTORY.md b/DIRECTORY.md index 76ebe28..de00190 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -16,3 +16,4 @@ * [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) diff --git a/sorts/example_usage_heap_sort.f90 b/sorts/example_usage_heap_sort.f90 index 5259d0e..a394dcc 100644 --- a/sorts/example_usage_heap_sort.f90 +++ b/sorts/example_usage_heap_sort.f90 @@ -1,12 +1,13 @@ -!> Test program for the Heap Sort algorithm +!> 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, dimension(12) :: array ! Test array - integer :: n, i + 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 /) @@ -26,4 +27,5 @@ program test_heap_sort do i = 1, n print *, array(i) end do + end program test_heap_sort diff --git a/sorts/tests_heap_sort.f90 b/sorts/tests_heap_sort.f90 new file mode 100644 index 0000000..e228505 --- /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 6: 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 7: 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 From c43fd2f9718cdec7fbba53c5dbe7ade11b0e151a Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Thu, 12 Sep 2024 14:37:34 +0200 Subject: [PATCH 05/10] Added more test cases for gnome_sort.f90 module. --- DIRECTORY.md | 1 + sorts/tests_gnome_sort.f90 | 93 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 sorts/tests_gnome_sort.f90 diff --git a/DIRECTORY.md b/DIRECTORY.md index 33464f1..98f2637 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -18,3 +18,4 @@ * [example_usage_heap_sort](/sorts/example_usage_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) diff --git a/sorts/tests_gnome_sort.f90 b/sorts/tests_gnome_sort.f90 new file mode 100644 index 0000000..e63eef9 --- /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 6: 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 7: 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 From e124185e8363888d6d0b3e49bf6c53361c0d3126 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Thu, 12 Sep 2024 14:43:33 +0200 Subject: [PATCH 06/10] Corrected DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index de00190..36917de 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -17,3 +17,6 @@ * [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) From d24917c3964f9cc8b5525129222d6756e983ebc2 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Thu, 12 Sep 2024 15:28:42 +0200 Subject: [PATCH 07/10] Added more test cases for quick_sort.f90. Corrected DIRECTORY.md. --- DIRECTORY.md | 3 +- sorts/example_usage_quick_sort.f90 | 5 +- sorts/tests_quick_sort.f90 | 93 ++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 sorts/tests_quick_sort.f90 diff --git a/DIRECTORY.md b/DIRECTORY.md index 6df9ee5..b4da9b3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -21,4 +21,5 @@ * [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) +* [example_usage_quick_sort](/sorts/example_usage_quick_sort.f90) +* [tests_quick_sort](/sorts/tests_quick_sort.f90) \ No newline at end of file diff --git a/sorts/example_usage_quick_sort.f90 b/sorts/example_usage_quick_sort.f90 index c94fe50..9bd2df6 100644 --- a/sorts/example_usage_quick_sort.f90 +++ b/sorts/example_usage_quick_sort.f90 @@ -1,4 +1,4 @@ -!> Test program for the Quick Sort algorithm +!> 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 @@ -19,6 +19,9 @@ program test_quick_sort end do ! Call quick_sort from the module to sort the array + ! Call quick_sort from the module to sort the array + ! The parameters 1 and n specify the indices for the low and high bounds of the array, respectively, + ! as defined in the quick_sort_module. call quick_sort(array, 1, n) ! Print the sorted array diff --git a/sorts/tests_quick_sort.f90 b/sorts/tests_quick_sort.f90 new file mode 100644 index 0000000..72878d5 --- /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 bounds 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 From de9fb4975d7ac931297b771ab54af8dd1fbc3e41 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Thu, 12 Sep 2024 15:32:25 +0200 Subject: [PATCH 08/10] Removed duplicate comments --- sorts/example_usage_quick_sort.f90 | 5 +---- sorts/tests_quick_sort.f90 | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/sorts/example_usage_quick_sort.f90 b/sorts/example_usage_quick_sort.f90 index 9bd2df6..1c5bae2 100644 --- a/sorts/example_usage_quick_sort.f90 +++ b/sorts/example_usage_quick_sort.f90 @@ -19,10 +19,7 @@ program test_quick_sort end do ! Call quick_sort from the module to sort the array - ! Call quick_sort from the module to sort the array - ! The parameters 1 and n specify the indices for the low and high bounds of the array, respectively, - ! as defined in the quick_sort_module. - call quick_sort(array, 1, n) + call quick_sort(array, 1, n) ! (1: low bound , n: high bound) of the array ! Print the sorted array print *, "Sorted array:" diff --git a/sorts/tests_quick_sort.f90 b/sorts/tests_quick_sort.f90 index 72878d5..262f251 100644 --- a/sorts/tests_quick_sort.f90 +++ b/sorts/tests_quick_sort.f90 @@ -78,7 +78,7 @@ subroutine run_test(array) end do ! Call quick_sort - call quick_sort(array, 1, n) ! 1: low bound & n: high bounds of the array + call quick_sort(array, 1, n) ! (1: low bound , n: high bound) of the array ! Print sorted array print *, "Sorted array:" From 4a02455457b592389d1c9ca0ab28d409203bfcab Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Thu, 12 Sep 2024 15:33:23 +0200 Subject: [PATCH 09/10] Minor change to test numbers --- sorts/tests_heap_sort.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/tests_heap_sort.f90 b/sorts/tests_heap_sort.f90 index e228505..1961d80 100644 --- a/sorts/tests_heap_sort.f90 +++ b/sorts/tests_heap_sort.f90 @@ -49,14 +49,14 @@ program tests_heap_sort array = (/ 7, 7, 7, 7, 7 /) call run_test(array) - ! Test 6: Array with alternating high and low values + ! 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 7: Empty array + ! Test 8: Empty array print *, "Test 8: Empty array" if (allocated(array)) deallocate(array) allocate(array(0)) From 173bc30e5060c4752dd7c5dec7b7255854988689 Mon Sep 17 00:00:00 2001 From: Ramy-Badr-Ahmed Date: Thu, 12 Sep 2024 15:34:17 +0200 Subject: [PATCH 10/10] Minor change to test numbers --- sorts/tests_gnome_sort.f90 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/tests_gnome_sort.f90 b/sorts/tests_gnome_sort.f90 index e63eef9..1e2bdae 100644 --- a/sorts/tests_gnome_sort.f90 +++ b/sorts/tests_gnome_sort.f90 @@ -49,14 +49,14 @@ program tests_gnome_sort array = (/ 7, 7, 7, 7, 7 /) call run_test(array) - ! Test 6: Array with alternating high and low values + ! 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 7: Empty array + ! Test 8: Empty array print *, "Test 8: Empty array" if (allocated(array)) deallocate(array) allocate(array(0))