Sorting option(quick/merge) for arrays.

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Sorting option(quick/merge) for arrays.

Post by skywalk »

Hi,
I would like the sort option of either QuickSort (unstable, in-place) or MergeSort for arrays.
I've coded a MergeSort routine in VB6, but it can't be faster than you guys. :)
Last edited by skywalk on Tue Jul 08, 2014 12:23 am, edited 1 time in total.
jamba
Enthusiast
Enthusiast
Posts: 144
Joined: Fri Jan 15, 2010 2:03 pm
Location: Triad, NC
Contact:

Re: Sorting option(quick/merge) for arrays.

Post by jamba »

what sort routine do the internal sortarray
and sortstructuredarray use?
-Jon

Fedora user
But I work with Win7
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Sorting option(quick/merge) for arrays.

Post by Fred »

It uses MergeSort IIRC. What's the problem with these functions ?
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Sorting option(quick/merge) for arrays.

Post by skywalk »

Ahh, even better Fred,
I am new and since I couldn't find reference to the sort algorithm I took the forum answer below to be true.

http://www.purebasic.fr/english/viewtop ... =7&t=40995

I am translating large VB6 projects and I'm not near the details of my sorting requirements. But, I use a form of mergesort mostly to preserve the pointer of multi-dimensional arrays sorted elements. I use quicksorts less frequently.

Thanks for clarifying.
So is Quicksort ever used?
Maybe in your Maps where duplicates are not required?
Last edited by skywalk on Tue Jul 08, 2014 12:23 am, edited 1 time in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Comtois
Addict
Addict
Posts: 1431
Joined: Tue Aug 19, 2003 11:36 am
Location: Doubs - France

Re: Sorting option(quick/merge) for arrays.

Post by Comtois »

Freak wrote:Since PB 4.30, SortStructuredList (and SortList) use Mergesort which is a stable sort, so if you first sort all the list by titles and then again by album you will get a list which is sorted by album and each album is sorted by title.

Note that this does not work with Arrays, as SortArray uses Quicksort which is unstable. (ie the sorting of the secondary key would be lost)
See here
Please correct my english
http://purebasic.developpez.com/
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Sorting option(quick/merge) for arrays.

Post by Fred »

My bad, it's QuickSort for Array and MergeSort for lists.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Sorting option(quick/merge) for arrays.

Post by skywalk »

Okay?
So who is correct?
I guess I will do a test to prove stability, but lots to do and a manual/forum update would be faster.
Last edited by skywalk on Tue Jul 08, 2014 12:23 am, edited 1 time in total.
Foz
Addict
Addict
Posts: 1359
Joined: Tue Nov 13, 2007 12:42 pm
Location: Manchester, UK

Re: Sorting option(quick/merge) for arrays.

Post by Foz »

Fred wrote:My bad, it's QuickSort for Array and MergeSort for lists.
Freak knows the system better than you do! :lol:

I'd watch out for him, he might just take over! ;)
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Sorting option(quick/merge) for arrays.

Post by Trond »

For unstructured arrays it doesn't matter if the sort is stable or not (just think about it).

SortStructuredArray() is not stable.

Is your arrays very large, so that an efficient sort is required?
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Sorting option(quick/merge) for arrays.

Post by freak »

Sort(Structured)Array: QuickSort
Sort(Structured)List: MergeSort

The underlying algorithm is intentionally undocumented in the manual to give us the freedom to change the implementation if the need arises (for example LinkedList sort used QuickSort too somewhen in the past).

If you have to rely on features of the implementation such as stability it is better to do your own sort functions so you can have total control over it.
quidquid Latine dictum sit altum videtur
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Sorting option(quick/merge) for arrays.

Post by Demivec »

skywalk wrote:Okay?
So who is correct?
I guess I will do a test to prove stability, but lots to do and a manual/forum update would be faster.
I have a PureBasic MergeSort (stable and in place) that is written for dynamic arrays (structured). If you would like it just let me know a few specifics about your needs and I can modify it so it will to make sure it's in line with what you need.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Sorting option(quick/merge) for arrays.

Post by skywalk »

Yeah, I knew someone was going to say that.
Why require stable sorts on single dimensioned arrays?
Stability is not always required with a simple array.
But it is for my multi-dimensional arrays.
Yes, the data is large. Is data ever small? :)

Anyway, with PB, I have access to Linked Lists, so I am considering those for my conversion of VB6 projects since they have built-in sort stability and copy assignment.
I just need clarification of these things before re-designing.
Freak or Fred or Forum or me.
Note, I put me last. :)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Sorting option(quick/merge) for arrays.

Post by Demivec »

Even though skywalk is taking another route (with linked-lists), for those who are interested I've prepared and posted a stable in-place mergesort that can be used with arrays here.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Sorting option(quick/merge) for arrays.

Post by skywalk »

Really freak?
It doesn't sound like you are coding as if your customer is a raging psychopath!!!???

How about making an option for the psychopath, I mean customer, to select the appropriate method?
You can control the default setting.
Keeping things hidden or undocumented and potentially changing them in future revs is just not fun.
I don't want to interrupt your trail blazing speed either, but is this in question?

Thanks very much, Demivec, I didn't see your offer until now.
My mergesort is not in ASM so it probably can't compare in speed.
I'll definitely look at your implementation before final design.
Last edited by skywalk on Tue Jul 08, 2014 12:23 am, edited 1 time in total.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Sorting option(quick/merge) for arrays.

Post by Demivec »

skywalk wrote:Thanks very much, Demivec, I didn't see your offer until now.
My mergesort is not in ASM so it probably can't compare in speed.
I'll definitely look at your implementation before final design.
My mergesort isn't in ASM either. It is roughly 3 - 5 times slower than PureBasic's QuickSort. I did the test with 50000 items in a structured array, it took 16.6 ms for PB's QuickSort and 70.4 ms for my mergesort. Because of the overhead (recursive procedure calls) it tends to increase in time according to the number of things in the array. For instance, with 5000000 it was about 8.5x slower.
Post Reply