Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

second maximum value from the array

 
kri shan
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to find second maximum value from the array ? I know how to find the maximum value from the array.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is a list of numbers

2, 19, 496, 22, 84, 196, 12

Tell me how YOU would find the second largest? work it out with pencil and paper.
 
kri shan
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2, 19, 496, 22, 84, 196, 12

Take each element and compare with other elements and swap the place for sorting/descending.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is a much better way to do it. You don't need any swapping or sorting. Just make sure to choose the correct initial value before you get to the array.
 
kri shan
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just make sure to choose the correct initial value before you get to the array. I do not understand this statement.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48968
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you start searching, you need a value for max or whatever your largest result is to be. Choosing the wrong value for that will, of course, mean you fail to find the correct maximum. The same applies if you are looking for second largest.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12125
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let's try this approach...

How would you find the maximum? Once you know how to do that, there are several other approaches you can take, but we should start with baby steps.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic