Download code

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 {- The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Quicksort_(Haskell)?oldid=19195
14 -}
15 
16 qsort1 :: Ord a => [a] -> [a]
17 qsort1 []     = []
18 qsort1 (p:xs) = qsort1 lesser ++ [p] ++ qsort1 greater
19     where
20         lesser  = [ y | y <- xs, y < p ]
21         greater = [ y | y <- xs, y >= p ]
22 
23 qsort2 :: Ord a => [a] -> [a]
24 qsort2 []     = []
25 qsort2 (x:xs) = qsort2 lesser ++ equal ++ qsort2 greater
26     where
27         (lesser,equal,greater) = part x xs ([],[x],[])
28        
29 part :: Ord a => a -> [a] -> ([a],[a],[a]) -> ([a],[a],[a])
30 part _ [] (l,e,g) = (l,e,g)
31 part p (x:xs) (l,e,g)
32     | x > p     = part p xs (l,e,x:g)
33     | x < p     = part p xs (x:l,e,g)
34     | otherwise = part p xs (l,x:e,g)
35 qsort3 :: Ord a => [a] -> [a]
36 qsort3 x = qsort3' x []
37 
38 qsort3' [] y     = y
39 qsort3' [x] y    = x:y
40 qsort3' (x:xs) y = part xs [] [x] []  
41     where
42         part [] l e g = qsort3' l (e ++ (qsort3' g y))
43         part (z:zs) l e g 
44             | z > x     = part zs l e (z:g) 
45             | z < x     = part zs (z:l) e g 
46             | otherwise = part zs l (z:e) g


hijacker
hijacker
hijacker
hijacker