home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / EXAMPLE.EX < prev    next >
Text File  |  1993-05-11  |  1KB  |  44 lines

  1.         -------------------------------------
  2.         -- Example Program from the Manual --
  3.         -------------------------------------
  4.  
  5. sequence list, sorted_list
  6.  
  7. function merge_sort(sequence x)
  8. -- put x into ascending order using a recursive merge_sort
  9.     integer n, mid
  10.     sequence newx, x1, x2
  11.  
  12.     n = length(x)
  13.     if n = 0 or n = 1 then
  14.     return x -- trivial case
  15.     end if
  16.     mid = floor(n/2)
  17.     x1 = merge_sort(x[1..mid])   -- sort first half of x
  18.     x2 = merge_sort(x[mid+1..n]) -- sort second half of x
  19.     
  20.     -- merge the two sorted halves into one
  21.     newx = {}
  22.     while length(x1) > 0 and length(x2) > 0 do
  23.     if compare(x1[1], x2[1]) < 0 then
  24.         newx = newx & x1[1]
  25.         x1 = x1[2..length(x1)]
  26.     else
  27.         newx = newx & x2[1]
  28.         x2 = x2[2..length(x2)]
  29.     end if
  30.     end while
  31.     newx = newx & x1 & x2  -- get leftovers
  32.     return newx
  33. end function
  34.  
  35. procedure print_sorted_list()
  36. -- generate sorted_list from list
  37.     list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2}
  38.     sorted_list = merge_sort(list)
  39.     ? sorted_list   -- print the results
  40. end procedure
  41.  
  42. print_sorted_list()  -- this command starts the program running
  43.  
  44.