uses crt; Procedure GetUserInput; Const kbEsc = #27; kbSpace = #32; Var ch: Char; Begin Repeat ch := ReadKey; If ch = kbEsc then begin GotoXY(1, 24); WriteLn('breaking program'); Halt(100) end; While KeyPressed Do ReadKey Until ch = kbSpace End; Const n = 8; a: Array[1 .. n] Of Integer = (44, 55, 12, 42, 94, 18, 6, 67); Type Index = 0..n; Procedure PrintArray(Const s: String); Var i: Index; Begin TextColor(LightGray); For i := 1 To n Do Write(a[i]:4); WriteLn(s:25); GetUserInput End; (* Global variables *) Var x: integer; cnt_comp, cnt_swap: integer; ToDo: boolean; Type TOperation = (opSelect, opCompare, opMove); Const strAction: array[TOperation] of string = ( 'current selection', 'comparison', 'moving' ); Type TMask = (mskHidden, mskNormal, mskRed, mskGreen); Const OutputColor: Array[TMask] Of Integer = (Black, LightGray, LightRed, LightGreen); Var show_mask: Array[1 .. 2, 1 .. n] Of TMask; show_values: Array[1 .. 2, 1 .. n] Of Integer; Procedure ShowByMask(Const s: String; ival: Integer); Procedure Scroll(x: Byte); Var i: Byte; Begin For i := 1 To x Do WriteLn End; Const scrollBy = 4; Var PosY: Integer; m, k: Index; Begin PosY := WhereY; If PosY > 22 Then Begin Scroll(scrollBy); Dec(PosY, scrollBy - 2); GotoXY(1, PosY); End; Writeln('i = ', ival); PosY := WhereY; For m := 1 to 2 do For k := 1 to n do Begin GotoXY(Pred(k)*4 + 1, Pred(PosY + m)); TextColor( OutputColor[show_mask[m, k]] ); Write(show_values[m, k]:4) End; TextColor(LightGray); Gotoxy(n*4 + 1, Pred(WhereY)); Write(s:25); GotoXY(79, Succ(WhereY)); WriteLn; GetUserInput End; Procedure ShowStep(op: TOperation; i, j: Integer); Var m, k: Index; Begin Case op of opSelect: For k := 1 To n Do Begin If k = i Then Begin show_mask[1, k] := mskHidden; show_mask[2, k] := mskNormal End Else Begin show_mask[1, k] := mskNormal; show_mask[2, k] := mskHidden End; show_values[1, k] := a[k]; show_values[2, k] := a[k] End; opCompare: Begin Inc(cnt_comp); For k := 1 to n do Begin If i = k then If x < a[j] Then show_mask[2, k] := mskRed Else show_mask[2, k] := mskGreen; If j = k Then If x < a[j] Then show_mask[1, k] := mskRed Else show_mask[1, k] := mskGreen; show_values[1, k] := a[k]; show_values[2, k] := a[k] End; For k := 1 To n Do If k <> i Then show_mask[2, k] := mskHidden; show_mask[1, Succ(j)] := mskHidden; show_values[2, i] := x; End; opMove: Begin Inc(cnt_swap); For k := 1 to n do Begin show_mask[1, k] := mskNormal; show_mask[2, k] := mskHidden; show_values[1, k] := a[k]; End; show_mask[1, Succ(j)] := mskHidden; show_mask[2, i] := mskNormal; show_values[2, i] := x; End End; ShowByMask(strAction[op], i) End; Function isLess(index_i, index_j, i, j: Integer): Boolean; Begin isLess := (i < j); If ToDo Then ShowStep( opSelect, index_i, index_j ); If index_j > 0 Then ShowStep( opCompare, index_i, index_j ) End; (* main sorting procedure ... *) Procedure Insert; Var i, j : Index; Begin cnt_comp := 0; cnt_swap := 0; For i := 1 To n do Begin x := a[i]; ToDo := True; j := Pred(i); While isLess(i, j, x, a[j]) Do Begin ToDo := False; a[Succ(j)] := a[j]; Dec(j); ShowStep(opMove, i, j) End; a[Succ(j)] := x; (* j:= i-1; while x < a[j] do begin a[j+1]:= a[j]; j:= j-1; end; a[j+1]:= x; *) WriteLn( 'i = ', i ); PrintArray('insertion') End; End; Begin ClrScr; PrintArray( 'Initial array' ); Insert; WriteLn( 'Total comparisons: ', cnt_comp ); WriteLn( 'Total movings: ', cnt_swap ); ReadLn End.