Guest User

Untitled

a guest
Jan 9th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. public double median(int[] arr)
  2. {
  3. Arrays.sort(arr);
  4. double position = ((double)arr.length - 1) / 2;
  5. if(position == (int)position)
  6. {
  7. return (double)arr[(int)osition];
  8. }
  9. else
  10. {
  11. double high = position + 0.5;
  12. double low = position - 0.5;
  13. return (arr[(int)low] + arr[(int)high]) / 2;
  14. }
  15. }
  16.  
  17. inputs:
  18. median(new int[]{2,5,4,1,5,3})
  19. median(new int[]{1,3,2,10,2,1,3,4})
  20. median(new int[]{1,2,3,4,5})
  21. median(new int[]{2,4,6,8})
  22. median(new int[]{10, 7, -4, 5, 2, -100, 23})
Advertisement
Add Comment
Please, Sign In to add comment