home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / programm / 3140 < prev    next >
Encoding:
Text File  |  1992-11-14  |  1.5 KB  |  38 lines

  1. Newsgroups: comp.programming
  2. Path: sparky!uunet!ukma!nntp.msstate.edu!emory!athena.cs.uga.edu!groucho.dev.uga.edu!is
  3. From: is@groucho.dev.uga.edu (Bob Stearns)
  4. Subject: Re: Partial sort/Nearest neighbor
  5. Message-ID: <1992Nov13.200417.12814@athena.cs.uga.edu>
  6. Sender: news@athena.cs.uga.edu
  7. Organization: University of Georgia
  8. References: <1e0a35INN511@agate.berkeley.edu>
  9. Date: Fri, 13 Nov 1992 20:04:17 GMT
  10. Lines: 26
  11.  
  12. In article <1e0a35INN511@agate.berkeley.edu> shein@nima.berkeley.edu (Soren Hein) writes:
  13. >I have a small list (9 or 25 elements) of integers between 0 and 255.  
  14. >My goal is to find the average of the K elements which are closest in
  15. >absolute value to a given element in the list.  K is typically about
  16. >half of the list length. I need the algorithm to be as fast as possible;
  17. >space consumption is no problem.
  18. >
  19. Try the following:
  20.    Initialize S to the empty list, heap representation
  21.    Let T be your original set
  22.    for t in T do
  23.      if |S|<k, insert t-t_0 into s, in sorted order by magnitude
  24.      if |S|>k then
  25.        if |t-t_0| < |S[k]| then
  26.           remove S[K] and insert |t-t_0| into k in sorted order by magnitude
  27.    end
  28.    average S for your new value.
  29. This seems to do the trick in O(nlogk) time. Another approach, which
  30. should approach O(n), is a modified Quick Sort algorithm. You only have
  31. to continue to curry the partition containing k, using the absolute
  32. value function as your comparison operator.
  33. -- 
  34. Bob Stearns
  35. University of Georgia
  36. is@groucho.dev.uga.edu
  37. (404)542-5110
  38.