Jump to content

_ArrayBinarySearch Poblem


 Share

Recommended Posts

OK, so this is killing me...

I have a 1D array which is ~190000 indexes long. It has all been sorted using _ArraySort(), however, when i use _ArrayBinarySearch() it cannot find any value I put into it; giving an @ERROR output of 2, which by the helpfile says that...

_ArrayBinarySearch(Const ByRef $avArray, $vValue[, $iStart = 0[, $iEnd = 0]])

@ERROR:
2 - [b]$vValue outside of array's min/max values[/b]

What does this actually mean?? The value i am searching for, i know exists 100%, yet it cannot find it.

A sample of the sorted data in the array looks something like:

1000010582494
100003156
100003874824478
100005183091
1000071928702
10001123902
100011716431
1000121507
10001321465905
10001365

If i use _ArraySearch() then it finds it fine; but with such a large collection of data, it is bound to be quicker using _ArrayBinarySearch().

Thanks in advance, if you can shed any light on this!

Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

  • Moderators

furrycow,

Your array does not look sorted to me if that sample is anything to go by. When I put some of your values in the _ArrayBinarySearch example, this is the sorted array I get:

[0]|10001365
[1]|100003156
[2]|1000121507
[3]|100005183091
[4]|100011716431
[5]|100011716549
[6]|1000010582494
[7]|1000071928702
[8]|10001321465905
[9]|100003874824478

It is hardly surprising if you get an "out-of-range" error if you use this function when the elements are not ordered.

Are you sure _ArraySort is doing its job? have you compared the arrays (or at least part of them :D ) before and after?

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

The @error = 2 means your search term is less than the value in the first index or more than the value in the last index. Since the array has to be sorted before the search, that means there is no match IF the array was sorted properly beforehand.

That array is NOT sorted properly for use with _ArrayBinarySearch(). Should be:

10001365
100003156
1000121507
10001123902
100005183091
100011716431
1000010582494
1000071928702
10001321465905
100003874824478

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks guys for your replies!

I had sorted it all using _ArraySort() and it did give me order that which is shown in my original post...and it did look sorted to me, because if you look at them, each digit in each number string less than or equal to that below it.

HOWEVER! Even though i had sorted and displayed the array many times...as it always is with me...it now works how you have explained...so in all honesty i have no idea why it did what it did for me.

Thanks again for your help Psalty and Melba....now just to let my old P4 sort out the 190000 values again...its in for a rough night...

...i need a new computer.

EDIT! Aha, i see why it wasnt working for me..they were strings! So it wasnt treating them as a number when it came to sorting it.

Edited by furrycow
Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

OK, so sorry to carry on like this, but from what i have discovered...

I am getting my ~190000 array list from a txt file, and using the following:

$NumLIST = StringSplit(StringStripCR(FileRead(@DesktopDir & "\NumlistTEST.txt")),@LF)

To put the whole list into an array..but of course then every indexed item in that array is treated as a string, so when i sort that array, it sorts it treating every item as a string rather than a number (even though every value is numerical).

Is there any way to read the values from a txt file, whilst keeping them as a number rather than a string, and not by using FileReadLine()? As this would take yonks!

I admit the StringSplit() is not going to be doing me any favours in making the outcome not a string, but any ideas?

Thanks again!

Instant Lockerz Invite - www.instantlockerzinvite.co.uk
Link to comment
Share on other sites

  • Moderators

furrycow,

Just convert all the strings to numbers before sorting like this: :D

#include <Array.au3>

$sData = "10001321465905" & @CRLF
$sData &= "1000071928702" & @CRLF
$sData &= "1000121507" & @CRLF
$sData &= "100011716431" & @CRLF
$sData &= "100003156" & @CRLF
$sData &= "10001365" & @CRLF
$sData &= "100005183091" & @CRLF
$sData &= "100011716549" & @CRLF
$sData &= "100003874824478"

$avArray = StringSplit(StringStripCR($sData),@LF)

; display unsorted array
_ArrayDisplay($avArray, "$avArray BEFORE _ArraySort()")

; Convert strings to numbers
For $i = 1 To $avArray[0]
    $avArray[$i] = Number($avArray[$i])
Next

; sort the array to be able to do a binary search
_ArraySort($avArray)

; display sorted array
_ArrayDisplay($avArray, "$avArray AFTER _ArraySort()")

; lookup existing entry
$iKeyIndex = _ArrayBinarySearch($avArray, 100003156) ; <<<<<<<<<<<<< make sure this is a number too!!!!!!!!!!
If Not @error Then
   MsgBox(0,'Entry found',' Index: ' & $iKeyIndex)
Else
   MsgBox(0,'Entry Not found',' Error: ' & @error)
EndIf

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...