A bug in SortArray?

Just starting out? Need help? Post your questions and find answers here.
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

A bug in SortArray?

Post by Armoured »

Hi

Code: Select all

Dim numbers.a(4)
numbers(0) = 4
numbers(1) = 3
numbers(2) = 1
numbers(3) = 2

SortArray(numbers(),#PB_Sort_Ascending)

For i = 0 To 3
  Debug Str(numbers(i))
Next i
I was expected an output like:
1
2
3
4

but the result is:
0
1
2
3

I used PureBasic 6.0 LTS
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: A bug in SortArray?

Post by RASHAD »

No bug

Code: Select all

Dim numbers.a(4)
numbers(0) = 4
numbers(1) = 3
numbers(2) = 1
numbers(3) = 2

SortArray(numbers(),#PB_Sort_Ascending,0,3)
For i = 0 To 3
  Debug Str(numbers(i))
Next i
Egypt my love
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: A bug in SortArray?

Post by Armoured »

Hi Reshad,
Seems that an extra value is allways inserted in the array with value 0 after the sort.
look this:

Code: Select all

Dim numbers.a(4)
numbers(0) = 4
numbers(1) = 3
numbers(2) = 1
numbers(3) = 2

SortArray(numbers(),#PB_Sort_Ascending,0,3)

For i = 0 To 4
  Debug Str(numbers(i))
Next i
I was expected an error but the output is:
1
2
3
4
0
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: A bug in SortArray?

Post by RASHAD »

Why
For i = 0 To 4
If you only need from 0 to 3 = 4 items

Or
Use
Dim numbers.a(3)

Still NO BUG :)
Egypt my love
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: A bug in SortArray?

Post by Armoured »

From the PureBasic manual on SortArray() parameters: "Start, End (optional) The index of the first and last element in the array that should be sorted. If these parameters are not specified, then the whole array is sorted."

As I understand it the whole array is the one without the extra value set to zero and for this reason this is a bug :D
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: A bug in SortArray?

Post by RASHAD »

PB DIM always + one extra item
But if you are going to argue and you belief it's a bug GO and post a Bug report
Egypt my love
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: A bug in SortArray?

Post by Armoured »

RASHAD wrote: Tue Sep 20, 2022 1:56 pm PB DIM always + one extra item
The problem is not the Dim but the extra element that is also ordered and inserted in the resulting sorted array
RASHAD wrote: Tue Sep 20, 2022 1:56 pm But if you are going to argue and you belief it's a bug GO and post a Bug report
In the "Bugs" section of the forum I read that first I have to post any bugs found here and only after eventually in the right section
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: A bug in SortArray?

Post by NicTheQuick »

When you write

Code: Select all

Dim numbers.a(4)
it will create an array with 5 elements, not 4.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
jacdelad
Addict
Addict
Posts: 1432
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: A bug in SortArray?

Post by jacdelad »

...plus there's value assigned to element 4, so it's 0 from the beginning.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Armoured
Enthusiast
Enthusiast
Posts: 346
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Re: A bug in SortArray?

Post by Armoured »

Ok thanks to all!
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8425
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: A bug in SortArray?

Post by netmaestro »

Simple, numbers(4) has nothing in it and you're sorting the whole array so the sort relocates it to the bottom of the array.

SortArray(numbers(),#PB_Sort_Ascending, 0, 3) matches the way you filled the array and will give you the result you want.
BERESHEIT
Post Reply