On Sun, May 12, 2002 at 09:22:02PM +0100, Claus Reinke wrote:
>
> > randomise l = do
> > map snd $ sortBy compareIdx $ zip rs l
> > where
> > n = length l
> > rs = take n $ randomRs (1,n) $ mkStdGen 100
> > compareIdx (i,_) (j,_) = i `compare` j
>
> > rsort l = sort $ randomise l
This algorithm doesn't have the stable property, but it can be easily
adapted with:
> srandomise l = do
> map fst $ map snd $ sortBy compareIdx $ zip rs $ zip l ([1..] :: Integer)
> where
> n = length l
> rs = take n $ randomRs (1,n) $ mkStdGen 100
> compareIdx (i,_) (j,_) = i `compare` j
> rssort l = sort $ srandomise l
Out of curiousity I also tried:
> sxrandomise l = do
> map fst $ map snd $ sortBy compareIdx $ zip rs $ zip l ([1..] :: Integer)
> where
> rs = randoms $ mkStdGen 100
> compareIdx (i,_) (j,_) = i `compare` j
> rsxsort l = sort $ sxrandomise l
Of course if you use 100 as the seed then rsxsort does poorly on the
random_list test as it first sorts the input and then sorts the sorted
input, giving
stdin 1random_list rsxsort 21.90
func1random_list rsxsort 27.21
The results using 200 as a seed are shown below (I removed stdin/10
as they were all *s again). mergesort has the edge, but you'd expect
that as r*sort aren't going to get perfect splits. r*sort should do
better if there were lots of repeated inputs.
Input style Input length Sort data Sort algUser time
stdin 1random_list sort2.85
stdin 1random_list rsort 2.98
stdin 1random_list rssort 3.04
stdin 1random_list rsxsort 3.18
stdin 1random_list mergesort 3.02
stdin 1sortedsort31.09
stdin 1sortedrsort 2.05
stdin 1sortedrssort 2.09
stdin 1sortedrsxsort 2.20
stdin 1sortedmergesort 1.88
stdin 1revsorted sort31.17
stdin 1revsorted rsort 2.04
stdin 1revsorted rssort 2.10
stdin 1revsorted rsxsort 2.17
stdin 1revsorted mergesort 1.85
func1random_list sort0.30
func1random_list rsort 0.94
func1random_list rssort 1.05
func1random_list rsxsort 1.14
func1random_list mergesort 0.91
func1sortedsort19.28
func1sortedrsort 0.30
func1sortedrssort 0.37
func1sortedrsxsort 0.44
func1sortedmergesort 0.16
func1revsorted sort19.30
func1revsorted rsort 0.30
func1revsorted rssort 0.37
func1revsorted rsxsort 0.48
func1revsorted mergesort 0.15
func10 random_list sort3.97
func10 random_list rsort *
func10 random_list rssort *
func10 random_list rsxsort *
func10 random_list mergesort *
func10 sortedsort5873.15
func10 sortedrsort 5.25
func10 sortedrssort 5.66
func10 sortedrsxsort 6.29
func10 sortedmergesort 2.18
func10 revsorted sort5828.57
func10 revsorted rsort 5.13
func10 revsorted rssort 5.57
func10 revsorted rsxsort 6.45
func10 revsorted mergesort 2.24
___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users