webu
Full Member
Posts: 149
|
Post by webu on Dec 18, 2022 19:33:25 GMT 1
No, we don't need an extra query for this, just move the termination condition from the loop-end (with 'Until') to the loop-entry (with 'While'):
Function bin_search2(such$) ' Searches in the range of 0 - (n-1) and returns the correct position in the VAR 'fnd'. If the element of 'fnd' is then not the one searched for, then it does not exist and can be inserted at exactly this position 'fnd' with the Insert-command. ' Sucht im Bereich von 0 - (n-1) und gibt in der VAR 'fnd' die richtige Stelle zurück. Ist das Element von 'fnd' dann nicht das gesuchte, dann ist es nicht vorhanden und kann an genau dieser Stelle 'fnd' mit Insert-Befehl eingefügt werden. Dim i% Dim unten% = -1 Public fnd% = n + 1 // Global for using outside ' While fnd <> unten + 1 i = (unten + fnd) \ 2 // Integer-div funktioniert auch. If a$(i) < such$ unten = i Else fnd = i EndIf Wend
Return such$ = a$(fnd)
Try it with one element in a$(0) and it will work also proper.
i have not need this for an array of only one element. My first version at that time needed at least one loop pass minimum, so my exit-condition was at the end.
|
|
|
Post by dragonjim on Dec 19, 2022 10:53:19 GMT 1
For your sort and remove duplicates code, you are better off ReDim-ing the array only once as this is "cycle-intensive". A quicker way is to keep a count of the new total of elements and then ReDim at the end.
The code below shows that, even with this small array, it is generally up to twice as fast:
Dim t# Dim a$(10), i% a$(0) = "-1" a$(1) = "0" a$(2) = "0" a$(3) = "1" a$(4) = "1" a$(5) = "1" a$(6) = "2" a$(7) = "2" a$(8) = "3" a$(9) = "4" a$(10) = "4" QSort a$()
t# = Timer i% = UBound(a$()) While i > 0 If a$(i%) = a$(i% - 1) Delete a$(i%) ReDim a$(UBound(a$()) - 1) EndIf i%-- Wend Debug Timer - t#
For i% = 1 To UBound(a$()) : Debug a$(i%) : Next i%
ReDim a$(10) : Dim ct% a$(0) = "-1" a$(1) = "0" a$(2) = "0" a$(3) = "1" a$(4) = "1" a$(5) = "1" a$(6) = "2" a$(7) = "2" a$(8) = "3" a$(9) = "4" a$(10) = "4" QSort a$()
t# = Timer i% = UBound(a$()) : ct% = i% While i > 0 If a$(i%) = a$(i% - 1) Delete a$(i%) ct%-- EndIf i%-- Wend ReDim a$(ct%) Debug Timer - t#
For i% = 1 To UBound(a$()) : Debug a$(i%) : Next i%
|
|
|
Post by Roger Cabo on Dec 19, 2022 17:35:07 GMT 1
Thanks I will take care of this behavior. Perhaps I found a bug during all these tests. Unfortunately I have also errors when I use i% = 3000 or any other values.
could it be possible qsort crash if all the array elements in a string are empty?
$Library "gfawinx" $Library "UpdateRT" UpdateRuntime ' Patches GfaWin23.Ocx
Global arr$(0)
OpenW # 1, 100, 100, 300, 300 OcxOcx Win_1 Command cmd1 = "cmd1", 0, 0, 100, 100
Do Sleep Loop Until Me Is Nothing
Sub cmd1_Click Dim i% = 0 ReDim arr$(i%) QSort arr$() EndSub
|
|
webu
Full Member
Posts: 149
|
Post by webu on Dec 20, 2022 16:32:28 GMT 1
Yes. After all, it makes no sense to sort a completely empty string array. Qsort assumes that you know what you want! :-)
It costs no time to test this before Qsort if necessary:
Dim a$(1000000) Message "1: Array is empty = " & isArrEmpty(a$()) a$(UBound(a$())) = "something" Message "2: Array is empty = " & isArrEmpty(a$())
Function isArrEmpty(ByRef a$()) As Bool Dim i% For i = LBound a$() To UBound a$() If a$(i) Return False // content found Next Return True // no content found EndFunc
|
|
|
Post by Roger Cabo on Dec 20, 2022 19:40:30 GMT 1
Yes. After all, it makes no sense to sort a completely empty string array. Qsort assumes that you know what you want! :-)
It costs no time to test this before Qsort if necessary:
Dim a$(1000000) Message "1: Array is empty = " & isArrEmpty(a$()) a$(UBound(a$())) = "something" Message "2: Array is empty = " & isArrEmpty(a$())
Function isArrEmpty(ByRef a$()) As Bool Dim i% For i = LBound a$() To UBound a$() If a$(i) Return False // content found Next Return True // no content found EndFunc
A string... if it contains values or not it's a valid string anyway.. The other string functions didn't crash as well.. I would recommended that Sjouke should fix this for his next update hopefully.. if hes able to.
|
|
webu
Full Member
Posts: 149
|
Post by webu on Dec 20, 2022 22:40:38 GMT 1
Yes, it's a bug, that's true. Either he fixes it or writes it in the help as a feature to train us users! :-)
I always have a variable for exactly such cases, which contains the number of valid field elements. Therefore I always need a simple
If booksN do_something_with_the_arr(books$(),booksN)
That's why I don't need a Ubound or Lbound.
|
|