deferred class interface ABSTRACT_SORTER[X]
--
-- Some algorithms to sort any COLLECTION, using a given order relation
-- in deferred methods lt, gt, lte and gte.
--
-- 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). Note that
-- "small" means "a is smaller than b" when "lt (a, b)", no matter what
-- lt is.
--
--
-- Some algorithms to sort any COLLECTION[COMPARABLE].
--
-- Elements are sorted using the order defined by lt.
--
feature(s) from ABSTRACT_SORTER
-- Auxiliary functions
lt (x, y: X): BOOLEAN
gt (x, y: X): BOOLEAN
lte (x, y: X): BOOLEAN
gte (x, y: X): BOOLEAN
feature(s) from ABSTRACT_SORTER
is_sorted (c: COLLECTION[X]): BOOLEAN
-- Is c already sorted ?
-- Uses lte 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
not c.is_empty implies c.valid_index(Result);
c.has(element) implies c.item(Result).is_equal(element)
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)
insert_index (c: COLLECTION[X]; element: X): INTEGER
-- retrieve the upper index for wich gt
require
c /= Void;
is_sorted(c)
ensure
c.valid_index(Result) implies gt(c.item(Result),element);
c.valid_index(Result - 1) implies lte(c.item(Result - 1),element);
Result.in_range(c.lower,c.upper + 1)
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)
feature(s) from ABSTRACT_SORTER
von_neuman_line (src, dest: COLLECTION[X]; count, d_count, lower, imax: INTEGER)
require
src /= dest;
src.lower = dest.lower;
src.upper = dest.upper;
count >= 1;
d_count = count * 2;
lower = src.lower;
imax = src.upper + 1
ensure
d_count >= dest.count implies is_sorted(dest)
von_neuman_inner_sort (src, dest: COLLECTION[X]; sg1, count, imax: INTEGER)
require
src.valid_index(sg1)
heap_repair (c: COLLECTION[X]; c_lower, first, last: INTEGER)
-- Repair the heap from the node number first
-- It considers that the last item of c is number last
-- It supposes that children are heaps.
require
c /= Void;
c.lower = c_lower;
c_lower <= first;
last <= c.upper
quick_sort_region (c: COLLECTION[X]; left, right: INTEGER)
end of deferred ABSTRACT_SORTER[X]