Skip to content

Refactor Heap Sort Implementation #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 4, 2024

Conversation

sozelfist
Copy link
Contributor

Description

  • Implement generic heap sort that can sort arrays in both ascending and descending order.
  • Parametrized tests using macros
  • Add file and function docstrings.

Type of change

  • Refactor old implementations

Checklist:

  • I ran the below commands using the latest version of rust nightly.
  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked COUNTRIBUTING.md and my code follows its guidelines.

- Implement generic heap sort that can sort array in both ascending and descending order.
- Parametrized tests using macros
- Add file and function docstrings.
@sozelfist sozelfist requested review from imp2002 and vil02 as code owners April 23, 2024 09:20
@codecov-commenter
Copy link

codecov-commenter commented Apr 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.78%. Comparing base (1bd27f5) to head (9b73940).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #705      +/-   ##
==========================================
- Coverage   94.79%   94.78%   -0.01%     
==========================================
  Files         298      298              
  Lines       22177    22161      -16     
==========================================
- Hits        21022    21006      -16     
  Misses       1155     1155              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

- Test cases are defined as tuples containing the input vector and the expected output vector
- The `run_test_case` function runs the sorting algorithm on the input vector twice: once in ascending order and once in descending order, and then asserts that the results match the expected output vectors
- The `test_heap_sort` function iterates over each test case and runs the run_test_case function for each of them
- Use two pre-defined methods `is_sorted` and `is_descending_sorted` to assert sorted arrays in ascending and descending orders respectively
- Remove predefined outputs in tests cases
@sozelfist
Copy link
Contributor Author

sozelfist commented Apr 29, 2024

Can you please have a look at this PR, @vil02?

Copy link
Member

@vil02 vil02 left a comment

Choose a reason for hiding this comment

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

What do you think about expressing test like that:

#[cfg(test)]
mod tests {
    use crate::sorting::{have_same_elements, heap_sort, is_descending_sorted, is_sorted};

    macro_rules! test_heap_sort {
        ($($name:ident: $input:expr,)*) => {
            $(
                #[test]
                fn $name() {
                    let input_array = $input;
                    let mut arr_asc = input_array.clone();
                    heap_sort(&mut arr_asc, true);
                    assert!(is_sorted(&arr_asc) && have_same_elements(&arr_asc, &input_array));

                    let mut arr_dsc = input_array.clone();
                    heap_sort(&mut arr_dsc, false);
                    assert!(is_descending_sorted(&arr_dsc) && have_same_elements(&arr_dsc, &input_array));
                }
            )*
        }
    }

    test_heap_sort! {
        empty_array: Vec::<i32>::new(),
        single_element_array: vec![5],
        sorted: vec![1, 2, 3, 4, 5],
        sorted_desc: vec![5, 4, 3, 2, 1, 0],
        basic_0: vec![9, 8, 7, 6, 5],
        basic_1: vec![8, 3, 1, 5, 7],
        basic_2: vec![4, 5, 7, 1, 2, 3, 2, 8, 5, 4, 9, 9, 100, 1, 2, 3, 6, 4, 3],
        duplicated_elements: vec![5, 5, 5, 5, 5],
        strings: vec!["aa", "a", "ba", "ab"],
    }
}

Please note that the current tests would pass if the input array would be always changed to an empty array.

@sozelfist
Copy link
Contributor Author

Thanks! Your suggestion is more concise and definite.

@sozelfist sozelfist requested a review from vil02 May 4, 2024 13:31
@vil02 vil02 merged commit 260b448 into TheAlgorithms:master May 4, 2024
@sozelfist sozelfist deleted the ref/sort/heap_sort branch May 5, 2024 00:48
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.

3 participants