473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

how to do it!!

I have readed a book, there is a sentence like this:"Suppose you
have a group of n numbers and would like to determine the kth largest",
i want to know how to do it.i can't understand

I am a newcomer,and i have apologized for posting it on wrong place!

Sep 1 '06 #1
1 1390
jh******@gmail.com said:
I have readed a book, there is a sentence like this:"Suppose you
have a group of n numbers and would like to determine the kth largest",
i want to know how to do it.i can't understand

I am a newcomer,and i have apologized for posting it on wrong place!
The comp.programming newsgroup would probably be a better match for your
question, but briefly, I suggest that your first attempt should work
something like this:

Set up an array of k numbers.

Populate the array with the first k numbers from your input.

Sort the array, with the lowest value being first in the array.

For each of the remaining (n - k) numbers, do this:
Compare the new number with the first element in the array.
If it is smaller or equal:
Discard it.
Otherwise:
Replace that element with the new number.
Taking care not to run off the end of the array...
While the new number is greater than the element to its right:
Swap the new number with the one to its right

At the end of this process, the kth largest number will be in position 0 of
the array.

Once you have a working solution, you will probably want to find ways to
improve its performance. But for now, the most important thing is to get
something that works. If you get stuck, let us know, and show us your best
attempt so far.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Sep 1 '06 #2

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

Similar topics

1
by: lawrence | last post by:
What is the PHP equivalent of messaging, as in Java?
6
by: Chewy509 | last post by:
Hi Everyone, I'll just start, and say I am not a PHP developer (I'm a sysadmin, who has gotten lumped with a non-working website). But since I like to do this type of stuff, I though I might...
11
by: Paul Neave | last post by:
Hi group, hope you can help. I have a URL on my website that is currently under the directory mysite.com/something/ But I'd like to change the URL to mysite.com/anotherthing/ This is peasy...
3
by: Randell D. | last post by:
Folks, When I read data from my form, I sanitize it before recording it in MySQL. At the moment, the values in $_POST are cleaned and returned in a new array called $formData. I found my...
7
by: Nick Messick | last post by:
I'm trying to modify a banner display function so it can be including on remote websites. If I just include the file from a remote website the code isn't executed before it is put into the page. ...
4
by: Phil Powell | last post by:
create table if not exists nnet_produkt_varegruppe ( nnet_produkt_varegruppe_id int not null auto_increment, primary key(nnet_produkt_varegruppe_id), nnet_produkt_varegruppe_navn varchar(255) not...
6
by: lawrence | last post by:
Most of our designers prefer to work in Dreamweaver. They now, this month, want to produce templates that are valid XHTML 1.0. In Dreamweaver, this is as simple as clicking a button. Up till...
8
by: Nick Messick | last post by:
I have a multi step signup form that I'm building and I have the following question. Should I do it this way: <?php if (blah == 1) { ?><table><td width="100">........etc <?php } else { ?>...
2
by: Phil Powell | last post by:
I have a variable with a value that I've verified countless times by using both echo and print_r; both indicate the variable exists and has a value. The moment I instantiate a class where this...
4
by: Bruce W...1 | last post by:
My PHP html file includes a header include, a form, and a footer include. After the user submits their information I want to replace the form part of the page with a thank you note. All the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.