Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 2.68 KB | None | 0 0
  1. class
  2.     STRING_SORTER
  3.  
  4. feature
  5.     sort(string_for_sorting : STRING; substring_start, substring_end : INTEGER) is
  6.         do
  7.             word := string_for_sorting
  8.             quicksort(substring_start, substring_end)
  9.         end
  10.  
  11.  
  12. feature {NONE}
  13.  
  14.     word : STRING
  15.     left_pointer, right_pointer : INTEGER    
  16.  
  17.     quicksort(substring_start, substring_end : INTEGER) is
  18.         -- quicksort the substring of the string delimited by
  19.         -- the indicies ssubstring_start and substring_end
  20.         local
  21.             i, j : INTEGER
  22.             pivot : CHARACTER
  23.         do
  24.             if substring_start < substring_end then
  25.                 pivot := word.item((substring_start + substring_end) // 2) -- arbitrary pivot
  26.                 partition(substring_start, substring_end, pivot)
  27.                 i := left_pointer
  28.                 j := right_pointer
  29.  
  30.                 -- recursively sort the partitions
  31.                 quicksort(substring_start, j)
  32.                 quicksort(i, substring_end)
  33.             end
  34.  
  35.         end -- quicksort
  36.  
  37.     partition(lower_bound, upper_bound : INTEGER; pivot : CHARACTER) is
  38.         -- Jiggle the string around until all
  39.         -- characters between lower_bound and upper_bound lower than p are less than
  40.         -- all characters between lower_bound and upper_bound greater than p
  41.         do
  42.             from
  43.                 left_pointer := lower_bound
  44.                 right_pointer := upper_bound
  45.             until
  46.                 left_pointer > right_pointer
  47.             loop    
  48.                 left_scan(pivot)
  49.                 right_scan(pivot)
  50.                 if left_pointer <= right_pointer then
  51.                     word.swap(left_pointer, right_pointer)
  52.                     left_pointer := left_pointer + 1
  53.                     right_pointer := right_pointer - 1
  54.                 end
  55.             end
  56.         end -- partition
  57.  
  58.     left_scan(pivot : CHARACTER) is
  59.         -- Increment the left pointer until
  60.         -- it points to a character in a position before
  61.         -- or equal to that of pivot
  62.         do
  63.             from
  64.             until
  65.                 word.item(left_pointer) >= pivot
  66.             loop
  67.                 left_pointer := left_pointer + 1
  68.             end
  69.         end -- left_scan
  70.  
  71.     right_scan(pivot : CHARACTER) is
  72.         -- Decrement the right pointer until
  73.         -- it points to a character in a position before
  74.         -- or equal to the pivot, p
  75.         do
  76.             from
  77.             until
  78.                 word.item(right_pointer) <= pivot
  79.             loop
  80.                 right_pointer := right_pointer - 1
  81.             end
  82.         end -- right_scan
  83.  
  84.  
  85. end -- class STRING_SORTER
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement