Skip to content

Commit

Permalink
fix: 'counting_sort' panicked at 'index out of bounds...
Browse files Browse the repository at this point in the history
  • Loading branch information
roninro committed Sep 18, 2019
1 parent 63e2dd2 commit aa36fcc
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rust/13_sorts/counting_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit aa36fcc

Please sign in to comment.