Skip to content
Advertisement

Can someone help me with my homework – Implementation of findMedian(…)

Problem

I have a task that reads as follows:

Implement an IntegerList class with a Vector list object attribute that contains a collection of integers. Implement the findMedian () method which, for a given object from o of the IntegerList class, returns a number m from the vector o.list such that there are at least half of the numbers less than or equal to m, and the numbers greater than or equal to m are also at least half:

For example, for [1, 4, 1, 3, 5, 7] it will be the number 4, and for [1, 1, 1, 2, 2] it will be the number 1 and only 1. If the vector is empty then the method is supposed to throw an IllegalArgumentException exception.

What I tried?

JavaScript

My question:

Why this not working? What would you change to even better solve this problem?

Advertisement

Answer

It is working, but only on empty list. To make it work: we should apply what we learned ..and little try.

Voila:

JavaScript

javadoc

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement