It is currently Fri May 24, 2013 6:21 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 35 posts ]  Go to page 1, 2, 3  Next
Author Message
 Post subject: Sorting option(quick/merge) for arrays.
PostPosted: Fri Feb 05, 2010 5:04 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
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. :)

Thanks,
Steve


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 1:49 pm 
Offline
Enthusiast
Enthusiast

Joined: Fri Jan 15, 2010 2:03 pm
Posts: 144
Location: Triad, NC
what sort routine do the internal sortarray
and sortstructuredarray use?

_________________
-Jon

Fedora user
But I work with Win7


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 2:58 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 8876
Location: France
It uses MergeSort IIRC. What's the problem with these functions ?


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 4:19 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
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.

viewtopic.php?f=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?

Thanks,
Steve

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 4:40 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Aug 19, 2003 11:36 am
Posts: 1113
Location: Doubs - France
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/


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 4:47 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 8876
Location: France
My bad, it's QuickSort for Array and MergeSort for lists.


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 4:52 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
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.

-Steve


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 5:29 pm 
Offline
Addict
Addict
User avatar

Joined: Tue Nov 13, 2007 12:42 pm
Posts: 1307
Location: Manchester, UK
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! ;)


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 5:42 pm 
Offline
Always Here
Always Here
User avatar

Joined: Mon Sep 22, 2003 6:45 pm
Posts: 7304
Location: Norway
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?

_________________
Woa, I set up a web server.


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 5:45 pm 
Offline
PureBasic Team
PureBasic Team
User avatar

Joined: Fri Apr 25, 2003 5:21 pm
Posts: 5188
Location: Germany
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.

_________________
Perl – The only language that looks the same before and after RSA encryption.
-- Keith Bostic


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 5:49 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
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.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 5:51 pm 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
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. :)

_________________
To understand recursion, you must first understand recursion. ~ unknown
I never make stupid mistakes. Only very, very clever ones. ~ John Peel


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Thu Feb 11, 2010 10:17 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
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.

_________________
Image


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Fri Feb 12, 2010 1:21 am 
Offline
Addict
Addict
User avatar

Joined: Wed Dec 23, 2009 10:14 pm
Posts: 1386
Location: Boston, MA
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.

Thanks,
Steve


Top
 Profile  
 
 Post subject: Re: Sorting option(quick/merge) for arrays.
PostPosted: Fri Feb 12, 2010 2:30 am 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 2401
Location: Utah, USA
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.

_________________
Image


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 35 posts ]  Go to page 1, 2, 3  Next

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye