Download code

From LiteratePrograms

Jump to: navigation, search

Back to Quicksort_(Haskell)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

qsort.hs

 1 {- Copyright (c) 2010 the authors listed at the following URL, and/or
 2 the authors of referenced articles or incorporated external code:
 3 http://en.literateprograms.org/Quicksort_(Haskell)?action=history&offset=20091006002328
 4 
 5 Permission is hereby granted, free of charge, to any person obtaining
 6 a copy of this software and associated documentation files (the
 7 "Software"), to deal in the Software without restriction, including
 8 without limitation the rights to use, copy, modify, merge, publish,
 9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Retrieved from: http://en.literateprograms.org/Quicksort_(Haskell)?oldid=16535
25 -}
26 
27 module Main where
28 
29 import System
30 import List( sort )
31 import Random
32 
33 
34 qsort1 :: Ord a => [a] -> [a]
35 qsort1 []     = []
36 qsort1 (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater
37     where
38         lesser  = [ y | y <- xs, y < p ]
39         greater = [ y | y <- xs, y >= p ]
40 
41 qsort2 :: Ord a => [a] -> [a]
42 qsort2 []     = []
43 qsort2 (x:xs) = qsort2 lesser ++ equal ++ qsort2 greater
44     where
45         (lesser,equal,greater) = part x xs ([],[x],[])
46 
47 part :: Ord a => a -> [a] -> ([a],[a],[a]) -> ([a],[a],[a])
48 part _ [] (l,e,g) = (l,e,g)
49 part p (x:xs) (l,e,g)
50     | x > p     = part p xs (l,e,x:g)
51     | x < p     = part p xs (x:l,e,g)
52     | otherwise = part p xs (l,x:e,g)
53 
54 
55 qsort3 :: Ord a => [a] -> [a]
56 qsort3 x = qsort3' x []
57 
58 qsort3' [] y     = y
59 
60 qsort3' [x] y    = x:y
61 
62 qsort3' (x:xs) y = part xs [] [x] []  
63     where
64         part [] l e g = qsort3' l (e ++ (qsort3' g y))
65         part (z:zs) l e g 
66             | z > x     = part zs l e (z:g) 
67             | z < x     = part zs (z:l) e g 
68             | otherwise = part zs l (z:e) g
69 
70 
71 
72 -- Testing

73 randSeq n gen = take n (randomRs (0,n) gen)
74 
75 testQsort q n = do
76         gen <- getStdGen
77         print (last (q (randSeq n gen)))  
78 
79 selectSort ["1"] = qsort1        
80 selectSort ["2"] = qsort2        
81 selectSort ["3"] = qsort3        
82 selectSort _     = List.sort 
83 
84         
85 main :: IO ()        
86 main = do (x:xs) <- getArgs
87           let listlen = read x :: Int
88               q = selectSort xs
89           testQsort q listlen
90 
91 
92 


Views
Personal tools