Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public double median(int[] arr)
- {
- Arrays.sort(arr);
- double position = ((double)arr.length - 1) / 2;
- if(position == (int)position)
- {
- return (double)arr[(int)osition];
- }
- else
- {
- double high = position + 0.5;
- double low = position - 0.5;
- return (arr[(int)low] + arr[(int)high]) / 2;
- }
- }
- inputs:
- median(new int[]{2,5,4,1,5,3})
- median(new int[]{1,3,2,10,2,1,3,4})
- median(new int[]{1,2,3,4,5})
- median(new int[]{2,4,6,8})
- median(new int[]{10, 7, -4, 5, 2, -100, 23})
Advertisement
Add Comment
Please, Sign In to add comment