I am currently studying the median of medians alg.
after studying from wiki, I have a question: what if the input size is not divisible by 5? how to find the median by using median of medians algorithm?
I am currently studying the median of medians alg.
after studying from wiki, I have a question: what if the input size is not divisible by 5? how to find the median by using median of medians algorithm?
You can proceed with the median-of-medians algorithm even if the input doesn't have a length that is a multiple of five in several ways. One option is to have the final block just contain the leftover elements and take its median and proceeding as before. Another option is to completely ignore the elements when splitting the elements into blocks of five and taking their median, since the pivot found by ignoring fewer than five elements will not noticeably degrade the quality of the pivot found this way (that is, you'll get roughly a 70 / 30 split ignoring the up to four missing elements, and adding those in isn't going to mess with the fraction too much).
Hope this helps!