Skip to content

Conversation

@Ramy-Badr-Ahmed
Copy link
Member

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed commented Sep 10, 2024

Description:

This pull request introduces an implementation of the Heap Sort algorithm.

Definition:

  • Heap Sort is an efficient comparison-based sorting algorithm that utilises a binary heap data structure to sort arrays.
  • This algorithm builds a heap from the input array, repeatedly extracts the maximum element from the heap, and rebuilds the heap until the array is sorted.
  • This algorithm is known for its O(n log n) time complexity.

Implementation Details:

heap_sort.f90: Contains the heap_sort_module with:

heap_sort: A subroutine that sorts the input array using the heap sort algorithm.
heapify: A subroutine that maintains the heap property for a given subtree.
swap: A helper subroutine that swaps two elements in the array.

The implementation follows the module structure as outlined in the contribution guidelines.

Example Usage:

example_usage_heap_sort.f90: A test program that demonstrates the heap_sort subroutine from the heap_sort_module. It sorts a sample array and prints the results.

Additional Test Cases:

tests_heap_sort.f90: A test program that includes a variety of test cases to validate the heap_sort implementation.


Reference

The Algorithm Design Manual, Latest edition

Copy link
Member Author

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SatinWukerORIG
Looking forward to your review 🙂

Copy link
Member

@SatinWukerORIG SatinWukerORIG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code is fine, but it would be better if you could add more test cases! 👍

@Ramy-Badr-Ahmed
Copy link
Member Author

The code is fine, but it would be better if you could add more test cases! 👍

Thanks, @SatinWukerORIG

Sure, I will work on adding more test cases 🙂

Ramy-Badr-Ahmed

This comment was marked as outdated.

Copy link
Member Author

@Ramy-Badr-Ahmed Ramy-Badr-Ahmed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @SatinWukerORIG

I've added more test cases for the heap_sort module.

Copy link
Member

@SatinWukerORIG SatinWukerORIG left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the cases that you made 👍
Do you think writing the algorithm and test cases inside one file is better? Or is it easier to write in separate files? In other repositories of TheAlgorithms (like Python and Java), we put everything under one file. Fortran is an old and redundant language, do you think which way is more readable?

@Ramy-Badr-Ahmed
Copy link
Member Author

I like the cases that you made 👍
Do you think writing the algorithm and test cases inside one file is better? Or is it easier to write in separate files? In other repositories of TheAlgorithms (like Python and Java), we put everything under one file. Fortran is an old and redundant language, do you think which way is more readable?

Thanks 🙂

In a language with verbose syntax like Fortran, combining both algorithm logic and test logic in one file can result in longer files that are harder to navigate. Separating them encourages modularity and clarity, especially when dealing with many test cases or complex algorithms.

We can probably use subdirectores to group the relevant files: algorithm, example usage, and test cases, by algorithm name.

For instance, we could use a structure like this:

|-- sorts
    -->heap_sort
        --> heap_sort_module.f90
        --> example_usage_heap_sort.f90
        --> tests_heap_sort.f90
    --> quick_sort  
        --> quick_sort_module.f90
        --> example_usage_quick_sort.f90
        --> tests_quick_sort.f90
    --> radix_sort  
        --> radix_sort_module.f90
        --> example_usage_radix_sort.f90
        --> tests_radix_sort.f90
    ⋮

In the Java repository they seprate the two logics: Java Repository.

In contrast, in the Python repository, combining the two works well because of Python’s concise syntax. Test cases are often embedded directly in the docstrings alongside the algorithm code, as seen in this example from the Python repository.

Let’s not forget that Fortran still holds value in fields like scientific computing, high-performance computing, and numerical analysis. Its performance and optimisation capabilities enable it to endure in computationally intensive domains. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants