I think otherway round. Your line 1 probably is the only one which is good. You declared class in correct way, camel casing is also looks good.
ohit Gaikwad wrote:i do have some prblem on the line 1
Shouldn't be "swapping element 0 with any number found that's larger than him and largest among the others", or simply "swapping element 0 with the largest one in case there is such"?
Junilu Lacar wrote:1. Starting from position 0, iterate over the array, swapping element 0 with any number found that's larger than it.
Same as above, probably not smaller, but in fact smallest element within the array.
=Junilu Lacar wrote:2. Go on to the next position and iterate over the rest of the array, only this time swapping with any smaller number.
Well, I haven't thought about optimization yet
Junilu Lacar wrote:Liutauras, I think you have fallen into a premature optimization trap. By wanting to look at the entirety of the array before swapping, you are avoiding unnecessary swaps but you also make it necessary to introduce another variable to track the index with which you will potentially perform a swap at the end of an iteration. This complicates the algorithm. Not thinking about optimizing too soon helps you consider the more straightforward solution first. If you really need to optimize, then it's not that hard to add the temporary variable.
As guys mentioned, probably it is not the best approach to get involved in thinking about even and odd indices, that gets too complicated (or maybe not). It has been suggested to use so called "flag" which should give you an answer what next you need to look for. Either for a max or min number.
Rohit Gaikwad wrote:for even and call getMin(); for odd Index but how will you guide the helper methods to search like 1st you search or highest then 2nd time you look for second highest and so on same or the getMin() method too.
That is the point. You don't look for 2nd or 3rd highest.. You always look for the highest among the rest of the array, which hasn't been rearranged yet. Remember, the top max or top min numbers should be left behind you (at the beginning of the array during each iteration).
Rohit Gaikwad wrote:how will you guide the helper methods to search like 1st you search or highest then 2nd time you look for second highest
Was it most readable? Yes, sometimes there are shortest and might even most clever ways of doing some things, but not necessarily they are most readable.
Rohit Gaikwad wrote:...Trust me i Tried that already.
and this Was the easiest implementation i found
Liutauras Vilda wrote:Could you please describe us your problem in english, non Java language.
I was mistaken. Unfortunately class name is misleading. I didn't notice that yesterday, fortunately it didn't hide from my eyes today
Liutauras Vilda wrote:Your line 1 probably is the only one which is good. You declared class in correct way
That should read MaxMinPair, but not the other way round. Some wouldn't notice probably, but it is worth of correcting. For this exercise might it is not that crucial, but in most cases that would be a serious mistake.
Rohit Gaikwad wrote:
Did Steve tell you that? Fuh - Steve. Just look at this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton