loading...
مرجع مهندسی نرم افزار
آخرین ارسال های انجمن
حامد شیرزاد بازدید : 597 شنبه 21 آبان 1390 نظرات (0)
 
type 
TStringArray = Array of String; 
{--------------------------------------------------------------------} 
//Start is the index of the first item of the array - usually 0 
//Stop is the index of the last item of the array 
procedure QuickSort(var Strings : TStringArray; Start, Stop : Integer); 
Var 
Left : Integer; 
Right : Integer; 
Mid : Integer; 
Pivot : String; 
Temp : String; 
begin 
Left := Start; 
Right := Stop; 
Mid := (Start + Stop) div 2; 
Pivot := Strings[mid]; 
Repeat 
While Strings[Left] <; Pivot do inc(Left); 
While Pivot <; Strings[Right] do dec(Right); 
If Left Right; 
If Start < Right then QuickSort(Strings, Start, Right); // Uses 
If Left <; Stop Then QuickSort(Strings, Left, Stop); // Recursion 
end; 
{--------------------------------------------------------------------} 
function BinSearch(Strings : TStringArray; SubStr : String) : Integer; 
var 
First : Integer; 
Last : Integer; 
Pivot : Integer; 
Found : Boolean; 
 
begin 
First := Low(Strings); //Sets the first item of the range 
Last := High(Strings); //Sets the last item of the range 
Found := False; //Initializes the Found flag (Not found yet) 
Result := -1; //Initializes the Result 
 
//If First >; Last then the searched item doesn't exist 
//If the item is found the loop will stop 
While (First SubStr then 
Last := Pivot - 1 
//else select the second half 
else First := Pivot + 1; 
end; 
end; 
{--------------------------------------------------------------------} 
 
//To use the Binary Search: 
procedure Button1Click(Sender : TObject); 
var 
MyStrings : TStringArray; 
begin 
//Give some values to your strings and remember to 
//Set it to the correct length :) 
//.. 
//.. 
//.. 
 
QuickSort(MyStrings, 0, High(MyStrings); 
ShowMessage('The index of 'Derek' is: ' + 
IntToStr(BinSearch(MyStrings,'Derek') 
//If 'Derek' is in MyStrings, its index value will be returned, 
//otherwise -1 will be returned. 
end;

 

برچسب ها delphi , دلفی , آرایه ,
مطالب مرتبط
ارسال نظر برای این مطلب

کد امنیتی رفرش
درباره ما
به نام آنکه جان را فکرت آموخت در این وبلاگ سعی می شود به صورت تخصصی به مباحث مربوط به مهندسی نرم افزار به خصوص برنامه نویسی کامپیوتری پرداخته شود. مدیر وبلاگ : حامد شیرزاد
اطلاعات کاربری
  • فراموشی رمز عبور؟
  • آمار سایت
  • کل مطالب : 431
  • کل نظرات : 9
  • افراد آنلاین : 3
  • تعداد اعضا : 109
  • آی پی امروز : 85
  • آی پی دیروز : 72
  • بازدید امروز : 505
  • باردید دیروز : 81
  • گوگل امروز : 0
  • گوگل دیروز : 0
  • بازدید هفته : 836
  • بازدید ماه : 3,487
  • بازدید سال : 22,588
  • بازدید کلی : 791,116
  • کدهای اختصاصی

    قالب وبلاگ