Wednesday, July 3, 2013

PROGRAM SORT1;
Uses WinCrt;

CONST   SIZE = 100;                   {maximum size of table}

TYPE
        Table = Array[1..SIZE] of real;

VAR
        A         : Table;               {Table to be sorted}
        MAX_VALUE : real;            {Maximum value of table}
        i,N       : integer;


{****************************************************
* Sorts an array ARR of length N in ascending order *
* by straight insertion.                            *
* ------------------------------------------------- *
* INPUTS:                                           *
*   N  size of table ARR                 *
*          ARR  table to be sorted                *
* OUTPUT:                                           *
*   ARR   table sorted in ascending order    *
*                                                   *
* NOTE: Straight insertion is a N² routine and      *
*       should only be used for relatively small    *
*       arrays (N<100).                             *
****************************************************}        
Procedure PIKSRT(N:INTEGER;VAR ARR:Table);
Label 10;
Var i,j:INTEGER; a:REAL;
Begin
  for j:=2 to N do
  begin
    a:=ARR[j];
    for i:=j-1 downto 1 do
    begin
      if ARR[i]<=a then goto 10;
      ARR[i+1]:=ARR[i]
    end;
    i:=0;
10:  ARR[i+1]:=a
  end
End;

{write table of size N to standard output}
Procedure TWRIT(N:INTEGER;ARR:Table);
Var i:integer;
begin
  writeln;
  for i:=1 to N do
  begin
    write(ARR[i]:6:0);
    if (i MOD 5)=0 then writeln
  end
end;

{main program}
BEGIN
  {initialize random number generator}
  Randomize;

  N:=25;    {initialize size of table}
  MAX_VALUE := 1000.0;

  {generate random table of numbers (between 0 and MAX_VALUE) }
  for i:=1 to N do A[i]:=MAX_VALUE*Random;

  writeln;
  writeln(' Table to be sorted:');
  TWRIT(N,A);

  {call sorting subroutine}
  PIKSRT(N,A);

  writeln;
  writeln(' Sorted table (straight insertion):');
  TWRIT(N,A);

  writeln;
  ReadKey; DoneWinCrt

END.

1 comment:

  1. Casino - Dr. mcd
    Explore our website: www.diamondscasino.com. 강원도 출장안마 Casino in Maryland - Play Slots & 김해 출장샵 Blackjack 용인 출장샵 - 광주 출장샵 DiamondS Casino. 경기도 출장마사지 Diamond Casino Resort is Washington's premier gaming

    ReplyDelete