Re: how to do it!!



jhb92678@xxxxxxxxx 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)
.



Relevant Pages

  • Re: Using an array with an additional independent cell value
    ... The problem as you now state it can be solved with this CSE formula... ... This means i want to reach the smallest positive number in the array. ... Just get the Kth smallest value from the original ... > take the array values by subtracting a constant from each array value. ...
    (microsoft.public.excel.programming)
  • Re: Using an array with an additional independent cell value
    ... if you are subtracting the same constant value from each array element, wouldn't the Kth smallest element stay the same? ... Just get the Kth smallest value from the original array and subtract your constant from whichever value is returned. ... take the array values by subtracting a constant from each array value. ...
    (microsoft.public.excel.programming)
  • Re: nonhomogenous structs (was: lisp performance questions and observations)
    ... ints ... naturally represented as an array of structs, ... So actually, I don't care about the mixture between bytes, longs, ... machine words for each struct before I even get to the "data" for the ...
    (comp.lang.lisp)
  • Re: Working on ARC4-16 bit
    ... > Or does your method of initializing the state array accomplish ... outputs where n is the state array size was fairly sure. ... What i'm doing is to initialize the array, then discard ...
    (sci.crypt)
  • Re: [PATCH 0/7] discard support revisited
    ... but we'll get some real array coverage soon. ... UNMAP or WRITE SAME ... ... TRIM tracking is not actually enabled anywhere by default for now. ... That should translate the discard request ...
    (Linux-Kernel)