X-Git-Url: https://git.llucax.com/z.facultad/75.40/1er-cuat/orden.git/blobdiff_plain/01ce990c404c3050fa816bf88c0dae98360536cb..refs/heads/master:/test/qsdemo.pas diff --git a/test/qsdemo.pas b/test/qsdemo.pas index f33b5c3..aeee128 100644 --- a/test/qsdemo.pas +++ b/test/qsdemo.pas @@ -1,187 +1,187 @@ -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. - +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. +