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 ] : 4 );
\r
31 procedure FillArray( var A : ArrayType );
\r
36 for I := 1 to Max do
\r
37 A[ I ] := Random( 100 )
\r
41 procedure WriteColor ( I : Integer;
\r
51 TextColor( LightGray )
\r
54 procedure WriteChColor ( I, J : Integer );
\r
68 procedure WriteNormal ( I : Integer;
\r
74 TextColor( LightGray );
\r
79 procedure SetDisplay ( Pivot, Lo, Hi : Integer );
\r
85 Write( 'Pivot Value = ', Pivot : 3 );
\r
86 TextColor( LightRed );
\r
87 Write( ' Lo Index = ', Lo : 3 );
\r
88 TextColor( LightBlue );
\r
89 Write( ' Hi Index = ', Hi : 3 );
\r
90 WriteChColor( Lo, Hi );
\r
100 TextColor( LightGray );
\r
103 procedure QuickSort ( var A : ArrayType;
\r
108 PivotPoint : Integer;
\r
114 Procedure Partition ( var A : ArrayType;
\r
117 var PivotPoint : Integer );
\r
123 WriteColor( PPos, Pivot, Cyan + Black + Blink );
\r
124 SetDisplay( Pivot, Lo, Hi );
\r
127 while ( Pivot < A[ Hi ] ) and ( Lo < Hi ) do
\r
130 SetDisplay( Pivot, Lo, Hi );
\r
134 WriteColor( Lo, A[ Hi ], LightRed );
\r
135 A[ Lo ] := A[ Hi ];
\r
138 WriteColor( Hi, Pivot, Cyan + Black + Blink );
\r
142 SetDisplay( Pivot, Lo, Hi );
\r
145 while ( Pivot > A[ Lo ] ) and ( Lo < Hi ) do
\r
148 SetDisplay( Pivot, Lo, Hi );
\r
152 WriteColor( Hi, A[ Lo ], LightBlue );
\r
153 A[ Hi ] := A[ Lo ];
\r
156 WriteColor( Lo, Pivot, Cyan + Black + Blink );
\r
160 SetDisplay( Pivot, Lo, Hi );
\r
164 WriteColor( Hi, Pivot, Yellow );
\r
171 Partition( A, Lower, Upper, PivotPoint );
\r
172 for I := Lower to Upper do
\r
173 if I <> PivotPoint then
\r
174 WriteNormal( I, A[ I ] );
\r
175 if Lower < PivotPoint then
\r
176 QuickSort( A, Lower, PivotPoint - 1 );
\r
177 if Upper > PivotPoint then
\r
178 QuickSort( A, PivotPoint + 1, Upper )
\r
184 QuickSort( A, 1, Max );
\r