niepok

bubblesort

Jan 25th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.73 KB | None | 0 0
  1. program bubblesort;
  2. uses crt, DOS;
  3. const N=50000;
  4. type
  5.     tab1 = array [1..N] of integer;
  6. var
  7.    t1,t2,t3 : tab1;
  8.    hour,minute,second,sec100,hour1,minute1,second1,sec1001 : Word;
  9. procedure Fill();
  10.           var
  11.              i : integer;
  12.           begin
  13.                randomize;
  14.                for i:=1 to N do t1[i]:=random(75000);
  15.           end;
  16. procedure Copy();
  17.           var
  18.              i : integer;
  19.           begin
  20.                for i:=1 to N do
  21.                    begin
  22.                         t2[i]:=t1[i];
  23.                         t3[i]:=t1[i];
  24.                    end;
  25.           end;
  26. procedure Print(t1 : tab1);
  27.           var
  28.              i : integer;
  29.           begin
  30.                for i:=1 to N do write(t1[i]:3,'|');
  31.                writeln;
  32.           end;
  33. procedure Czas();
  34.           begin
  35.                write('Czas wykonania sortowania: ');
  36.                write(sec1001+second1*100+minute1*6000+hour*360000-hour*360000-minute*6000-second*100-sec100);
  37.                writeln(' setnych sekundy');
  38.           end;
  39. procedure Sort();     //zwykle
  40.           var
  41.              i,j,x : integer;
  42.              licznik : integer=0;
  43.           begin
  44.                gettime(hour,minute,second,sec100);
  45.                for j:=1 to N-1 do
  46.                    for i:=1 to N-1 do
  47.                        if t1[i]>t1[i+1] then
  48.                           begin
  49.                                x:=t1[i]; t1[i]:=t1[i+1]; t1[i+1]:=x;
  50.                                //Print(t1);
  51.                                //licznik+=1;
  52.                           end;
  53.                gettime(hour1,minute1,second1,sec1001);
  54.                Czas();
  55.                //writeln('Posorotwano w ',licznik,' operacjach');
  56.           end;
  57. procedure Sort_2();   //jednostronne pominiÄ™cie pustych operacji
  58.           var
  59.              i,j,x,licznik,p: integer;
  60.           begin
  61.                licznik:=0;
  62.                j:=N-1;
  63.                gettime(hour,minute,second,sec100);
  64.                repeat
  65.                       for i:=1 to j do
  66.                           if t2[i]>t2[i+1] then
  67.                           begin
  68.                                x:=t2[i]; t2[i]:=t2[i+1]; t2[i+1]:=x;
  69.                                p:=1;
  70.                                //Print(t2);
  71.                                //licznik+=1;
  72.                           end;
  73.                           if p=0 then j:=0
  74.                           else dec(j);
  75.                until j<1;
  76.                gettime(hour1,minute1,second1,sec1001);
  77.                Czas();
  78.                //writeln('Posorotwano w ',licznik,' operacjach');
  79.           end;
  80. procedure Sort_3();   //dwustronne pominiÄ™cie pustych operacji
  81.           var
  82.              i,j,x,licznik,p,pmin,pmax: integer;
  83.           begin
  84.                licznik:=0;
  85.                pmin:=1;
  86.                pmax:=N-1;
  87.                gettime(hour,minute,second,sec100);
  88.                repeat
  89.                       p:=0;
  90.                       for i:=pmin to pmax do
  91.                           if t3[i]>t3[i+1] then
  92.                           begin
  93.                                x:=t3[i]; t3[i]:=t3[i+1]; t3[i+1]:=x;
  94.                                if p=0 then pmin:=i;
  95.                                p:=i;
  96.                                //Print(t2);
  97.                                //licznik+=1;
  98.                           end;
  99.                           if pmin>1 then dec(pmin);
  100.                           pmax:=p-1;
  101.                until p=0;
  102.                gettime(hour1,minute1,second1,sec1001);
  103.                Czas();
  104.                //writeln('Posorotwano w ',licznik,' operacjach');
  105.           end;
  106. begin
  107.   Fill();
  108.   Copy();
  109.   //Print(t1);
  110.   readln;
  111.   Sort();
  112.   //readln;
  113.   Sort_2();
  114.   //readln;
  115.   Sort_3();
  116.   readln;
  117.   readln;
  118. end.
Advertisement
Add Comment
Please, Sign In to add comment