home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / shell.ex < prev    next >
Text File  |  1994-03-08  |  1KB  |  68 lines

  1.             --------------------------
  2.             -- Shell sort Benchmark --
  3.             --------------------------
  4. without type_check -- makes no difference
  5.  
  6. constant BATCH = 100
  7. constant BENCH_TIME = 15
  8.  
  9. constant TRUE = 1
  10.  
  11. sequence list, slist
  12.  
  13. function shell_sort(sequence x)
  14. -- Shell sort 
  15. -- will sort any sequence of integers
  16.     integer gap, j
  17.     integer first, last 
  18.     integer tempi, tempj
  19.  
  20.     last = length(x)
  21.     gap = floor(last / 4) + 1
  22.     while TRUE do
  23.      first = gap + 1
  24.     for i = first to last do
  25.         tempi = x[i]
  26.         j = i - gap 
  27.         while TRUE do
  28.         tempj = x[j]
  29.         if tempi >= tempj then
  30.             j = j + gap
  31.             exit
  32.         end if
  33.         x[j+gap] = tempj
  34.         if j <= gap then
  35.             exit
  36.         end if
  37.         j = j - gap
  38.         end while
  39.         x[j] = tempi
  40.     end for
  41.     if gap = 1 then
  42.         return x
  43.     else
  44.         gap = floor(gap / 4) + 1 
  45.     end if
  46.     end while
  47. end function
  48.  
  49. list = {9, 34, 14, 18, 33, 46, 11, 13, 7, 26, 22, 10, 36, 40, 2, 28, 32, 1,
  50.     23, 31, 43, 5, 24, 42, 45, 50, 16, 3, 47, 39, 21, 49, 41, 6, 19, 30,
  51.     20, 35, 44, 38, 25, 15, 27, 17, 8, 4, 29, 37, 48, 12}
  52.  
  53. atom t, cycles
  54.  
  55. puts(1, "shell sort benchmark ...\n")
  56. cycles = 0
  57. t = time()
  58. while time() < t + BENCH_TIME do
  59.     for i = 1 to BATCH do 
  60.         slist = shell_sort(list) 
  61.     end for
  62.     cycles = cycles + BATCH
  63. end while
  64. t = time() - t
  65. printf(1, "%6.1f sorts per second\n", cycles / t)
  66. ? slist
  67.  
  68.