home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / programm / 3611 < prev    next >
Encoding:
Internet Message Format  |  1993-01-27  |  3.3 KB

  1. Path: sparky!uunet!comp.vuw.ac.nz!waikato.ac.nz!aukuni.ac.nz!cs18.cs.aukuni.ac.nz!jeremy
  2. Newsgroups: comp.programming
  3. Subject: Re: is it possible to find max & next smallest in one pass?
  4. Message-ID: <1993Jan28.010659.8273@cs.aukuni.ac.nz>
  5. From: jeremy@cs.aukuni.ac.nz (Jeremy Gibbons)
  6. Date: Thu, 28 Jan 1993 01:06:59 GMT
  7. References: <1993Jan25.224200.7195@bnlux1.bnl.gov>
  8. Organization: Computer Science Dept. University of Auckland
  9. Cc: jeremy@cs.aukuni.ac.nz
  10. Lines: 61
  11.  
  12. In <1993Jan25.224200.7195@bnlux1.bnl.gov> murphy@sscdaq.phy.bnl.gov (Michael Murphy) writes:
  13.  
  14. >>jos@and.nl (Jos Horsmeier) writes:
  15.  
  16. >>>In article <asean.727858796@armstrong.ecn.purdue.edu> asean@armstrong.ecn.purdue.edu (Sean Ahern) writes:
  17. >>>|jagrant@emr1.emr.ca (John Grant) writes:
  18. >>>|>Given an array of values, is it possible to find the maximum of
  19. >>>|>the array and the next smallest value in a single pass?  
  20. >>>|
  21. >>|If you were able to do this, the algorithm should be able to extend to the
  22. >>|next smallest, and the next, with little or no trouble.
  23. >>|
  24. >>|But the requirement of the algorithm was to do this in one pass.  This would
  25. >>|give us an O(n) sorting algorithm that works on arbitrary data.  I don't
  26. >>|believe such an animal exists with only one processor.
  27. >>
  28. >>I beg to differ here. Lets do some big Oh calculations. Given n elements
  29. >>in an array, one can find the maximum value in O(n) operations. One can 
  30. >>find the next value in O(n) operations too, so both can be done in 
  31. >>O(n)+O(n)= O(2*n) operations. But constants don't count here, so the
  32. >>whole job can be done uisng O(n) operations. 
  33. >
  34. >This is true
  35.  
  36. No, it isn't. Or at least, not in the way you think. Generalising the
  37. obvious algorithm for finding the largest two elements out of n to an
  38. algorithm for finding the largest k elements out of n leads to O(nk)
  39. comparisons (keeping a sorted list of the k largest so far), or to O(n log
  40. k) (keeping a balanced binary tree of the k largest so far), in the worst
  41. case. This is only O(n) if k is considered fixed, not part of the problem.
  42.  
  43. Put another way: if you allow yourself arithmetic with big Os, then
  44.    O(n)+O(n) = O(n)
  45.    O(n)+O(n)+O(n) = O(n)
  46. etc, but
  47.    O(n)+O(n)+...+O(n) [k times]  is not  O(n)
  48. Which just goes to show that you can't trust arithmetic with big Os. :-)
  49.  
  50. >>I suppose a good way to find the k smallest elements of an array would be
  51. >>to do a O(n*logn) sort and then pull off the smallest k elements.  Though I
  52. >>must admit that if you only want the first 2 smallest, the O(2*n) algorithm
  53. >>would be better for n>4.
  54. >
  55. >The problem of finding the k-smallest elements of an array is often called 
  56. >"Selection."  Algorithms exist that can perform Selection in guaranteed 
  57. >linear time.  
  58.  
  59. Actually, "Selection" refers to finding the (single) kth-smallest element,
  60. not to all k smallest elements. I don't know of any way of finding all k
  61. smallest elements for arbitrary k in O(n) comparisons in the worst case.
  62. (Information-theoretic arguments show that you can't find all k smallest
  63. elements for arbitrary k in O(n) comparisons in the worst case *and* return
  64. them in sorted order, because then you could sort in O(n) comparisons.)
  65.  
  66. Jeremy
  67.  
  68. ---
  69. Jeremy Gibbons <jeremy@cs.aukuni.ac.nz>         tel: +64 9 373 7599
  70.    Department of Computer Science,              fax: +64 9 373 7453
  71.    University of Auckland, Private Bag 92019, Auckland, New Zealand.
  72.  
  73.