View Single Post
  #1   Spotlight this post!  
Unread 24-06-2002, 00:30
archiver archiver is offline
Forum Archival System
#0047 (ChiefDelphi)
 
Join Date: May 2001
Location: Pontiac, MI
Posts: 21,214
archiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond reputearchiver has a reputation beyond repute
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
__________________
This message was archived from an earlier forum system. Some information may have been left out. Start new discussion in the current forums, and refer back to these threads when necessary.