Delphi algorithm and Data Structure Learning and perception [1]: describes the importance of the algorithm through "sequential search" and "binary search"

Source: Internet
Author: User
Test:


Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) button1: tbutton; memo1: tmemo; procedure button1click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} {sequential query function} function seqsearch (list: tstringlist; const STR: string): integer; var I: integer; begin for I: = 0 to list. count-1 do if comparetext (list [I], STR) = 0 then begin result: = I; exit; end; Result: =-1; end; {binary search function; binary Search can only target ordered list} function binarysearch (list: tstringlist; const STR: string): integer; var L, R, M: integer; compareresult: integer; begin result: =-1; L: = 0; R: = List. count-1; while l 0 then R: = m-1 else begin result: = m; exit; end; {comparison test} procedure tform1.button1click (Sender: tobject); var testlist: tstringlist; I: integer; N1, N2: int64; count1, count2: integer; s: string; const num = 1000000; {prepare to test millions of data records} begin testlist: = tstringlist. create; for I: = 0 to num-1 do testlist. add (inttohex (I, 8); {prepare an ordered list of test values} memo1.clear; count1: = 0; count2: = 0; {engage in 10 experiments} For I: = 0 to 9 do begin {random string within the generation range} randomize; s: = inttohex (random (Num), 8); {sequential lookup} querycececounter (N1 ); seqsearch (testlist, S); queryperformancecounter (N2); memo1.lines. add (inttostr (n2-n1) + #9); count1: = count1 + (n2-n1); {binary lookup} queryperformancecounter (N1); binarysearch (testlist, S ); queryperformancecounter (N2); memo1.lines [I]: = memo1.lines [I] + inttostr (n2-n1); count2: = count2 + (n2-n1); end; memo1.lines. add ('----------------'); memo1.lines. add ('average: '); memo1.lines. add (inttostr (count1 Div 10) + #9 + inttostr (count2 Div 10); memo1.lines. add ('----------------'); memo1.lines. insert (0, 'ordered '# 9' binary'); testlist. free; end.
 
   
 

The binary search is too fast and cannot be tested using gettickcount. Therefore, queryperformancecounter must be used;
In addition, the tstringlist. Find method also uses the "binary search" method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.