数组中的第二个最高数字 [英] Second Highest number in an array

查看:83
本文介绍了数组中的第二个最高数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一些数据库应用程序,并且这个小任务

在课堂上获得第二个高分。我能够使用

子查询来做到这一点。


想到什么是获得第二高价值的好方法

整数数组。我知道的一种方法是让第一次通过

数组并找到最高数字。在第二遍中,我们可以找到

最高数字,该数字小于我们在第一张

通行证中获得的数字。


但是必须有一种更好,更有效的方法。

请让我知道一些提示,我会在C中独自尝试。


谢谢,享受愉快的周末。

解决方案

文章< 11 ********* *************@g43g2000cwa.googlegroups .com> ;,

Jaspreet< js *********** @ gmail.com>写道:


只是想想在
整数数组中得到第二高的值的好方法。我所知道的一种方法是让第一次通过
数组并找到最高数字。在第二遍中,我们可以找到最高的数字,这个数字小于我们在第一次通过时获得的数字。

然而,必须有一个更好,更有效的方式这样做。
请让我知道一些提示,我会在C中自己尝试。




只需一次通过数组。保留两个变量,

跟踪:


*目前为止最高

*目前为止第二高


当你遍历数组时,当你找到一个新的最高

时,移动前一个最高的数字。到第二高。另外,如果

你发现他的元素高于第二高价

但不高于第一个元素,那么这就是第二高的没有

扰乱最高点。


当您将想法扩展到寻找(例如)第490个最高

元素时,它变得相当低效,因为这是

基本上是插入排序。在某些时候,

只需对数组进行排序,然后一切都完美定位。


Jaspreet写道:

我正在研究一些数据库应用程序,并且这个小任务是在课堂上获得第二个高分。我能够使用
子查询来做到这一点。

只是想想在
整数数组中得到第二高值的好方法。我所知道的一种方法是让第一次通过
数组并找到最高数字。在第二遍中,我们可以找到最高的数字,这个数字小于我们在第一次通过时获得的数字。

然而,必须有一个更好,更有效的方式这样做。
请让我知道一些提示,我会在C中自己尝试。

谢谢,度过一个愉快的周末。




好​​吧,它可能不是_most_高效,但它比你想象的方式更有效率。

。你能做的就是制作一个复制数组(除非你不想编辑原来的数组,否则你不需要b

$ b它是你可能不是原始阵列的数据库应用程序,通过它一次,从最高到最低排序。

然后,最高数组的值将在数组[0]中,并且数组中第二个最高值

将在数组[1]中。


-

Patrick M.

/ * EOF * /


" Jaspreet" < JS *********** @ gmail.com>在消息中写道

news:11 ********************** @ g43g2000cwa.googlegr oups.com ...

只是想想在
整数数组中得到第二高值的好方法。我所知道的一种方法是让第一次通过
数组并找到最高数字。在第二遍中,我们可以找到最高的数字,这个数字小于我们在第一次通过时获得的数字。

然而,必须有一个更好,更有效的方式这样做。
请让我知道一些提示,我会在C中自己尝试。




这是OT,因为它没有任何内容这是一个算法,它是一个算法,它是一个独立于语言的东西。


无论如何提示:而不是2次传递和1次运行在每个

中你可以有1个通行证和2个正在运行的vars(最高和第二高)。


Alex


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.

解决方案

In article <11**********************@g43g2000cwa.googlegroups .com>,
Jaspreet <js***********@gmail.com> 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.


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 */


"Jaspreet" <js***********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr 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


这篇关于数组中的第二个最高数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆