Sorting algorithms/Gnome sort: Difference between revisions

Content added Content deleted
Line 637: Line 637:


sort (ar: ARRAY [G]): ARRAY [G]
sort (ar: ARRAY [G]): ARRAY [G]
-- sort array ar with gnome sort
-- Sort array in ascending order.
require
require
array_not_void: ar /= VOID
array_not_void: ar /= VOID
Line 667: Line 667:
end
end
ensure
ensure
same_length: ar.count = Result.count
Same_length: ar.count = Result.count
Result_is_sorted: is_sorted (Result)
Result_is_sorted: is_sorted (Result)
end
end
Line 676: Line 676:
--- Is 'ar' sorted in ascending order?
--- Is 'ar' sorted in ascending order?
require
require
ar_not_empty: ar.is_empty = FALSE
ar_not_empty: ar.is_empty = False
local
local
i: INTEGER
i: INTEGER
do
do
Result := TRUE
Result := True
from
from
i := ar.lower
i := ar.lower
Line 687: Line 687:
loop
loop
if ar [i] > ar [i + 1] then
if ar [i] > ar [i + 1] then
RESULT := FALSE
Result := False
end
end
i := i + 1
i := i + 1