खोज…


सम्मिलन सॉर्ट

insert :: Ord a => a -> [a] -> [a]
insert x [] = [x]
insert x (y:ys) | x < y     = x:y:ys
                | otherwise = y:(insert x ys)

isort :: Ord a => [a] -> [a]
isort [] = []
isort (x:xs) = insert x (isort xs)

उदाहरण का उपयोग करें:

> isort [5,4,3,2,1]

परिणाम:

[1,2,3,4,5]

मर्ज़ सॉर्ट

दो आदेशित सूचियों के विलय का आदेश दिया

डुप्लिकेट को संरक्षित करना:

merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge (x:xs) (y:ys) | x <= y    = x:merge xs (y:ys)
                    | otherwise = y:merge (x:xs) ys

टॉप-डाउन संस्करण:

msort :: Ord a => [a] -> [a]
msort [] = []
msort [a] = [a]
msort xs = merge (msort (firstHalf xs)) (msort (secondHalf xs))

firstHalf  xs = let { n = length xs } in take (div n 2) xs
secondHalf xs = let { n = length xs } in drop (div n 2) xs

इसे स्पष्टता के लिए इस तरह परिभाषित किया गया है, दक्षता के लिए नहीं।

उदाहरण का उपयोग करें:

> msort [3,1,4,5,2]

परिणाम:

[1,2,3,4,5]

नीचे-ऊपर संस्करण:

msort [] = []
msort xs = go [[x] | x <- xs]
    where
    go [a] = a
    go xs = go (pairs xs)
    pairs (a:b:t) = merge a b : pairs t
    pairs t = t

जल्दी से सुलझाएं

qsort :: (Ord a) => [a] -> [a]
qsort [] = []
qsort (x:xs) = qsort [a | a <- xs, a < x]
                      ++ [x] ++
               qsort [b | b <- xs, b >= x]

बबल शॅाट

bsort :: Ord a => [a] -> [a]
bsort s = case bsort' s of
               t | t == s    -> t
                 | otherwise -> bsort t
  where bsort' (x:x2:xs) | x > x2    = x2:(bsort' (x:xs))
                         | otherwise = x:(bsort' (x2:xs))
        bsort' s = s

क्रमचय क्रमबद्धता

जिसे बोगोसॉर्ट के नाम से भी जाना जाता है।

import Data.List (permutations)

sorted :: Ord a => [a] -> Bool
sorted (x:y:xs) = x <= y && sorted (y:xs)
sorted _        = True

psort :: Ord a => [a] -> [a]
psort = head . filter sorted . permutations

अत्यधिक अक्षम (आज के कंप्यूटरों पर)।

चयन छांटना

चयन क्रम सूची के खाली होने तक, बार-बार न्यूनतम तत्व का चयन करता है।

import Data.List (minimum, delete)

ssort :: Ord t => [t] -> [t]
ssort [] = []
ssort xs = let { x = minimum xs } 
           in  x : ssort (delete x xs)


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow