[4.41] Bug in ArraySort (solved)

Just starting out? Need help? Post your questions and find answers here.
neotoma
User
User
Posts: 84
Joined: Sun Dec 14, 2003 6:38 pm
Location: Germany, Mechernich
Contact:

[4.41] Bug in ArraySort (solved)

Post by neotoma »

I found a very annoying Bug in PB4.41 as i used the SortArray-Function.
It allways add a Zero Entry and loose the highest one.

Example:

Code: Select all

;PB 4.41-Bug

RandomSeed( ElapsedMilliseconds() )

#ITEMS=6

;Fill Array With Random Values:
Dim idx.i(#ITEMS)
For i = 1 To #ITEMS
  idx(i-1) = i*10; Random(10000)
Next

Debug "Before Sorting:"
Debug "---------------"
;Print Result
For i = 0 To #ITEMS-1
  Debug( Str( idx(i) ) )
Next

;Sort
SortArray(idx(),#PB_Sort_Ascending)

Debug "After Sorting:"
Debug "---------------"
;Print Result
For i = 0 To #ITEMS-1
  Debug( Str( idx(i) ) )
Next


;With Sort we loose the highest number, and get a zero as lowest 
;Also if ther are no zero value in the array.
Mike
Last edited by neotoma on Thu Mar 18, 2010 9:43 pm, edited 1 time in total.
User avatar
Arctic Fox
Enthusiast
Enthusiast
Posts: 609
Joined: Sun Dec 21, 2008 5:02 pm
Location: Aarhus, Denmark

Re: [4.41] Bug in ArraySort

Post by Arctic Fox »

Well, you have seven items in the array, the last one is zero.

Try this and you will find out

Code: Select all

Debug "After Sorting:"
Debug "---------------"
;Print Result
For i = 0 To #ITEMS
  Debug( Str( idx(i) ) )
Next
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: [4.41] Bug in ArraySort

Post by IdeasVacuum »

...just replace "#ITEMS - 1" with "#ITEMS" in both loops. Your code seems to be attempting to avoid collecting the value of the first index (index zero). If there is a good reason to do that, then this is how:

Code: Select all

#ITEMS=6

;Fill Array With Random Values:
Dim idx.i(#ITEMS)
For i = 1 To #ITEMS
  idx(i) = i*10
Next

Debug "Before Sorting:"
Debug "---------------"
;Print Result
For i = 1 To #ITEMS
  Debug( Str( idx(i) ) )
Next

;Sort
SortArray(idx(),#PB_Sort_Ascending)

Debug "After Sorting:"
Debug "---------------"
;Print Result
For i = 1 To #ITEMS
  Debug( Str( idx(i) ) )
Next
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
neotoma
User
User
Posts: 84
Joined: Sun Dec 14, 2003 6:38 pm
Location: Germany, Mechernich
Contact:

Re: [4.41] Bug in ArraySort

Post by neotoma »

Yes, you are right - was my fault.

Thanks
Post Reply