Download code

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.
1213 Retrieved from: http://en.literateprograms.org/Quicksort_(Haskell)?oldid=19195
14 -}1516qsort1::Orda=>[a]->[a]17qsort1[]=[]18qsort1(p:xs)=qsort1lesser++[p]++qsort1greater19where20lesser=[y | y<-xs, y<p]21greater=[y | y<-xs, y>=p]2223qsort2::Orda=>[a]->[a]24qsort2[]=[]25qsort2(x:xs)=qsort2lesser++equal++qsort2greater26where27(lesser,equal,greater)=partxxs([],[x],[])2829part::Orda=>a->[a]->([a],[a],[a])->([a],[a],[a])30part_[](l,e,g)=(l,e,g)31partp(x:xs)(l,e,g)32 | x>p=partpxs(l,e,x:g)33 | x<p=partpxs(x:l,e,g)34 | otherwise=partpxs(l,x:e,g)35qsort3::Orda=>[a]->[a]36qsort3x=qsort3'x[]3738qsort3'[]y=y39qsort3'[x]y=x:y40qsort3'(x:xs)y=partxs[][x][]41where42part[]leg=qsort3'l(e++(qsort3'gy))43part(z:zs)leg44 | z>x=partzsle(z:g)45 | z<x=partzs(z:l)eg46 | otherwise=partzsl(z:e)g