package body sorting4 is -- same sub-programs as sorting but different presentation and -- more comments -- Buble sort procedure CommentedBuble(toSort : in out integerList ; -- list of integer elements to sort length : in integer) -- length of this list is indexElement : integer; -- index of current analyzed element in the list modified : boolean; -- true if two elements where swapped temp : integer; -- temporary variable for values exchange begin modified := true; -- in order to make something while modified -- loop because something to do loop modified := false; -- maybe sort is finished for indexElement in 0 .. length-2 -- going thru all elements of the list loop if toSort(indexElement) > toSort(indexElement+1) then -- two element in bad order temp := toSort(indexElement); -- save value toSort(indexElement) := toSort(indexElement+1); -- put good value toSort(indexElement+1) := temp; -- and other good value modified := true; -- sort is not finished end if; end loop; end loop; end CommentedBuble; -- Selection sort procedure CommentedSelection(toSort : in out integerList ; -- list of integer elements to sort length : in integer) is -- length of this list indexElement, j : integer; -- two index for examining elements of the list posCurMin : integer; -- position of minimum value temp : integer; -- temporary variable for values exchange begin for indexElement in 0 .. length-1 -- Going thru all sublists loop posCurMin := indexElement; -- maybe the first element of current sublist is the smallest for j in indexElement+1 .. length-1 -- looking at other elements in sublist loop if toSort(j) < toSort(posCurMin) then -- an element is smaller posCurMin := j; -- memorizing it position end if; end loop; -- ok, now we will exchange positions of sallest element and first element of sublist if indexElement /= posCurMin then -- nothig to do if first element of sublist is the smallest temp := toSort(indexElement); -- save value toSort(indexElement) := toSort(posCurMin); -- exchange toSort(posCurMin) := temp; -- finish exchange end if; end loop; end CommentedSelection; end sorting4;