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

fix: 'counting_sort' panicked at 'index out of bounds... #397

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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