Find Code:
All Words
Any of the Words
Exact Phrase
Home
:
Code
:
Forums
:
Submit
:
Mailing List
:
About
:
Contact
Code
All
VB.NET
ASP.NET
C#
VB Classic
ASP Classic
Snippets
Popular
Resources
Submit Code
Forums
Articles
Tips
Links
Books
Contest
Link to us
Dot Net's BinarySearch
Author:
Philfr
Submitted:
9/14/2005
Version:
VB 2005
Compatibility:
VB.NET 2002, VB.NET 2003, VB 2005
Category:
Miscellaneous
Views:
16197
Dot Net's BinarySearch function has a defect when the array contains duplicate data.
Declarations:
'none
Code:
Function MyBinarySearch(ByVal arr() As String, ByVal TargetString As String) As Integer ' written by Philfr ' 13-9-05 ' for Certificate 4, students, ' Moreton Institute of TAFE ' Queensland, Australia ' Dot Net's BinarySearch function has a defect when the array ' contains duplicate data, as shown below. ' eg. Find "3" ' 1,1,1,1,2,2,3,3,3,3,3,3,5,5,7,7,7,7,7,8 (20 entries) ' ' Because the 10th entry (half way) is the first stop for a binary search, ' and because "3" is found there, Dot Net returns that value. In as much as ' this value is "3", the result is correct, but it is not the first such value. ' My routine searches previous array values, and returns the actual value ' of the first such element in the array. ' please retain the credit as above, if you intend using this code. Dim i As Integer i = arr.BinarySearch(arr, TargetString) Do While i > 1 AndAlso arr(i) = arr(i - 1) i = i - 1 Loop Return i End Function
Home
|
Forums
|
Submit
|
Books
|
Mailing List
|
Advertising
|
About
|
Contact
© 2024 A1VBCode. All rights reserved.
Legal disclaimer & terms of use
Privacy statement