Up Top       Prev COLLECTION3      Next COMPARABLE



expanded class interface COLLECTION_SORTER[X->COMPARABLE]
   --
   -- Some algorithms to sort any COLLECTION[COMPARABLE].
   --
   -- Elements are sorted using increasing order: small elements
   -- at the beginning of the collection, large at the end (decreasing
   -- order is implemented by class REVERSE_COLLECTION_SORTER).
   --
   --
   -- How to use this expanded class :
   --
   --          local
   --             sorter: COLLECTION_SORTER[INTEGER]
   --             array: ARRAY[INTEGER]
   --          do
   --             array := <<1,3,2>>
   --             sorter.sort(array)
   --             check
   --                sorter.is_sorted(array)
   --             end
   --             ...
   --

feature(s) from COLLECTION_SORTER   is_sorted (c: COLLECTION[X]): BOOLEAN
      -- Is c already sorted ?
      -- Uses infix "<=" for comparison.

      require
         c /= Void
      ensure
         c.is_equal(old c.twin)

   has (c: COLLECTION[X]; element: X): BOOLEAN
      require
         c /= Void;
         is_sorted(c)
      ensure
         Result = c.has(element)

   index_of (c: COLLECTION[X]; element: X): INTEGER
      require
         c /= Void;
         is_sorted(c)
      ensure
         c.has(element) implies c.item(Result).is_equal(element);
         c.valid_index(Result - 1) implies not (element < c.item(Result - 1));
         not c.has(element) and c.valid_index(Result) implies element < c.item(Result)

   add (c: COLLECTION[X]; element: X)
      -- Add element in collection c keeping the sorted property.

      require
         c /= Void;
         is_sorted(c)
      ensure
         c.count = old c.count + 1;
         is_sorted(c)

   sort (c: COLLECTION[X])
      -- Sort c using the default most efficient sorting algorithm
      -- already implemented.

      require
         c /= Void
      ensure
         c.count = old c.count;
         is_sorted(c)

   quick_sort (c: COLLECTION[X])
      -- Sort c using the quick sort algorithm.

      require
         c /= Void
      ensure
         c.count = old c.count;
         is_sorted(c)

   von_neuman_sort (c: COLLECTION[X])
      -- Sort c using the Von Neuman algorithm.
      -- This algorithm needs to create a second collection.
      -- Uses infix "<=" for comparison.

      require
         c /= Void
      ensure
         c.count = old c.count;
         is_sorted(c)

   heap_sort (c: COLLECTION[X])
      -- Sort c using the heap sort algorithm.

      require
         c /= Void
      ensure
         c.count = old c.count;
         is_sorted(c)

   bubble_sort (c: COLLECTION[X])
      -- Sort c using the bubble sort algorithm.
      -- Uses infix "<" for comparison.

      require
         c /= Void
      ensure
         c.count = old c.count;
         is_sorted(c)



end of expanded COLLECTION_SORTER[X->COMPARABLE]




All classes inherit from ANY, ANY inherits from PLATFORM and PLATFORM inherits from GENERAL.



Generated by short -html_deb on 31 March 2005.