473,836 Members | 1,254 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Second largest

Can anybody tell me a method to

Use only N + O(log n) comparisons to find the second largest (or
smallest) element in a list of N elements.

Thnx in advance

Aug 1 '07
40 2198
CBFalconer <cb********@yah oo.comwrote:
Kelsey Bjarnason wrote:
Convention is to count only the most significant factor; thus an
algorithm which runs in O(N) + 1000 time is an O(N) algorithm, the
constant is discarded.

In the description above, the function should run in N + O(log N)
time. As N increases, runtime increases in proportion to it, with
log N adding a small and ever less significant portion of the
runtime; thus the O(log n) is discarded and the algorithm is thus
found to be O(N).

It's not a convention, it's reality. The technique is intended to
describe execution time required for large values of N. For large
enough N the value of log N is negligible, compared to N, and is
thus discarded.
No, it's convention. Reality is that a function which is X*O(N) +
Y*O(log N) tends to O(N) or to O(log N), depending on the relative sizes
of X and Y, for reasonable N. O(N) only dominates the time for _all_ X
if you go to Microsoft-sales-figures values of N. For most of us, such
an algorithm is O(N) if X is large relative to Y, and O(log N) if X is
small compared to Y. Assuming that N will always tend to be large enough
to dominate both X and Y, that _is_ mere convention.
(For example: even though it is by convention O(N), an algorithm which
runs through an array once, to determine maximum and minimum values, and
then performs an O(log N) operation on it quickly using those values,
can be faster for all data sets which occur in reality than an algorithm
which is also O(log N), but which runs slower because it doesn't use the
min/max values.)

Richard
Aug 3 '07 #11
In article <c4************ @spanky.localho st.net>,
Kelsey Bjarnason <kb********@gma il.comwrote:
>What's wrong with N + O(log N) (assuming the n should be N)? It has a
perfectly well-defined meaning, that if you subtract N from the number
of comparisons, the result is O(log N).
>Convention is to count only the most significant factor; thus an algorithm
which runs in O(N) + 1000 time is an O(N) algorithm, the constant is
discarded.

In the description above, the function should run in N + O(log N) time.
As N increases, runtime increases in proportion to it, with log N adding a
small and ever less significant portion of the runtime; thus the O(log
n) is discarded and the algorithm is thus found to be O(N).
I'm not sure whether you're disagreeing with me or not...

An algorithm that does N + O(log N) comparisons does indeed do O(N)
comparisons, but the former statement is more precise. There are
algorithms that do O(N) comparisons but more than N + O(log N), for
example one that does 2N.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Aug 3 '07 #12
ravi wrote:
Use only N + O(log N) comparisons to find the second largest
(or smallest) element in a list of N elements.
You're in the wrong newsgroup. (I think you want comp.programmin g)
http://en.wikipedia.org/wiki/Selection_algorithm
Aug 3 '07 #13
Army1987 wrote:
if (a - b - abs(a - b))
/* a is greater than or equal to b */;
else
/* a is smaller than b */;

so you can do anything with 0 comparisons.
How do you suppose abs() is defined?
Aug 3 '07 #14
Spoon said:
Army1987 wrote:
>if (a - b - abs(a - b))
/* a is greater than or equal to b */;
else
/* a is smaller than b */;

so you can do anything with 0 comparisons.

How do you suppose abs() is defined?
As a function that takes ints, not elements (whereas the OP specifically
asked for elements, not necessarily ints, from which we may assume that
the OP is after a general solution).

Another objection to the above is the behaviour when a - b < INT_MIN.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 3 '07 #15
On Fri, 03 Aug 2007 13:38:20 +0200, Spoon wrote:
Army1987 wrote:
>if (a - b - abs(a - b))
/* a is greater than or equal to b */;
else
/* a is smaller than b */;

so you can do anything with 0 comparisons.

How do you suppose abs() is defined?
I don't think anything in the Standard forbids it to be defined
like
int abs(int _n)
{
long double _x = n * n;
return (int)nearbyintl (sqrtl(_x));
}
provided that long double has enough precision. I don't think it
is actually implemented like that anywhere, but you can always
write your own function which does that. :-)

--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 3 '07 #16
Richard Bos wrote:
CBFalconer <cb********@yah oo.comwrote:
.... snip ...
>
>It's not a convention, it's reality. The technique is intended to
describe execution time required for large values of N. For large
^^^^^^^^^^^^
>enough N the value of log N is negligible, compared to N, and is
thus discarded.

No, it's convention. Reality is that a function which is X*O(N) +
Y*O(log N) tends to O(N) or to O(log N), depending on the relative sizes
of X and Y, for reasonable N. O(N) only dominates the time for _all_ X
....

Are you deliberately trying to create confusion? Above I specified
(see the underlined area) "large values". These are not "expected
values" nor "mean values", etc. They are of unlimited size. If
you don't like a size, go bigger. In the particular example, for
large N, log N becomes negligible when compared to N.

--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."

--
Posted via a free Usenet account from http://www.teranews.com

Aug 3 '07 #17
On Fri, 03 Aug 2007 15:41:54 -0400, CBFalconer wrote:
--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."
Where did you get that quote?
(And unfortunately a future in which people smart enough to do that
will want to bother because somebody pays them to do so doesn't
seem that unlikely.)

(I only used Vista once or twice, to try to solve a problem a
friend of mine was having. Both times I gave up and the second
time I kind of asked him to get lost until he stopped using it.)
--
Army1987 (Replace "NOSPAM" with "email")
"Never attribute to malice that which can be adequately explained
by stupidity." -- R. J. Hanlon (?)

Aug 4 '07 #18
Army1987 wrote:
On Fri, 03 Aug 2007 15:41:54 -0400, CBFalconer wrote:
>"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."

Where did you get that quote?
>From someone in some Linux group. I acknowledged the theft at the
time, but forget where I stole it.

--
"Vista is finally secure from hacking. No one is going to 'hack'
the product activation and try and steal the o/s. Anyone smart
enough to do so is also smart enough not to want to bother."
--
Posted via a free Usenet account from http://www.teranews.com

Aug 4 '07 #19
Kelsey Bjarnason wrote:
[snips]

On Mon, 06 Aug 2007 10:36:04 -0400, Eric Sosman wrote:
>Kelsey Bjarnason wrote:
[snip]
>>If my choice is expected running time, I'd have to choose heapsort, as
its worst case is O(n log n), unless I'm working with sufficiently
small, or sufficiently well-defined data sets that O(n^2) degeneration
doesn't affect the outcome significantly enough to matter, or it can be
proven that quicksort can be designed to avoid degenerate behaviour in
all cases.

If the choice is based on *expected* running time, then
you'd be making the wrong choice. Worst-case running time is a
different criterion.

On the contrary. *Expected* run time *includes* worst case, unless you
can guarantee non-degenerate data sets. Since you can't do that in most
cases, you *expect* to run across them at least occasionally.
[ ... ]
Maybe that degenerate case only happens one in a thousand runs... but
considering the scope of the failure when it does happen, even odds like
that make it worth using the slightly less efficient algorithm which
doesn't degenerate.
Or you could always use an hybrid algorithm that switches according to
runtime behaviour.

Aug 6 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
12280
by: news1.on.sympatico.ca | last post by:
what is the literal for the largest character value in java? ie. 111 in binary is representing 7 (4+2+1) <-- this is 3 bit now java is 32 or 64 bit what is the largest character value ? thanks
2
8897
by: Keke922 | last post by:
I have to write a program that allows the user to enter a series of integers and -99 when they want to exit the loop. How do I display the largest and smallest number the user entered?
21
13493
by: Jaspreet | last post by:
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.
6
1446
by: thomasp | last post by:
For those who gave advice on the shortfalls of my first attempt at writing a vb.net class, Thank You. I hope that I was able to apply some of your advice to this larger atempt. At first I didn' t really see an advantage of a Class over a module containing the same functions, but now that this Class is working for me, I have found a possible use. Since this class will hold all the values used in a report I am building, I think I will now...
20
5113
by: Rajesh | last post by:
Hello Everybody, Can anybody help me to write a C program for finding the second largest element in an array. without using any sort algo. The array may conatin duplicate elements. The algo should run in O(n) time. Rajesh
3
3954
by: HEMH6 | last post by:
Who can help solve this problem??? Finding the Largest Value (a) write a function, largest(), that returns the largest value in a signed integer array. The array and its size are passed as arguments. (b)Write a main program that inputs MAX values from the keyboard into a signed integer array, array, and points, using largest(), the largest value to the screen.
38
2472
by: bele_harshad2006 | last post by:
how can i pick up largest no from 5 rows by 5 column matrix????
8
6956
by: pichukakiran | last post by:
Query how to get second and third largest values from database
8
12191
crystal2005
by: crystal2005 | last post by:
I am writing a program that receive maximum of 25 line of string each has 20 characters maximum. The program will print the smallest and the largest string. However the following program gives me Segmentation fault (core dumped) :(( It looks simple but i have no idea what went wrong.... Can anyone help me out?? #include<stdio.h> #include<stdlib.h> #include<string.h> #define MAX_INPUT 25
0
9813
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10582
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7778
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6976
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4446
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3106
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.