Сортировка

Формат:

Дата создания: 10.10.2016

Размер: 1.28 KB

Скачать реферат

1. ‹€Ž€’Ž€Ÿ €Ž’€ Ž Žƒ€ŒŒˆŽ‚€ˆž “—…ˆŠ€ 10¤ Š‹€‘‘€ ˜ŠŽ‹› N57 €•Œ€Ž‚€ ‘…ƒ…Ÿ Ž ’…Œ… "‘Ž’ˆŽ‚Šˆ". 2. Ž‘’€Ž‚Š€ ‡€„€—ˆ.    „ ­ ä ©«, ᮤ¥à¦ é¨© ç¨á«  ⨯  longint, à á¯®«®¦¥­­ë¥ ¢ ¯à®¨§¢®«ì­®¬ ¯®à浪¥. ’ॡã¥âáï à á¯®«®¦¨âì í⨠ç¨á«  ¯® ¢®§à áâ ­¨î, ¨á¯®«ì§ãï ­¥ ¡®«¥¥ 40 ª¨«®¡ ©â ®¯¥à â¨¢­®© ¯ ¬ï⨠¨ ¤¨áª®¢®£® ¯à®áâà ­á⢠ ­¥ ¡®«¥¥ 祬 ¢ ¤¢  à §  ¡®«ìè¥ ¨á室­®£® ä ©« . 3. €‹ƒŽˆ’Œ (¬¥â®¤ à¥è¥­¨ï).    ‘­ ç «  ¨á室­ë© ä ©« à §¡¨¢ ¥âáï ­  ªã᪨ ¯® 10000 ç¨á¥«, ª ¦¤ë© ªã᮪ á®àâ¨àã¥âáï ¢ ¯ ¬ï⨠¨ § ¯¨á뢠¥âáï ¢ ®¤¨­ ¨§ ¤¢ãå ¢à¥¬¥­­ëå ä ©«®¢, ¯à¨ç¥¬ â ª, çâ® ª®«¨ç¥á⢮ ªã᪮¢ ¢ íâ¨å ä ©« å ®â«¨ç ¥âáï ­¥ ¡®«¥¥ 祬 ­  1(¤ «¥¥ - ¯¥à¢®­ ç «ì­ ï á®àâ¨à®¢ª ).    ‡ â¥¬, ­¥áª®«ìª® à § ¢ë¯®«­ï¥âáï ®¯¥à æ¨ï "᪫¥¨¢ ­¨¥"(®¤­® ¢ë¯®«­¥­¨¥ ®¯¥à æ¨¨ "᪫¥¨¢ ­¨¥" ¬ë ¡ã¤¥¬ ­¥§ë¢ âì "è £"), â.¥ ¤¢  ¨á室­ëå ä ©« , ¢ ª®â®àëå ­ å®¤¨«¨áì ®âá®àâ¨à®¢ ­­ë¥ ªã᪨ ª®¯¨àãîâáï ¢ ¤¢  ¤à㣨å ä ©« , ¯à¨ í⮬ ¨§ ¤¢ãå ªã᪮¢, ­ å®¤ïé¨åáï ¢ à §­ëå ä ©« å ¨ ¨¬¥îé¨å ®¤¨­ ª®¢ë¥ ­®¬¥à  ᮧ¤ ¥âáï ®¤¨­ ®âá®àâ¨à®¢ ­­ë© ªã᮪. â®â ªã᮪ § ¯¨á뢠¥âáï ¢ ¯¥à¢ë© ¢ë室­®© ä ©« ¥á«¨ ¨á室­ë¥ ªã᪨ ¨¬¥«¨ ­¥ç¥â­ë¥ ­®¬¥à  ¨ ¢® ¢â®à®©, ¥á«¨ ¨á室­ë¥ ªã᪨ ¨¬¥«¨ ç¥â­ë¥ ­®¬¥à . 4. ‚“’…ŸŸ ‘…–ˆ”ˆŠ€–ˆŸ Žƒ€ŒŒ›.    à¨ ­ ¯¨á ­¨¨ ¯à®£à ¬¬ë ¨á¯®«ì§®¢ « áì á।  Borland Pascal 7.0 ¨ ¢áâ஥­­ë© ª®¬¯¨«ïâ®à.    „«ï ã᪮७­®£® ®¡¬¥­  á ¤¨áª®¬ ¯à¨¬¥­ï«áï ¡«®ª®¢ë© ¢¢®¤-¢ë¢®¤, â.¥ ¨­ä®à¬ æ¨ï ç¨â ¥âáï ¨ § ¯¨á뢠¥âáï 楫묨 ª« áâ¥à ¬¨. „«ï ®áãé¥á⢫¥­¨ï í⮣® ᯮᮡ  ¢¢®¤ -¢ë¢®¤  ¡ë« ­ ¯¨á ­ ¬®¤ã«ì(Files), á ¯®¬®éìî ª®â®à®£® ¢¢®¤-¢ë¢®¤ ¢­¥è­¥ ­¥ ®â«¨ç ¥âáï ®â ®¡ëç­®£®.    ‘奬  ¯à®£à ¬¬ë ¯à¥¤¥«ì­® ¯à®áâ : á­ ç «  ¢ë¯®«­ï¥âáï ¯¥à¢®­ ç«ì­ ï á®àâ¨à®¢ª (¯à®æ¥¤ãà  firstsort), § â¥¬ ¢ë§ë¢ ¥¬ ᪫¥¨¢ ­¨¥(¯à®æ¥¤ãà  ftrans(in1, in2, out1, out2: workfile);), £¤¥ ¯ àë ä ©«®¢ ¢á¥ ¢à¥¬ï ¬¥­ïîâáï ¨ ¯®á«¥ ª ¦¤®£® § ¯ã᪠ ¯à®æ¥¤ãàë ¯à®¢¥àï¥âáï ãá«®¢¨¥ ¢ë室 .    à®æ¥¤ãà  ftrans ®âªà뢠¥â ¢á¥ ä ©«ë, § â¥¬ ¢ë¯®«­ï¥â ­¥áª®«ìª® à § ¯à®æ¥¤ãàã ᫨¢  ®¤­®£® ªã᪠(onestep) ¨ § ªà뢠¥â ä ©«ë. 5. ŠŽŒŒ…’ˆŽ‚€›‰ ’…Š‘’ Žƒ€ŒŒ›. {Œ®¤ã«ì Files.   ‘¤¥áì ¯¥à¥¯¨á ­ë ¢á¥ ¯à®æ¥¤ãàë ¨ ä㭪樨 ­¥®¡å®¤¨¬ë¥ ¤«ï à ¡®âë á ä ©« ¬¨, à ¡®â î騥 á ¡«®ª ¬¨.  ¡®â  á ­¨¬¨ ®áãé¥á⢫ï¥âáï â ª¦¥ ª ª ¨ á ®¡ëç­ë¬¨ ¯à®æ¥¤ãà ¬¨ ¬®¤ã«ï System.} unit Files; interface const typesize=4; const bufsize = 2048; type using=longint; type buffer = array[1..bufsize] of using; type pbuffer = ^buffer; type filemode = (fread, fwrite, closed); type tfile = record  buf: pbuffer;  mode: filemode;  f: file;  count, leng: integer; end; procedure fAssign(var w: tfile; name: string); procedure fReWrite(var w: tfile); procedure fReset(var w: tfile); procedure fPut(var w: tfile; d: using); procedure fGet(var w: tfile; var d: using); procedure fClose(var w: tfile); function fEof(var w: tfile): boolean; implementation procedure fAssign(var w: tfile; name: string); begin  Assign(w.f, name);  w.mode:=closed; end; procedure fReWrite(var w: tfile); begin  if w.mode=closed then  begin   ReWrite(w.f, typesize);   new(w.buf);   w.count:=0;   w.leng:=0;   w.mode:=fwrite;  end; end; procedure fReset(var w: tfile); begin  if w.mode=closed then  begin   Reset(w.f, typesize);   new(w.buf);   BlockRead(w.f, w.buf^, bufsize, w.leng);   w.count:=1;   w.mode:=fread;  end; end; procedure fPut(var w: tfile; d: using); begin  if w.mode=fwrite then  begin   w.count:=w.count+1;   w.buf^[w.count]:=d;   if w.count=bufsize then   begin    BlockWrite(w.f, w.buf^, w.count);    w.count:=0;   end;  end; end; procedure fGet(var w: tfile; var d: using); begin  if (w.mode=fread) then  begin   d:=w.buf^[w.count];   if w.leng=w.count then   begin    BlockRead(w.f, w.buf^, bufsize, w.leng);    w.count:=1;   end else w.count:=w.count+1;  end; end; procedure fClose(var w: tfile); begin  if w.mode=fwrite then BlockWrite(w.f, w.buf^, w.count);  dispose(w.buf);  w.mode:=closed;  Close(w.f); end; function fEof(var w: tfile): boolean; begin  if (w.mode=fread) and (w.leng=0) then fEof:=true  else fEof:=false; end; begin end. {ª®­¥æ files.pas} {----------------------------------------------------------------------------} {” ©« sort.pas - á®àâ¨à®¢ª  ¢ ¯ ¬ïâ¨.} var k: integer; function SwapTops(no: integer): integer; var t: longint; begin  if (memo^[2*no+1]>memo^[2*no]) then  begin   t:=memo^[no];   memo^[no]:=memo^[2*no+1];   memo^[2*no+1]:=t;   SwapTops:=2*no+1;  end else  begin   t:=memo^[no];   memo^[no]:=memo^[2*no];   memo^[2*no]:=t;   SwapTops:=2*no;  end; end; procedure SwapHalf(no: integer); var t: longint; begin  if memo^[no]k then Reg:=true else  if (2*no+1)>k then  begin   SwapHalf(no);   Reg:=true;  end else  if (memo^[2*no]<=memo^[no]) and (memo^[2*no+1]<=memo^[no]) then Reg:=true  else Reg:=false; end; procedure HalfReg(no: integer); var next: integer; begin  next:=no;  while (not Reg(next)) do next:=SwapTops(next); end; procedure RegTree; var i: integer; begin  for i:=k downto 1 do HalfReg(i); end; procedure SwapLeaves(l1, l2: integer); var t: longint; begin  t:=memo^[l1];  memo^[l1]:=memo^[l2];  memo^[l2]:=t; end; procedure SortMemo(len: integer); begin  k:=len;  RegTree;  for k:=len-1 downto 1 do  begin   SwapLeaves(1, k+1);   HalfReg(1);  end; end; {ª®­¥æ sort.pas} {----------------------------------------------------------------------------} {Žá­®¢­ ï ¯®£à ¬¬ } uses Dos, Files{®¤ª«î祭¨¥ ¬®¤ã«ï, ®áãé¥á⢫ïî饣® ¢¢®¤-¢ë¢®¤.}; const memlen=10000;{ §¬¥à ¯ ¬ïâ¨, à §à¥è¥­­®© ¤«ï ¨á¯®«ì§®¢ ­¨ï} type tmemo = array[0 .. memlen] of longint; type pmemo = ^ tmemo;{’¨¯-㪠§ â¥«ì ­  ®á­®¢­®© ¬ áᨢ, ¨á¯®«ì§ã¥¬ë© ¯à®£à ¬¬®©} var memo : pmemo; {$I sort.pas} {®¤ª«î祭¨¥ ä ©« , ᮤ¥à¦ é¥£® ¯à®æ¥¤ãàã á®àâ¨à®¢ª¨ ¬ áᨢ  §  ¢à¥¬ï n*(log n), ­¥ ¨á¯®«ì§ãï ¤®¯®«­¨â¥«ì­®© ¯ ¬ïâ¨(á®àâ¨à®¢ª  ¤¥à¥¢®¬).} type workfile = record  main{®á­®¢­®© ä ©«},  inf{ä ©«, ᮤ¥à¦ é¨© ¤«¨­ë ®âá®àâ¨à®¢ ­­ëå ªã᪮¢}: tfile; end;{tfile - ⨯, ®¯à¥¤¥«¥­­ë© ¢ unit Files, ª®â®àë© § ¬¥­ï¥â ä ©«®¢ë¥ ⨯ë} var  t1, t2, t3, t4,    dest, seur: workfile;  {¢à¥¬¥­­ë¥ ä ©«ë}  {¢å®¤­®© ¨ ¢ë室­®© ä ©«} {ˆ­¨æ¨ «¨§ æ¨ï} procedure Init; var tmp: string; begin  tmp:=getenv('TEMP');  fAssign(t1.main, tmp+'\~fsort-1.tmp');  fAssign(t2.main, tmp+'\~fsort-2.tmp');  fAssign(t3.main, tmp+'\~fsort-3.tmp');  fAssign(t4.main, tmp+'\~fsort-4.tmp');  fAssign(t1.inf, tmp+'\~finf-1.tmp');  fAssign(t2.inf, tmp+'\~finf-2.tmp');  fAssign(t3.inf, tmp+'\~finf-3.tmp');  fAssign(t4.inf, tmp+'\~finf-4.tmp');  fAssign(seur.main,ParamStr(1));  fAssign(dest.main,ParamStr(2)); end; {¥à¢®­ ç «ì­ ï á®àâ¨à®¢ª } procedure firstsort(var inp, out1, out2: workfile); var i, k: longint; begin  fReset(inp.main);  fRewrite(out1.main);  fRewrite(out2.main);  fRewrite(out1.inf);  fRewrite(out2.inf);  new(memo);  repeat   for i:=1 to memlen do    if fEof(inp.main) then    begin     i:=i-1;     break    end else fGet(inp.main, memo^[i]);   k:=i;   sortmemo(k);   for i:=1 to k do fPut(out1.main, memo^[i]);   fPut(out1.inf, k);   if k=memlen then   begin    for i:=1 to memlen do     if fEof(inp.main) then     begin      i:=i-1;      break;     end     else fGet(inp.main, memo^[i]);    k:=i;    sortmemo(k);    for i:=1 to k do fPut(out2.main, memo^[i]);    fPut(out2.inf, k);   end;  until fEof(inp.main);  dispose(memo);  fClose(inp.main);  fClose(out1.main);  fClose(out2.main);  fClose(out1.inf);  fClose(out2.inf); end; {à®æ¥¤ãà , ª®¯¨àãîé ï § ¤ ­­®¥ ª®«¨ç¥á⢮ í«-⮢ ¨§ ®¤­®£® ä ©«  ¢ ¤à㣮©. ˆá¯®«ì§ã¥âáï ¯à¨ á«¨¢ ­¨¨ ¤«ï ª®¯¨à®¢ ­¨ï ®á⠢襩áï ç á⨠ªã᪠(¥á«¨ ¤à㣮© ªã᮪ ¨ááïª).} procedure Copy(var inp, out: workfile; c0: longint); var c, n: longint; Done: boolean; begin  for c:=c0 downto 1 do  begin   fGet(inp.main, n);   fPut(out.main, n);  end; end; {‘«¨¢ ¥â ¤¢  ®ç¥à¥¤­ëå ªã᪠ ¨§ ä ©«®¢ in1 ¨ in2 ¨ § ¯¨á뢠¥â ¢ out.} procedure onestep(var in1, in2, out: workfile; c01, c02: longint); var n1, n2, c1, c2, c: longint; Done: boolean; begin  Done:=false;  c1:=c01-1;  c2:=c02-1;  c:=0;  fGet(in1.main, n1);  fGet(in2.main, n2);  repeat   if n1 <¢ë室­®© ä ©«>    à®£à ¬¬  ­¥ § ¤ ¥â ­¨ ª ª¨å ¢®¯à®á®¢, ç⮠ᨫ쭮 ã¯à®é ¥â à ¡®âã á ­¥©.    ¥§ã«ìâ â à ¡®âë ¬®¦­® ¯®¢¥à¨âì á ¯®¬®éìî ¯à¨« £ ¥¬®© ã⨫¨âë f_check, ᮧ¤ âì á«ãç ©­ë© ¨á室­ë© ä ©« - á ¯à®¬®éìî f_make.    à¨ç¨­ ¬¨ ®è¨¡®ª ¬®£ãâ á«ã¦¨âì ­¥ ᮮ⢥âá⢨¥ á¨á⥬ë âॡ®¢ ­¨ï¬, ¨§«®¦¥­­ë¬ ¢ ¯. 6, ­¥¤®áâ â®ç­®¥ ¬¥áâ® ­  ¤¨áª¥, à §¬¥à(¢ ¡ ©â å) ¨á室­®£® ä ©«  ­¥ ªà â¥­ 4.    ‚ ¤ ­­®¬ ®âç¥â¥ ®¯¨á뢠¥âáï á ¬ ï í䥪⨢­ ï ¢¥àá¨ï í⮩ ¯à®£à ¬¬ë, ­® áãé¥áâ¢ãîâ ¢¥àᨨ, ­¥ ¨á¯®«ì§ãî騥 ¢¢®¤-¢ë¢®¤ ¡«®ª ¬¨, âॡãî騥 ¬¥­ìè¥ à¥áãàᮢ á¨á⥬ë. 8. Žˆ‘€ˆ… ’…‘’Ž‚.    à®£à ¬¬¬  â¥áâ¨à®¢ « áì ­¥®¤­®ªâ â­®, ­  ¢å®¤¥ ¨á¯®«ì§®¢ «¨áì, ¢ ®á­®¢­®¬, ä ©«ë ¨§ á«ãç ©­ëå ç¨á¥« à §«¨ç­®© ¤«¨­ë.   ¢ë室¥ ¡ë«¨ ¯®«ã祭ë ä ©«ë ⮩¦¥ ¤«¨­ë, ­¥ ᮤ¥à¦ é¨¥ ®è¨¡®ª, â.¥. ç¨á«  ¢ íâ®å ä ©« å ®ª § «¨áì ¢ ¯®à浪¥ ­¥ áâண®£® ¢®§à áâ ­¨ï. ‘®¤¥à¦¨¬®¥ íâ¨å ä ©«®¢ ¯®«­®áâìî ᮢ¯ «® á à §ã«ìâ â ¬¨ à ¡®âë ¤àã£¨å ¯à®£à ¬¬ á®àâ¨à®¢ª¨ ­  â¥å ¦¥ ¢å®¤­ëå ä ©« å, ç⮠ᨫ쭮 á­¨¦ ¥â ¢¥à®ïâ­®áâì ¤¨ä¥ªâ®¢ ¯à®£à ¬¬ë.    à¨ â¥áâ¨à®¢ ­¨¨ ¨á¯®«ì§®¢ «¨áì ®¯¥à æ¨®­­ë¥ á¨á⥬ë MS-DOS 6.22, Windows`95, ª®¬¯ìîâ¥àë PC AT 486DX4-100, 486SX-25, à ¡®â î騥 á «®ª «ì­ë¬ ¢¨­ç¥áâ¥à®¬, ஡®ç¨¥ á⠭樨 486DX-40, 386SX, à ¡®â î騥 ¢ á¥â¨ Novell. ¥§ã«ìâ âë â¥áâ¨à®¢ ­¨ï(­  ä ©«¥ à §¬¥à®¬ 4M) § ­¥á¥­ë ¢ â ¡«¨æã: ª®¬¯ìîâ¥à    à ¡®â  ¢ á¥â¨     ¢à¥¬ï à ¡®âë 486DX4-100    ­¥â       3 ¬¨­. 486SX-25      ­¥â       7 ¬¨­. 486DX-40      ¤  386SX         ¤