![]() |
delphi: binary sort
Posted by bryn.
Student on team #1 from uni. Posted on 11/23/2000 6:00 AM MST hi, this is the code in my prog. basically it reads a file called list.txt and then finds a number which the user entered... prob is it only works for sorted data. and i need to get a sort going! a binary sort would be good! how do i do it? could someone sort it for me? and email it, its kinda important heh brynbush@hotmail.com cheers Bryn unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Findbtn: TButton; Inputbox: TEdit; Label1: TLabel; Label2: TLabel; DispBox: TMemo; outputbox: TMemo; Label3: TLabel; Label4: TLabel; procedure FindbtnClick(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} procedure TForm1.FindbtnClick(Sender: TObject); var fil:textfile; nam:Array [1..40] of string; num:Array [1..40] of integer; ct:integer; items:integer; high:integer; Mid:integer; Low:integer; Number:integer; begin Assignfile(fil,'c:\delphi\soft124\list.txt'); Reset(Fil); ct:=0; while not(eof(fil)) do begin ct:=ct+1; Readln(fil, num [ct], nam [ct]); DispBox.Lines.Add(Format('%2d %4d %s', [ct, num[ct], nam[ct]])); end; items := ct; outputbox.clear; Number := strtoint(inputbox.text); low := 1; high := items; Inputbox.clear; if Number = num[low] then Outputbox.Lines.Add(format('%3d %s', [low,nam[low]])); if Number num[high] then showmessage('Number Entered is too High') else if Number = num[high] then Outputbox.Lines.Add(format('%3d %s', [high,nam[high]])) else repeat mid := trunc((low+high)/2); if Number = num[mid] then Outputbox.Lines.Add(format('%3d %s', [Mid,nam[Mid]])) else if Number |
| All times are GMT -5. The time now is 21:40. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi