home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Shareware - Software Farm 2
/
wosw_2.zip
/
wosw_2
/
PASCAL
/
SORTDEMO.ZIP
/
SDSORT01.INC
< prev
next >
Wrap
Text File
|
1992-04-15
|
2KB
|
62 lines
(*
╔═══════════════════════════════════════════════════════════════════════════╗
║ Turbo Pascal 6.0 Include File : SDSORT01.INC ║
╟───────────────────────────────────────────────────────────────────────────╢
║ Program : SORTDEMO.PAS ║
╟───────────────────────────────────────────────────────────────────────────╢
║ Version : 1.0 ║
╟───────────────────────────────────────────────────────────────────────────╢
║ Copyright (c) 1992 by Jon S. Russell ║
╟───────────────────────────────────────────────────────────────────────────╢
║ Bubble sort routine for SORTDEMO.PAS ║
╚═══════════════════════════════════════════════════════════════════════════╝
*)
procedure BubbleSort (var Info : InfoType);
var
Current : IndexType;
(*───────────────────────────────────────────────────────────────────────*)
procedure BubbleUp (var Info : InfoType;
StartIndex : IndexType;
EndIndex : IndexType);
var
Index : IndexType;
begin (* BubbleUp *)
(* Loop Invariant: StartIndex <= Index <= EndIndex AND *)
(* List[Index+1] .. List[EndIndex] >= List[Index]. *)
for Index := EndIndex downto StartIndex+1 do
if (Info.List[Index].Key < Info.List[Index-1].Key)
then Swap(Info,Index,Index-1);
end; (* BubbleUp *)
(*───────────────────────────────────────────────────────────────────────*)
begin (* BubbleSort *)
Current := 1; (* index of first unsorted element *)
(* Loop Invariant: 1 <= Current <= Info.Len AND *)
(* the values in List[1] .. List[Current-1] are *)
(* sorted and are less than or equal to the values *)
(* in List[Current] .. List[Len]. *)
while Current < Info.Len do
begin
(* Bubble up the smallest element from the unsorted *)
(* part of the list, with intermediate swaps. *)
BubbleUp(Info, Current, Info.Len);
(* Shrink the unsorted part of the list *)
Inc(Current);
end; (* while *)
Info.Sorted := true; (* set flag *)
end; (* BubbleSort *)
(*─────────────────────────────────────────────────────────────────────────*)