1 program SortDemo ( Input, Output );
9 ArrayType = array [ 1 .. Max ] of Integer;
14 procedure DisplayArray ( var A : ArrayType );
31 procedure FillArray( var A : ArrayType );
37 A[ I ] := Random( 100 )
40 procedure WriteLT ( Position : Integer;
43 GoToXY( 4 * Position - 2, Level );
46 TextColor( LightGray );
49 procedure WriteBlank ( Position : Integer;
52 GoToXY( 4 * Position - 2, Level );
55 TextColor( LightGray );
58 procedure WriteColor ( I : Integer;
69 TextColor( LightGray )
72 procedure WriteNormal ( I : Integer;
78 TextColor( LightGray );
84 procedure MergeSort ( var A : ArrayType );
89 procedure Transfer( var F, T : ArrayType;
96 for I := FromFirst to FromLast do
97 T[ ToFirst + ( I - FromFirst ) ] := F[ I ];
100 procedure Merge ( var A : ArrayType;
115 MidPoint := ( First + Last ) div 2;
117 Right := Midpoint + 1;
119 {V} for I := First to Midpoint do
120 {V} WriteColor( I, A[ I ], LightRed, 5 );
121 {V} for I := Right to Last do
122 {V} WriteColor( I, A[ I ], LightBlue, 5 );
125 {V} for I := First to Last do
126 {V} WriteBlank( I, 5 );
127 {V} for I := First to Midpoint do
128 {V} WriteColor( I, A[ I ], LightRed, 10 );
129 {V} for I := Right to Last do
130 {V} WriteColor( I, A[ I ], LightBlue, 11 );
133 while ( Left <= Midpoint ) and ( Right <= Last ) do
135 if A[ Left ] < A[ Right ] then
137 Temp[ Count ] := A[ Left ];
139 {V} WriteColor( Count, A[ Left ], LightRed, 5 );
140 {V} WriteBlank( Left, 10 );
147 Temp[ Count ] := A[ Right ];
149 {V} WriteColor( Count, A[ Right ], LightBlue, 5 );
150 {V} WriteBlank( Right, 11 );
158 if ( Left <= MidPoint ) then
160 Transfer( A, Temp, Left, Midpoint, Count );
161 {V} for I := Left to Midpoint do
163 {V} WriteColor( Count, A[ I ], LightRed, 5 );
164 {V} WriteBlank( I, 10 );
172 Transfer( A, Temp, Right, Last, Count );
173 {V} for I := Right to Last do
175 {V} WriteColor( Count, A[ I ], LightBlue, 5 );
176 {V} WriteBlank( I, 11 );
182 Transfer( Temp, A, First, Last, First );
187 procedure MSort ( var A : ArrayType;
197 MidPoint := ( First + Last ) div 2;
198 MSort( A, First, MidPoint );
200 {V} for I := First to MidPoint do
201 {V} WriteLT( I, Level );
204 MSort( A, MidPoint + 1, Last );
206 {V} for I := MidPoint + 1 to Last do
207 {V} WriteLT( I, Level );
210 Merge( A, First, Last );
212 {V} for I := MidPoint + 1 to Last do
214 {V} WriteBlank( I, Level );
215 {V} WriteBlank( I, Level - 1 );
216 {V} WriteLT( I, Level - 2 );
220 {V} for I := First to Last do
221 {V} WriteNormal( I, A[ I ] );
231 {V} for I := 1 to Max do
232 {V} WriteLT( I, Level );