-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix median algorithm of math package
- Loading branch information
1 parent
2b218a2
commit 2ab63be
Showing
1 changed file
with
11 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
use sort::{ | ||
quick_sort, | ||
} | ||
use sort::{quick_sort} | ||
|
||
pub fn median(mut slice: []int): f64 { | ||
slice = quick_sort(slice) | ||
let length: int = slice.len | ||
let mut median: f64 = 0 | ||
|
||
if length % 2 == 0 { | ||
median = (slice[(length / 2) - 1] + slice[(length / 2)]) / 2.0 | ||
} else { | ||
median = slice[length/2] | ||
} | ||
let l = slice.len | ||
match { | ||
| l == 0: | ||
ret 0 | ||
|
||
| l%2 == 0: | ||
ret f64(slice[l/2-1] + slice[l/2]) / 2 | ||
|
||
ret median | ||
|: | ||
ret f64(slice[l/2]) | ||
} | ||
} |