Skip to content
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

rust "binary_search" return type -> choose uszie or i32 ? #479

Closed
XIAOZHUXUEJAVA opened this issue Apr 30, 2023 · 5 comments
Closed

rust "binary_search" return type -> choose uszie or i32 ? #479

XIAOZHUXUEJAVA opened this issue Apr 30, 2023 · 5 comments

Comments

@XIAOZHUXUEJAVA
Copy link

I think the binary search in Rust could be written like this:

pub fn binary_search(nums: &[i32], target: i32) -> Option<usize> {
        let mut low = 0;
        let mut high = nums.len();
        while low < high {
            let mid = (high - low) / 2 + low;
            match target.cmp(&nums[mid]) {
                std::cmp::Ordering::Equal => return Some(mid),
                std::cmp::Ordering::Less => high = mid,
                std::cmp::Ordering::Greater => low = mid + 1,
            }
        }
        None
}

This is more in line with Rust's language features, maybe

@krahets
Copy link
Owner

krahets commented May 3, 2023

@sjinzh @xBLACKICEx

@krahets
Copy link
Owner

krahets commented May 3, 2023

Simplicity is one of the principles of this book.

This looks like a language syntax sugar that doesn't suppress the if ... else ... in performance or simplicity.

            match target.cmp(&nums[mid]) {
                std::cmp::Ordering::Equal => return Some(mid),
                std::cmp::Ordering::Less => high = mid,
                std::cmp::Ordering::Greater => low = mid + 1,
            }

Using usize is more formal and with greater adaptivity. However, it is not vital for the subject of data structure and algorithms.

@XIAOZHUXUEJAVA
Copy link
Author

XIAOZHUXUEJAVA commented May 3, 2023

The code use of while i <= j and j = m - 1 in the code may lead to boundary issues (left inclusive, right inclusive interval).

In Rust, array indices must be of non-negative integer type usize. This means that if we want to access an element of an array, we must provide a non-negative integer index, which means that the code will fail when the target value is smaller than all array elements.

Of course, my programming level is not high, and my analysis may not be accurate.

Of course, you are not wrong that data structures and algorithms are indeed largely language-independent, but in my view, there is a problem with this code. If you do not think so, you can ignore this.

    #[test]
    fn test_binary_search() {
        let nums = [2, 3, 4, 7, 9];
        assert_eq!(-1, BinarySearch::binary_search_i(&nums, 1));
    }

image

For instance:
image
image

@krahets
Copy link
Owner

krahets commented May 3, 2023

It is designed to return -1 when not finding the target. I believe it is fine to return i32 type here.

@krahets krahets closed this as completed May 3, 2023
@krahets krahets reopened this Aug 13, 2023
@krahets
Copy link
Owner

krahets commented Aug 13, 2023

@night-cruise Could you please track the issue, thanks!

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

No branches or pull requests

2 participants