±ÍÇÏ´Â ¼Õ´Ô À̽ʴϴÙ
·Î±×ÀÎ
ȸ¿ø°¡ÀÔ
  
  µ¨¸¶´ç °ø½Ä ÀºÇà°èÁÂ
  ÇϳªÀºÇà 227-910235-83607
  ¿¹±ÝÁÖ ÀÌ»ó±¹(¿î¿µÁø)
ÇÁ·ÎÁ§Æ® °Ô½ÃÆÇ
ÅõÇ¥°Ô½ÃÆÇ
µ¨¸¶´ç¼Ò°³
±âÃʺÎÅÍ È°¿ë±îÁö! µ¨ÆÄÀÌ ±³À° - µ¥ºê±â¾î
°­ÁÂ, ÆÁ, Á¤º¸ °­ÁÂ, ÆÁ, Á¤º¸ ÀÔ´Ï´Ù.
±Û³»¿ë - °­ÁÂ, ÆÁ, Á¤º¸
 Quick Sort¸¦ »ç¿ëÇÑ µ¥ÀÌÅ͠󸮠¹× Ã£±â Sample ÄÚµå
¼ö¿ø¼º
(°­°æ¼ö)
2016-07-13 ¿ÀÀü 11:44:42
Ä«Å×°í¸®: ÆÁ
2268ȸ Á¶È¸



µî·ÏµÈ ÆÄÀÏÀÌ ¾ø½À´Ï´Ù.
// 100¸¸°³ Record Ã³¸®Çϴµ¥ 6ÃÊ °É¸².
unit Unit5;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  // Record ¼±¾ðÇϱâ 
  PDataA = ^TDataA;

  TDataA = Record
    admn_no: integer;
    name: string;
  end;

  TForm5 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form5: TForm5;

implementation

{$R *.dfm}

// sort(admn_no) 
function Sort_Item_admn_no(item1, item2: Pointer): integer;
begin
  Result := PDataA(item1)^.admn_no - PDataA(item2)^.admn_no;
end;

// sort(name) 
function Sort_Item_name(item1, item2: Pointer): integer;
begin
  Result := AnsiCompareText(PDataA(item1)^.name, PDataA(item2)^.name);
end;

// sort(name, admn_no)
function Sort_Item_name_no(item1, item2: Pointer): integer;
begin
  Result := AnsiCompareText(PDataA(item1)^.name, PDataA(item2)^.name);
  if Result = 0 then
    Result := PDataA(item1)^.admn_no - PDataA(item2)^.admn_no;
end;

function QuickFind(List: TList; var Index: integer; pt: Pointer; Compare: TListSortCompare): boolean;
var
  L, h, i, C: integer; // Ã¹¹ø° Ç׸ñÀ» Ã£µµ·Ï ÇÔ. 
begin
  Result := false;
  L := 0;
  h := List.Count - 1;
  if (Index >= L) and (Index <= h) and (Compare(List[Index], pt) = 0) then
  begin
    Result := true;
    exit;
  end;

  while L <= h do
  begin
    i := (L + h) shr 1;
    C := Compare(List[i], pt);
    if C < 0 then
      L := i + 1
    else
    begin
      h := i - 1;
      if C = 0 then
      begin
        Index := i;
        Result := true;
      end;
    end;
  end;
end;

procedure TForm5.Button1Click(Sender: TObject);
var
  List: TList;
  i, n, nCnt, index: integer;
  aP, cP: PDataA;
begin
  List := TList.create;
  New(cP); // Ã£±â¿ë Record »ý¼ºÇÔ. 
  try
    Memo1.Lines.Clear;
    Memo1.Lines.BeginUpdate;
    nCnt := 1000000; // 100¸¸°³ µ¥ÀÌÅÍ
    // Sample data ¸¸µé±â // Test¿ë - ½ÇÁ¦ µ¥ÀÌÅÍ ³ÖÀ¸¸é µÊ. 
    for i := 0 to nCnt - 1 do
    begin
      New(aP); // »õ·Î¿î µ¥ÀÌÅÍ 
      List.Add(aP); // List¿¡ ³Ö±â 
      aP^.admn_no := i; // ½ÇÁ¦ µ¥ÀÌÅÍ °ª 
      aP^.name := inttostr(i);
    end;

    // ¼ø¼­ ¼¯±â 
    Randomize;
    for i := nCnt-1 downto nCnt div 2 do
    begin
      n := Random(i);
      if n <> i then
        List.Exchange(n, i);
    end;

    // Sort Àü 
    // Memo1.Lines.Add('Sort Àü'); 
    for i := 0 to List.Count - 1 do
      with PDataA(List[i])^ do
      begin
        // Memo1.Lines.Add(name); // nameÀ¸·Î Sort ¼ø¼­¸¦ º¼¼ö ÀÖÀ½. 
      end;

    List.Sort(Sort_Item_name); // Sort(name) 

    // Memo1.Lines.Add('Sort ÈÄ'); 
    for i := 0 to List.Count - 1 do
      with PDataA(List[i])^ do
      begin
        // Memo1.Lines.Add(name);  // nameÀ¸·Î Sort ¼ø¼­¸¦ º¼¼ö ÀÖÀ½. 
      end;

    // Sort ÈĠã±â 
    Memo1.Lines.Add(''); //
    cP^.name := inttostr(Random(nCnt)); // Ã£À» °ª ³Ö±â 
    if QuickFind(List, Index, cP, Sort_Item_name) then // Sort ÈĠã±â (log2 N) 
    begin
      aP := List[index]; // Index ¼ø¹ø, aP Ã£Àº  record; 
      Memo1.Lines.Add(aP^.name);
    end;

//    List.Sort(Sort_Item_name_no); // sort(name, admn_no) //Ç׸ñÀº ¿øÇϴ µ¥·Î ´Ã¸®¼ö ÀÖÀ½.
                                  // ÇØ´ç ºñ±³±¸¹®À» ¸¸µé¾î Áà¾ß ÇÔ.

    List.Sort(Sort_Item_admn_no); // Sort(admn_no)

    for i := 0 to List.Count - 1 do
      with PDataA(List[i])^ do
      begin
        // Memo1.Lines.Add(inttostr(admn_no));  // nameÀ¸·Î Sort ¼ø¼­¸¦ º¼¼ö ÀÖÀ½. 
      end;

    // Sort ÈĠã±â 
    cP^.admn_no := Random(nCnt); // Ã£À» °ª ³Ö±â 
    if QuickFind(List, Index, cP, Sort_Item_admn_no) then // Sort ÈĠã±â (log2 N) 
    begin
      aP := List[index]; // Index ¼ø¹ø, aP Ã£Àº  record; 
      Memo1.Lines.Add(aP^.Name);
    end;

  finally
    Memo1.Lines.EndUpdate;
    for i := 0 to List.Count - 1 do
    begin
      PDataA(List[i])^.name := ''; // String°æ¿ì reset 
      Dispose(List[i]); // ¸Þ¸ð¸® ÇØÁ¦ 
    end;

    List.Free;
    Dispose(cP);
  end;
end;

end.