18-1 Pascal
procedure InsertionSort ( Var Data : SortArray_t ; FirstElmt : Integer
LastElmt : Integer ) ; { use the insertion sort technique to sort the
“Data” array in ascending order. This routine assumes that Data
[ FirstElmt ] is not the FirstElmt element in Data and that Data
[ FirstElmt - 1 ] can be accessed. }Const SortMin = ’’ ; Var SortBoundary :
Integer ; { upper end of sorted range } InsertPos: Integer ; {position
to insert element } InsertVal : SortElmt_t ; {value to insert}
LowerBoundary : SortElmt_t ; {first value below range to sort} begin
{ Replace element at lower boundary with an element guaranteed to be first
in a sorted list } LowerBoundary : = Data[ FirstElmt_t ] ; Data
[FirstElmt-1] : = SortMin ; {The elements in positions FirstElmt through
SortBoundary-1 are always sorted. In each pass through the loop,
SortBoundary is increased, and the element at the position of new
SortBoundary Probably isn’t in its sorted place in the array,so it’s
Inserted into the proper place somewhere between FirstElmt and
SortBoundary.}for SortBoundary : = FirstElmt + 1 to stElmt do begin
InsertVal : = Data[SortBoundary] ; InsertPos : = SortBoundary ;while
InsertVal <= Data[ InsertPos – 1 ] do begin Data[InsertPos]
: = Data[ InsertPos – 1 ] ; InsertPos : = InsertPos – 1 ; end; Data
[ InsertPos ] : = InsertVal ;end;{Replace orginal lower-boundary
element }Data[ FirstElmt – 1 ] : = LowBoundary ; End; { InsertionSort}
,
18 2
18-2 Pascal
procedure InsertionSort( Var Data : SortArray_t ; FirstElmt:Integer ;
LastElmt : Integer ) ;
{ use the insertion sort technique to sort the “Data” array in ascending
order. This routine assumes that Data[ FirstElmt ] is not the
first element in Data and that Data[ FirstElmt – 1 ] can be accessed. }
Const
SortMin = ’’ ;
Var
SortBoundary : Integer ; { upper end of sorted range }
InsertPos : Integer ; {position to insert element }
InsertVal : SortElmt_t ; {value to insert}
LowerBoundary :SortElmt_t ; {first value below range to sort}
begin
{ Replace element at lower boundary with an element
guaranteed to be first in a sorted list }