Re: Binary Search
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Mon, 18 Jun 2007 23:55:42 -0500
John Dough wrote:
On Sun, 17 Jun 2007 22:07:02 -0500, Rob Kennedy <me3@xxxxxxxxxxx>
wrote:
Make your data structure store arrays instead of single items.
My data structure is an array of strings with 100 elements [1..100]
Yeah. Who says you can't change it to better suit your needs? So far, your stated needs are to sort the list and to find duplicates. Why use an array for that?
For testing purposes, I've divided the array up into 4 groups of 25
strings. There's 25 instances of "test", along with 25 instances of
"aaa"..... "bbb"....."ccc".
I sort the array before searching (as is required with binary search).
Binary search requires that the elements be in sorted order. It doesn't require that the search be preceded by an explicit sort operation. (So, for instance, if you're inserting things in the right place already, you don't need a separate sorting step.)
The reason you're not getting examples is because an example would require laying out a data structure and then writing the binary search into which the solution would be added. That's a lot for a one-off example. If you'd shown what you already have, an example might have been more forthcoming.
I already did that.
Right, sorry. I missed that message.
function TForm1.BinarySearch(const searchstring: string; const
Left,Right: integer): integer;
OK, since you want to find a range, rather than just one element, let's start by changing the return value:
type
TArrayRange = record
First, Last: Integer;
end;
This will represent a half-open interval [First,Last). So the first element in the array will be at index First and the final element in the range will be at index Last-1. That has the nice property that any range where First=Last is empty. It also means that the size of the range is Last-First.
So here's the new function signature:
function TForm1.BinarySearchForDuplicates(const searchstring: string; const Left, Right: Integer): TArrayRange;
var L,R,M,CompareResult: integer;
begin
L := Left;
R := Right;
while (L <= R) do
begin
M := (L + R) div 2;
CompareResult := CompareText(datalist[M],searchstring);
if (CompareResult = 0) then
begin
Result.Left := M;
Result.Right := M + 1;
// Now do two more binary searches. First, on the left side for
// the greatest L such that datalist[L] <> searchstring. It might
// not exist.
L := Left;
R := Result.Left;
while L < R do begin
M := (L + R) div 2;
Assert(M < R); // L < R and "div" rounds down.
CompareResult := CompareText(datalist[M], searchstring);
Assert(CompareResult <= 0); // We've already found one match on
// the far right of our subrange.
if CompareResult < 0 then begin
// We jumped too far to the left. We're in the non-match zone.
// M is not a match, so go to the next one.
L := M + 1;
end else begin
// We jumped too far to the right. We're in duplicate zone.
R := M;
end;
end;
Assert(L = R);
Result.Left := L;
// Now for the second search, finding the least R such that
// searchstring < datalist[R]. R might not exist.
L := Result.Right + 1;
R := Right;
while L < R do begin
M := (L + R) div 2;
Assert(M < R); // L < R and "div" rounds down.
CompareResult := CompareText(datalist[M], searchstring);
Assert(CompareResult >= 0); // We've already found one match on
// the far left of our subrange.
if CompareResult > 0 then begin
// We jumped too far to the right. We're in the non-match zone.
// M is not a match, so go to the next one.
R := M;
end else begin
// We jumped too far to the left. We're in duplicate zone.
L := M + 1;
end;
end;
Assert(L = R);
Result.Right := R;
Result := M;
foundlist.Add(inttostr(m)+' - '+datalist[m]);
L := M + 1; // this is the only line I've played with (it
used to be just "exit")
A call to "exit" is sufficient at this point.
end
else if (CompareResult < 0) then L := M + 1
else R := M - 1;
end;
Result := -1;
end;
--
Rob
.
- References:
- Binary Search
- From: John Dough
- Re: Binary Search
- From: Stefan Meisner
- Re: Binary Search
- From: John Dough
- Re: Binary Search
- From: Maarten Wiltink
- Re: Binary Search
- From: John Dough
- Re: Binary Search
- From: Rob Kennedy
- Re: Binary Search
- From: John Dough
- Binary Search
- Prev by Date: Re: Binary Search
- Next by Date: Re: Binary Search
- Previous by thread: Re: Binary Search
- Next by thread: Re: Binary Search
- Index(es):
Relevant Pages
|