-program SortDemo ( Input, Output );\r
-uses\r
- Crt;\r
-\r
-const\r
- Max = 12;\r
-\r
-type\r
- ArrayType = array [ 1 .. Max ] of Integer;\r
-\r
-var\r
- A : ArrayType;\r
-\r
- procedure DisplayArray ( var A : ArrayType );\r
- var\r
- I : Integer;\r
- begin\r
- ClrScr;\r
- GotoXY( 1, 5 );\r
- Write( '(' );\r
- for I := 1 to Max do\r
- begin\r
- Write( A[ I ] : 4 );\r
- if I <> Max then\r
- Write( ',' )\r
- else\r
- Write( ')' )\r
- end\r
- end;\r
-\r
- procedure FillArray( var A : ArrayType );\r
- var\r
- I : Integer;\r
- begin\r
- Randomize;\r
- for I := 1 to Max do\r
- A[ I ] := Random( 100 )\r
- end;\r
-\r
-\r
- procedure WriteColor ( I : Integer;\r
- Value : Integer;\r
- Color : Integer );\r
- var\r
- X : Integer;\r
- begin\r
- X := 5 * I - 3;\r
- GoToXY( X, 5 );\r
- TextColor( Color );\r
- Write( Value : 4 );\r
- TextColor( LightGray )\r
- end;\r
-\r
- procedure WriteChColor ( I, J : Integer );\r
- var\r
- X : Integer;\r
- begin\r
- X := 5 * I - 1;\r
- TextColor( White );\r
- GotoXY( X, 7 );\r
- Write( 'Lo' );\r
- X := 5 * J - 1;\r
- GoToXY( X, 7 );\r
- Write( 'Hi' );\r
- end;\r
-\r
-\r
- procedure WriteNormal ( I : Integer;\r
- Value : Integer );\r
- var\r
- X : Integer;\r
- begin\r
- X := 5 * I - 3;\r
- TextColor( LightGray );\r
- GoToXY( X, 5 );\r
- Write( Value : 4 )\r
- end;\r
-\r
- procedure SetDisplay ( Pivot, Lo, Hi : Integer );\r
- var\r
- Ch : Char;\r
- begin\r
- GoToXY( 1, 9 );\r
- TextColor( Green );\r
- Write( 'Pivot Value = ', Pivot : 3 );\r
- TextColor( LightRed );\r
- Write( ' Lo Index = ', Lo : 3 );\r
- TextColor( LightBlue );\r
- Write( ' Hi Index = ', Hi : 3 );\r
- WriteChColor( Lo, Hi );\r
- Ch := ReadKey;\r
- GoToXY( 1, 9 );\r
- ClrEol;\r
- GoToXY( 1, 7 );\r
- Write(' ');\r
- GoToXY( 1, 8 );\r
- Write(' ');\r
- GoToXY( 1, 9 );\r
- Write(' ');\r
- TextColor( LightGray );\r
- end;\r
-\r
- procedure QuickSort ( var A : ArrayType;\r
- Lower,\r
- Upper : Integer );\r
-\r
- var\r
- PivotPoint : Integer;\r
- Ch : Char;\r
- I : Integer;\r
-\r
- PPos : Integer;\r
-\r
- Procedure Partition ( var A : ArrayType;\r
- Lo,\r
- Hi : Integer;\r
- var PivotPoint : Integer );\r
- var\r
- Pivot : Integer;\r
- begin\r
- Pivot := A[ Lo ];\r
- PPos := Lo;\r
- WriteColor( PPos, Pivot, Cyan + Black + Blink );\r
- SetDisplay( Pivot, Lo, Hi );\r
- while Lo < Hi do\r
- begin\r
- while ( Pivot < A[ Hi ] ) and ( Lo < Hi ) do\r
- begin\r
- Hi := Hi - 1;\r
- SetDisplay( Pivot, Lo, Hi );\r
- end;\r
- if Hi <> Lo then\r
- begin\r
- WriteColor( Lo, A[ Hi ], LightRed );\r
- A[ Lo ] := A[ Hi ];\r
- if Lo = PPos then\r
- begin\r
- WriteColor( Hi, Pivot, Cyan + Black + Blink );\r
- PPos := Hi;\r
- end;\r
- Lo := Lo + 1;\r
- SetDisplay( Pivot, Lo, Hi );\r
- end;\r
-\r
- while ( Pivot > A[ Lo ] ) and ( Lo < Hi ) do\r
- begin\r
- Lo := Lo + 1;\r
- SetDisplay( Pivot, Lo, Hi );\r
- end;\r
- if Hi <> Lo then\r
- begin\r
- WriteColor( Hi, A[ Lo ], LightBlue );\r
- A[ Hi ] := A[ Lo ];\r
- if Hi = PPos then\r
- begin\r
- WriteColor( Lo, Pivot, Cyan + Black + Blink );\r
- PPos := Lo;\r
- end;\r
- Hi := Hi - 1;\r
- SetDisplay( Pivot, Lo, Hi );\r
- end;\r
-\r
- end;\r
- WriteColor( Hi, Pivot, Yellow );\r
- Ch := ReadKey;\r
- A[ Hi ] := Pivot;\r
- PivotPoint := Hi\r
- end;\r
-\r
- begin\r
- Partition( A, Lower, Upper, PivotPoint );\r
- for I := Lower to Upper do\r
- if I <> PivotPoint then\r
- WriteNormal( I, A[ I ] );\r
- if Lower < PivotPoint then\r
- QuickSort( A, Lower, PivotPoint - 1 );\r
- if Upper > PivotPoint then\r
- QuickSort( A, PivotPoint + 1, Upper )\r
- end;\r
-\r
-begin\r
- FillArray( A );\r
- DisplayArray( A );\r
- QuickSort( A, 1, Max );\r
- ClrScr\r
-end.\r
-\r
+program SortDemo ( Input, Output );
+uses
+ Crt;
+
+const
+ Max = 12;
+
+type
+ ArrayType = array [ 1 .. Max ] of Integer;
+
+var
+ A : ArrayType;
+
+ procedure DisplayArray ( var A : ArrayType );
+ var
+ I : Integer;
+ begin
+ ClrScr;
+ GotoXY( 1, 5 );
+ Write( '(' );
+ for I := 1 to Max do
+ begin
+ Write( A[ I ] : 4 );
+ if I <> Max then
+ Write( ',' )
+ else
+ Write( ')' )
+ end
+ end;
+
+ procedure FillArray( var A : ArrayType );
+ var
+ I : Integer;
+ begin
+ Randomize;
+ for I := 1 to Max do
+ A[ I ] := Random( 100 )
+ end;
+
+
+ procedure WriteColor ( I : Integer;
+ Value : Integer;
+ Color : Integer );
+ var
+ X : Integer;
+ begin
+ X := 5 * I - 3;
+ GoToXY( X, 5 );
+ TextColor( Color );
+ Write( Value : 4 );
+ TextColor( LightGray )
+ end;
+
+ procedure WriteChColor ( I, J : Integer );
+ var
+ X : Integer;
+ begin
+ X := 5 * I - 1;
+ TextColor( White );
+ GotoXY( X, 7 );
+ Write( 'Lo' );
+ X := 5 * J - 1;
+ GoToXY( X, 7 );
+ Write( 'Hi' );
+ end;
+
+
+ procedure WriteNormal ( I : Integer;
+ Value : Integer );
+ var
+ X : Integer;
+ begin
+ X := 5 * I - 3;
+ TextColor( LightGray );
+ GoToXY( X, 5 );
+ Write( Value : 4 )
+ end;
+
+ procedure SetDisplay ( Pivot, Lo, Hi : Integer );
+ var
+ Ch : Char;
+ begin
+ GoToXY( 1, 9 );
+ TextColor( Green );
+ Write( 'Pivot Value = ', Pivot : 3 );
+ TextColor( LightRed );
+ Write( ' Lo Index = ', Lo : 3 );
+ TextColor( LightBlue );
+ Write( ' Hi Index = ', Hi : 3 );
+ WriteChColor( Lo, Hi );
+ Ch := ReadKey;
+ GoToXY( 1, 9 );
+ ClrEol;
+ GoToXY( 1, 7 );
+ Write(' ');
+ GoToXY( 1, 8 );
+ Write(' ');
+ GoToXY( 1, 9 );
+ Write(' ');
+ TextColor( LightGray );
+ end;
+
+ procedure QuickSort ( var A : ArrayType;
+ Lower,
+ Upper : Integer );
+
+ var
+ PivotPoint : Integer;
+ Ch : Char;
+ I : Integer;
+
+ PPos : Integer;
+
+ Procedure Partition ( var A : ArrayType;
+ Lo,
+ Hi : Integer;
+ var PivotPoint : Integer );
+ var
+ Pivot : Integer;
+ begin
+ Pivot := A[ Lo ];
+ PPos := Lo;
+ WriteColor( PPos, Pivot, Cyan + Black + Blink );
+ SetDisplay( Pivot, Lo, Hi );
+ while Lo < Hi do
+ begin
+ while ( Pivot < A[ Hi ] ) and ( Lo < Hi ) do
+ begin
+ Hi := Hi - 1;
+ SetDisplay( Pivot, Lo, Hi );
+ end;
+ if Hi <> Lo then
+ begin
+ WriteColor( Lo, A[ Hi ], LightRed );
+ A[ Lo ] := A[ Hi ];
+ if Lo = PPos then
+ begin
+ WriteColor( Hi, Pivot, Cyan + Black + Blink );
+ PPos := Hi;
+ end;
+ Lo := Lo + 1;
+ SetDisplay( Pivot, Lo, Hi );
+ end;
+
+ while ( Pivot > A[ Lo ] ) and ( Lo < Hi ) do
+ begin
+ Lo := Lo + 1;
+ SetDisplay( Pivot, Lo, Hi );
+ end;
+ if Hi <> Lo then
+ begin
+ WriteColor( Hi, A[ Lo ], LightBlue );
+ A[ Hi ] := A[ Lo ];
+ if Hi = PPos then
+ begin
+ WriteColor( Lo, Pivot, Cyan + Black + Blink );
+ PPos := Lo;
+ end;
+ Hi := Hi - 1;
+ SetDisplay( Pivot, Lo, Hi );
+ end;
+
+ end;
+ WriteColor( Hi, Pivot, Yellow );
+ Ch := ReadKey;
+ A[ Hi ] := Pivot;
+ PivotPoint := Hi
+ end;
+
+ begin
+ Partition( A, Lower, Upper, PivotPoint );
+ for I := Lower to Upper do
+ if I <> PivotPoint then
+ WriteNormal( I, A[ I ] );
+ if Lower < PivotPoint then
+ QuickSort( A, Lower, PivotPoint - 1 );
+ if Upper > PivotPoint then
+ QuickSort( A, PivotPoint + 1, Upper )
+ end;
+
+begin
+ FillArray( A );
+ DisplayArray( A );
+ QuickSort( A, 1, Max );
+ ClrScr
+end.
+