Merge pull request #397 from mjyi/master

fix: 'counting_sort' panicked at 'index out of bounds...
This commit is contained in:
wangzheng0822 2019-10-11 10:38:17 +08:00 committed by GitHub
commit d7aa998778
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,13 +7,14 @@ pub fn counting_sort(mut nums: Vec<i32>) -> Vec<i32> {
let nums_len = nums.len();
// 获取最大数
let mut max = nums[0];
// 申请一个长度为 max + 1 的新数组
let mut bucket = vec![0; (max+1) as usize];
let mut tmp = vec![0; nums_len];
for i in 1..nums_len {
if max < nums[i] { max = nums[i]; }
}
// 申请一个长度为 max + 1 的新数组
let mut bucket = vec![0; (max+1) as usize];
for i in 0..nums_len {
bucket[nums[i] as usize] += 1;