Welcome to Web-News
A Web-based News Reader
Subject Multisort
From dsimcha <dsimcha@yahoo.com>
Date Sun, 6 Jul 2008 21:34:33 +0000 (UTC)
Newsgroups digitalmars.D
Attachment(s) phobosSort.dphobosSort.html

I've been working on implementing some statistics functions in D, and noticed
that it would be nice if a multisort function were in Phobos.  Such a function
would take N arrays in and sort them by the first one.  The common way to
handle something like this seems to be to make a struct with the fields, make
opCmp point to whatever field you want to sort by, and then use a regular
sorting algorithm.  This is slow and a pain.  I've implemented a few different
multisort functions, along with documentation and unit testing, and would like
to contribute them to Phobos.  I've attached the source file and the Ddoc
documentation.

phobosSort

phobosSort



T[] newVoid(T)(size_t length);
Returns a new T[], skipping initialization. Useful when fast memory allocation is necessary, and the initial values are definitely not going to be used.

void rotateRight(T)(T[] input);
Helper function for stableSelectionSort.

T[0] qsort(T...)(T data);
Unstable quick multisort. Sorts N arrays by first array. Faster than stable version, and sorts in place.

T[0] stableQsort(T...)(T data);
Stable version of qsort() function. Does not sort in place, and is about 15% slower than unstable version, so use only when necessary. Also, performance is O(N^2) on pre-sorted arrays, because choosing an intelligent pivot is difficult or impossible in a stable qsort. stableQsort creates temp variables and forwards all data to stableQsortBackEnd. However, stableQsortBackEnd is callable directly, in case the user wants to recycle pre-allocated temp space.

T[0] mergeSort(T...)(T data);
Merge multisort. Takes in N arrays and sorts them by the first array. If last argument is a ulong*, increments this by the number of swaps that would have been necessary to bubble sort the data. This is useful for calculating statistical functions like Kendall correlation. Slower than stableQsort on average, use only if you need to count the swaps for some statistical function, or if you need O(N log N) worst case performance. Like stableQsort(), front end function creates temp variables and forwards them to back end function.

void merge(T...)(T data);
Merge function for merge sort.

T[0] bubbleSort(T...)(T data);
Bubble multisort function. Useful only because it's easy to implement and test other sorting functions against. Counts swaps if last argument is a ulong*.

T[0] selectionSort(T...)(T data);
Unstable selection sort. Used for small subarrays in qsort.

T[0] stableSelectionSort(T...)(T data);
Stable selection sort. Used for small subarrays in stableQsort and mergeSort. If last argument is a ulong*, keeps track of number of swaps that would have been necessary if this were a bubble sort.


Page generated by Ddoc.

Recent messages in this thread
 
-# Multisort (Current message) dsimcha 06-Jul-2008 05:34 pm
.|# Re: Multisort bearophile 06-Jul-2008 05:47 pm
.\# Re: Multisort superdan 06-Jul-2008 05:59 pm