Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Second Highest number in an array

Reply
Thread Tools

Second Highest number in an array

 
 
Jaspreet
Guest
Posts: n/a
 
      09-23-2005
I was working on some database application and had this small task of
getting the second highes marks in a class. I was able to do that using
subqueries.

Just thinking what is a good way of getting second highest value in an
integer array. One method I know of is to make the 1st pass through the
array and find the highest number. In the second pass we can find the
highest number which is less than the number we obtained in the 1st
pass.

However there has to be a better and more efficient way of doing this.
Please just let me know some hints and I would try it on my own in C.

Thanks and have an enjoyable weekend.

 
Reply With Quote
 
 
 
 
Anonymous 7843
Guest
Posts: n/a
 
      09-23-2005
In article <(E-Mail Removed) .com>,
Jaspreet <(E-Mail Removed)> wrote:
>
> Just thinking what is a good way of getting second highest value in an
> integer array. One method I know of is to make the 1st pass through the
> array and find the highest number. In the second pass we can find the
> highest number which is less than the number we obtained in the 1st
> pass.
>
> However there has to be a better and more efficient way of doing this.
> Please just let me know some hints and I would try it on my own in C.


Just make one pass through the array. Keep two variables,
which track:

* highest so far
* 2nd highest so far

As you iterate through the array, when you find a new "highest"
one, move the previous "highest" to "2nd highest." Plus, if
you happen upon an element that his higher than the 2nd highest
but not as high as the first, make that the 2nd highest without
disturbing the highest.

As you extend the idea to finding (for example) 490th highest
element it becomes quite a bit less efficient, since it's
basically an insertion sort. At some point it's better to
just sort the array and then everything is positioned perfectly.
 
Reply With Quote
 
 
 
 
Alexei A. Frounze
Guest
Posts: n/a
 
      09-23-2005
"Jaspreet" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Just thinking what is a good way of getting second highest value in an
> integer array. One method I know of is to make the 1st pass through the
> array and find the highest number. In the second pass we can find the
> highest number which is less than the number we obtained in the 1st
> pass.
>
> However there has to be a better and more efficient way of doing this.
> Please just let me know some hints and I would try it on my own in C.


This is OT since it has nothing to do with C. It's just an algorithm, which
is a language independent thing.

Hint anyway: instead of having 2 passes and 1 running variable in each of
them you may have 1 pass and 2 running vars (highest & 2nd highest) in it.

Alex


 
Reply With Quote
 
Patrick M.
Guest
Posts: n/a
 
      09-23-2005
Jaspreet wrote:
> I was working on some database application and had this small task of
> getting the second highes marks in a class. I was able to do that using
> subqueries.
>
> Just thinking what is a good way of getting second highest value in an
> integer array. One method I know of is to make the 1st pass through the
> array and find the highest number. In the second pass we can find the
> highest number which is less than the number we obtained in the 1st
> pass.
>
> However there has to be a better and more efficient way of doing this.
> Please just let me know some hints and I would try it on my own in C.
>
> Thanks and have an enjoyable weekend.
>


Well, it may not be the _most_ efficient, but it's more efficient than
the way you're thinking of. What you could do is make a copy array (you
don't even need to, unless you don't want to edit the original array,
and since it's a database application you probably don't) of the
original array, pass through it once, sorting it from highest to lowest.
Then, the highest value of the array will be in array[0], and the
second highest value in the array will be in array[1].

--
Patrick M.
/* EOF */
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      09-23-2005
In article <(E-Mail Removed) .com>,
"Jaspreet" <(E-Mail Removed)> wrote:

> I was working on some database application and had this small task of
> getting the second highes marks in a class. I was able to do that using
> subqueries.
>
> Just thinking what is a good way of getting second highest value in an
> integer array. One method I know of is to make the 1st pass through the
> array and find the highest number. In the second pass we can find the
> highest number which is less than the number we obtained in the 1st
> pass.
>
> However there has to be a better and more efficient way of doing this.
> Please just let me know some hints and I would try it on my own in C.


Loop through the array once. Keep track of the largest and second largest
element found so far. You can ignore everything that is smallest than
the second largest object found so far.
 
Reply With Quote
 
Christian Kandeler
Guest
Posts: n/a
 
      09-26-2005
Patrick M. wrote:

> Well, it may not be the _most_ efficient, but it's more efficient than
> the way you're thinking of. What you could do is make a copy array (you
> don't even need to, unless you don't want to edit the original array,
> and since it's a database application you probably don't) of the
> original array, pass through it once, sorting it from highest to lowest.
> Then, the highest value of the array will be in array[0], and the
> second highest value in the array will be in array[1].


Please show us the O(n) sorting algorithm you use for that.


Christian
 
Reply With Quote
 
=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=
Guest
Posts: n/a
 
      09-26-2005
Jaspreet wrote:
> I was working on some database application and had this small task of
> getting the second highes marks in a class. I was able to do that using
> subqueries.
>
> Just thinking what is a good way of getting second highest value in an
> integer array. One method I know of is to make the 1st pass through the
> array and find the highest number. In the second pass we can find the
> highest number which is less than the number we obtained in the 1st
> pass.
>
> However there has to be a better and more efficient way of doing this.
> Please just let me know some hints and I would try it on my own in C.

If you keep it sorted while you build your array, you can do e.g.
a binary search on the array.
Also consider reading about binary trees.
 
Reply With Quote
 
Jaspreet
Guest
Posts: n/a
 
      09-26-2005
Nils O. Selåsdal wrote:
> Jaspreet wrote:
> > I was working on some database application and had this small task of
> > getting the second highes marks in a class. I was able to do that using
> > subqueries.
> >
> > Just thinking what is a good way of getting second highest value in an
> > integer array. One method I know of is to make the 1st pass through the
> > array and find the highest number. In the second pass we can find the
> > highest number which is less than the number we obtained in the 1st
> > pass.
> >
> > However there has to be a better and more efficient way of doing this.
> > Please just let me know some hints and I would try it on my own in C.

> If you keep it sorted while you build your array, you can do e.g.
> a binary search on the array.
> Also consider reading about binary trees.


Hi

Thanks a lot guys. Yes it seems it would have been better if I had some
foresight and had maintained the array in the sorted order.

Thanks once again.

 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      09-26-2005
Anonymous 7843 wrote:
> Jaspreet <(E-Mail Removed)> wrote:
> > Just thinking what is a good way of getting second highest value in an
> > integer array. One method I know of is to make the 1st pass through the
> > array and find the highest number. In the second pass we can find the
> > highest number which is less than the number we obtained in the 1st
> > pass.
> >
> > However there has to be a better and more efficient way of doing this.
> > Please just let me know some hints and I would try it on my own in C.

>
> Just make one pass through the array. Keep two variables,
> which track:
>
> * highest so far
> * 2nd highest so far
>
> As you iterate through the array, when you find a new "highest"
> one, move the previous "highest" to "2nd highest." Plus, if
> you happen upon an element that his higher than the 2nd highest
> but not as high as the first, make that the 2nd highest without
> disturbing the highest.


For specifically the second highest, I will endorse this algorithm.
You are hardly going to do better.

> As you extend the idea to finding (for example) 490th highest
> element it becomes quite a bit less efficient, since it's
> basically an insertion sort.


Well, for the mth highest this algorithm is basically O(m^2*n).
However, there exists an O(n) "kth rank" algorithm. See:

http://www.nist.gov/dads/HTML/select.html
http://en.wikipedia.org/wiki/Selection_algorithm

Unfortunately, I have not seen a really good explanation for why the
implementation truly matches the analysis. However, if you think about
it, its not hard to see that they are right. The "group of 5" are not
adjacent sub-elements, but in fact seperated by n/5 offsets, and then
each group is just shifted down one element at a time, with some number
of tail entries with only 4 elements each. In this way, the median of
5 steps are O(n) and the final partitioning does not require additional
movement operations.

> At some point it's better to just sort the array and then everything is
> positioned perfectly.


Well, O(n) < O(n*ln(n)), so sorting is only going to be better if you
have many "kth element requests" relative to the number of insert or
delete operations.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/

 
Reply With Quote
 
Joe Wright
Guest
Posts: n/a
 
      01-27-2006
Jaspreet wrote:
> I was working on some database application and had this small task of
> getting the second highes marks in a class. I was able to do that using
> subqueries.
>
> Just thinking what is a good way of getting second highest value in an
> integer array. One method I know of is to make the 1st pass through the
> array and find the highest number. In the second pass we can find the
> highest number which is less than the number we obtained in the 1st
> pass.
>
> However there has to be a better and more efficient way of doing this.
> Please just let me know some hints and I would try it on my own in C.
>
> Thanks and have an enjoyable weekend.
>


#include <stdio.h>
int main(void) {
int pri, sec, i, v;
int arr[] = {4,10,3,8,6,7,2,7,9,2,0};
pri = sec = 0;
for (i = 0; arr[i]; ++i) {
v = arr[i];
if (v > pri) sec = pri, pri = v;
if (v > sec && v < pri) sec = v;
}
printf("pri is %d, sec is %d\n", pri, sec);
return 0;
}

One trip through the array.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
strip all but second second line from bottom and then strip that!!!! yelipolok Perl Misc 4 01-27-2010 08:14 AM
Motherboard with the highest number of PCI-Express slots Luca Villa Computer Support 2 01-07-2008 07:00 PM
sort a number highest to lowest rhitz1218@gmail.com C Programming 17 12-11-2006 04:25 AM
OT: Number Nine, Number Nine, Number Nine Frisbee® MCSE 37 09-26-2005 04:06 PM
Rounding to next highest number? NoKetch C Programming 7 12-15-2003 11:23 PM



Advertisments