home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1720 < prev    next >
Internet Message Format  |  1990-12-28  |  3KB

  1. From: ajz@mentor.cc.purdue.edu.UUCP
  2. Newsgroups: alt.sources
  3. Subject: Re: Sorting Algorithm
  4. Message-ID: <FooBarmentor.cc.purdue.edu>
  5. Date: 27 Aug 90 07:18:24 GMT
  6.  
  7.  
  8. Having recently taken a course where a 3 week project was based upon sorting,
  9. here are my notes...
  10.  
  11. Here's the background, the sort was performed on four 9-digit integers where
  12. a seperate file told which of those 4 integers was the most significant key and
  13. which was the least.  The total time spent in the program had to be less than
  14. 4 seconds for a 4000 item list (ours was about 1 second and although .15
  15. seconds could have shaved off of it [ie, by killing all the error checking
  16. routines], I saw one that did it in .5 seconds).
  17.  
  18. The first thing you should know is that any textbook routine like quicksort,
  19. mergesort, or even a radix sort failed to sort a random 4000 item list in under
  20. 4 seconds on our Gould NP-1.
  21.  
  22. This is what my team learned...
  23. 1) Traversing a linked list to locate an item is very time consuming.
  24. 2) Dumb O(n^2) routines have very little overhead making them MUCH FASTER than
  25.    O(n log n) routines (which have high overheads) for small values of n (<15).
  26. 3) An insertion sort [O(n^2)] is one of the fastest sorts for nearly sorted
  27.    data especially when the unsorted items are close to each other.
  28. 4) Recursive routines took more time than non-recursive ones.
  29.  
  30. The best routine I saw chain hashed the values into a 33333 item array, then
  31. it took the array and rebuilt a linked list out of it, and finally it ran an
  32. insertion sort on the list.  For random data, there should be very few cases
  33. where more than 5% of the items in the rebuilt linked list will be out of 
  34. place.  Of these out of place items, they should be uniformly distributed
  35. within the linked list so that they don't have to be moved more than 3 spaces
  36. to place them back into their proper position.
  37.  
  38. Since a 4000 item list will only fill the hash table to ~10%, there should be
  39. on average only 1.1 probes needed for each item.  It should therefore take
  40. O(1.1n + n + c) where the second "n" comes from the insertion sort having to
  41. traverse the entire list at least once and the "c" is a constant depending upon
  42. how many items were out of place and how far they had to be moved on the
  43. average (shouldn't be more than 4000 * 5% * 3 = 600 or n * 5% * 3 = .15n).
  44. It should therefore average O(2.25n), have a best case of O(2n), and have
  45. a worst case of O(2n^2).
  46.  
  47. The routine my team wrote insertion sorted every group of 12 integers, placed
  48. this group into an larger array, and then merge sorted this array.  This gave
  49. a average time of O((n / 12) log (n / 12) + (12^2 / 4) * (n / 12)), a best
  50. case of O((n / 12) log (n / 12) + n), and a worst case of O((n / 12) log
  51. (n / 12) + (12^2 / 2) * (n / 12)).
  52.  
  53. For 4000 items...
  54.                 First routine    Second routine
  55. average time    O(9000)          O(14793.6)
  56. best time       O(8000)          O(6793.6)
  57. worst time      O(3.2 * 10^7)    O(26793.6)
  58.  
  59. I should stress that the worst case for the second routine is pretty straight
  60. forward to find, but the worst case determination of the first routine would
  61. be quite a task.  In practice, chances are the second routine will operate
  62. under unfavorable conditions many more times than the first routine.
  63.  
  64. -- 
  65.  
  66. T. Tim Hsu                           UUCP    ...pur-ee!ajz@mentor.cc.purdue.edu
  67.                                      ARPA    ajz@mentor.cc.purdue.edu
  68. FAX  1 317 494 0566                  BITNET  xajz@PURCCVM
  69.