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