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