Sortowanie Przez Scalanie
const
   pocz=0;
   kon=255;

var
   tab,ptab:array[pocz..kon]of longint;
   i:longint;

procedure MergeSort(var tab,ptab:array of longint;left,right:longint);
var m,i,i1,i2,i3:longint;
begin
   if left<right then begin
      m:=(left+right) div 2;
      MergeSort(tab,ptab,left,m);
      MergeSort(tab,ptab,m+1,right);
      i1:=left;
      i2:=m+1;
      i3:=0;
      while (i1<=m)and(i2<=right) do
      if tab[i1]<tab[i2] then begin
         ptab[i3]:=tab[i1];
         inc(i3);
         inc(i1);
      end else begin
         ptab[i3]:=tab[i2];
         inc(i3);
         inc(i2);
      end;
      while i1<=m do begin
         ptab[i3]:=tab[i1];
         inc(i3);
         inc(i1);
      end;
      while i2<=right do begin
         ptab[i3]:=tab[i2];
         inc(i3);
         inc(i2);
      end;
      i3:=0;
      for i:=left to right do begin
         tab[i]:=ptab[i3];
         inc(i3);
      end;
   end;
end;

BEGIN
   randomize;
   writeln('Zawartosc przed sortowaniem');
   for i:=pocz to kon do begin
      tab[i]:=random(256);
      write(tab[i],' ');
   end;
   writeln;
   MergeSort(tab,ptab,pocz,kon);
   Writeln('Zawartosc tablicy po posortowaniu');
   for i:=pocz to kon do write(tab[i],' ');
   writeln;
   writeln('Nacisnij Enter zeby kontynuowac');
   readln;
END.

Podobne strony

Podobne Strony
Add a New Comment
Table of Contents
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License