optimizing search in two dimensional array

Jeevi

Member
Joined
Nov 2, 2005
Messages
15
Programming Experience
1-3
Hi,
i want to search a particular string on first dimension of two dimensional array. i can do this by looping thru each element on the array. but its really time consuming and my array has large amout of data too and also i need to repeat this process many times.so that, i like to use some default functions to do this such as binary search or anyother?
i know, we can use binary search on one dimensional array. but , how can we implement this for two dimen. or is there any direct way to copy one dimension of two dimensional array into one dimensional array?
Thanks in advance.
 
I would be tempted to make another location array from your first.. so say your first one has 1 million in it.. like you said it would take ages to search through every one and check to see if its the one you want... so I would split this array up into say 1000 parts and convert the string in the array into a number.. would use asc numbers for this... so something like
VB.NET:
sa = 0
 
For n = 0 to ubound(bigarray) - 1 step 1000
redim preserver smallarray(n)
smallarray(sa) = getasc(bigarray(n))
sa += 1
next

the getasc function would be something like..
VB.NET:
Public Function getasc(ByVal p As String)
 
Dim no As Integer
Dim ns As String
ns = ""
 
For no = 1 To Len(p)
ns = ns & Asc(UCase(Mid(p, no, 1)))
Next
If Len(p) < 41 Then 'rebuild length
For no = 1 To 40 - Len(p)
ns = ns & "00"
Next
Else
ns = Mid(ns, 1, 80)
End If
getasc = ns
End Function
The above function converts all strings to ascii numbers.. this makes it very easy then to locate the correct section of the array your search needs to look in... so once you have this small array done, you simply convert your search string.. Ie.. SE to getasc("SE") and it will return the value.. then you just do a do while search value is less then array value.. once its bigger, you then know the value you need to find was in the last array section..

no doubt I've prob lost you by now.. but I hope this helps

Steve
 
Last edited by a moderator:
optimizing search

hi,
thank you for ur reply. i am storing date value as string in my array. i can use "binary search", bcz my array is a sorted one. but my problem is, my array is two dimensional.
 
Can you more accurately describe what youre trying to achieve? (i.e. dont recap your current solution and the flaw with the current solution - tell me what problem youre actually trying to solve by using a 2D string array)

I cant help but think that you would be better using a DataTable as your container, as it has built in searching and typed columns; something your 2D string array lacks
 
let me explain,
in my program i am using two dimensional array to store large amount of data. i need to search for particular string, for that i am using For..Next to loop thru my array and i need to repeat this step many times for some other checking. this is really time consumable and so i am searching for some other method to optimize this process.

we can use BinarySearch on single dimensional sorted arrays. but what about multi dimensional arrays searching? is there any suggestion?
for example, my code will be like that,
VB.NET:
Dim myArray(1000,1) as string
Dim mySearchString as string = "Hello"
Dim myResult as string
myArray = GetArray() //some custom function which returns array
For i as integer = 0 to ubound(myArray,1)
If mySearchString = myArray(i,0) Then
myResult = myArray(i,1)
Exit For
End If
Next
ur idea also seems to be good. let m try. is there any way to seach particular dimension in Datatable? that is, can we use something like "Datatable.Rows.itemArray(2).search(myString)", without looping thru each value?
Thanks.
 
Last edited by a moderator:
DataTable.Select() takes a string that looks markedly similar to a database sql WHERE clause.

Imagine:
Dataset designer, create a Person DataTable of:
Name, String
Age, Integer
Alive, Boolean


VB.NET:
Dim x as new PersonDataTable
x.AddPersonRow("fred", "23", true)
x.AddPersonRow("john", "43", true)
x.AddPersonRow("joe", "68", false)
 
 
Dim deadOnes as PersonDataRow( ) 'array
deadOnes = x.Select("Alive = false")
 
Dim youngOnes as PersonDataRow( )
youngOnes = x.Select("age < 50")
 
Dim joOnes as PersonDataRow( )
joOnes = x.Select("Name LIKE 'jo%'")
 
Dim johnHimself as PersonDataRow( )
johnHimself = x.Select("Name = 'john'")

Useful!
 
Re.Datatable

oh! yeah. this is what i really want. i think using this datatable, atleast i can reduce the burden of my loop.
Thank you very much for ur help.
 
http://msdn2.microsoft.com/en-us/library/system.data.datatable.select.aspx does NOT have "This method is new in .NET Framework 2.0" in red text at the top - it normally does for the new stuff but it may be a document errata

You can assess for yourself, by pressing F2, type datatable and have a look. F2 is one of the most useful things about visual studio - sometimes I just read it for fun! Other times I might want to know if there is already a suitably named exception for what I want to throw, so i might search Exception.. "ahh, OperationCanceledException is named suitably for my need! I'll re-use that!"
 
Back
Top