home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / SORT.E < prev    next >
Text File  |  1993-03-16  |  901b  |  41 lines

  1. -- sort.e: sort a sequence of arbitrary objects into ascending order
  2.  
  3. -- If you don't want the default ordering of objects in Euphoria 
  4. -- write your own compare() function to override the builtin compare,
  5. -- then include this file after it.
  6.  
  7. global function sort(sequence x)
  8. -- Shell sort - will sort any sequence of Euphoria objects
  9.  
  10.     integer gap, j, first, last
  11.     object tempi, tempj
  12.  
  13.     last = length(x)
  14.     gap = floor(last / 3) + 1
  15.     while 1 do
  16.     first = gap + 1
  17.     for i = first to last do
  18.         tempi = x[i]
  19.         j = i - gap 
  20.         while 1 do
  21.         tempj = x[j]
  22.         if compare(tempi, tempj) >= 0 then
  23.             j = j + gap
  24.             exit
  25.         end if
  26.         x[j+gap] = tempj
  27.         if j <= gap then
  28.             exit
  29.         end if
  30.         j = j - gap
  31.         end while
  32.         x[j] = tempi
  33.     end for
  34.     if gap = 1 then
  35.         return x
  36.     else
  37.         gap = floor(gap / 3) + 1 
  38.     end if
  39.     end while
  40. end function
  41.