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

bin_insertion_sort.rs 中存在usize类型 Attempt to subtract with overflow 的问题 #3

Closed
hechengyuhui opened this issue Feb 8, 2022 · 1 comment
Labels
bug Something isn't working

Comments

@hechengyuhui
Copy link

bin_insertion_sort.rs 17行开始的以下代码段中

// 二分法找到 temp 的位置
        while left <= right {
            mid = (left + right) >> 1;
            if temp < nums[mid] {
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }

midusize 类型,若待排序数组为 [2,3,1,4] ,则会出现 0 usize - 1 的情况,造成程序 panic ,加上一段对 mid 为 0 的处理就好,代码如下

while left <= right {
            mid = (left + right) >> 1;
            if temp < nums[mid] {
                if mid == 0 {break;}
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
@QMHTMY
Copy link
Owner

QMHTMY commented Feb 9, 2022

已修复,多谢!

@QMHTMY QMHTMY closed this as completed Feb 9, 2022
@QMHTMY QMHTMY added the bug Something isn't working label Feb 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants