home *** CD-ROM | disk | FTP | other *** search
/ What PC? 1996 August / WPCAUG96.ISO / multi / singing / shared.dxr / 01980_BINARY.ls < prev    next >
Encoding:
Text File  |  1996-06-12  |  842 b   |  37 lines

  1. on BinarySearch thePhrase
  2.   global dataFirst, dataLast
  3.   set first to dataFirst
  4.   set last to dataLast
  5.   set found to 0
  6.   repeat while 1
  7.     if first >= last then
  8.       set found to thePhrase = the name of cast first
  9.       set chop to first
  10.       exit repeat
  11.     end if
  12.     set chop to (first + last) / 2
  13.     set testAgainst to the name of cast chop
  14.     if thePhrase = testAgainst then
  15.       set found to 1
  16.       exit repeat
  17.     end if
  18.     if thePhrase < testAgainst then
  19.       set last to chop - 1
  20.       next repeat
  21.     end if
  22.     set first to chop + 1
  23.   end repeat
  24.   if found then
  25.     return chop
  26.   else
  27.     return -1
  28.   end if
  29. end
  30.  
  31. on TestBinarySearch
  32.   set testData to ["range", "opera", "cadenza", "maestro", "a tempo", "the business"]
  33.   repeat with test in testData
  34.     put the number of cast test && BinarySearch(test)
  35.   end repeat
  36. end
  37.