GO-Ve01
ServicesDevelopersBooksDictionaryPapersWikidataEncyclopediaSearchBabelCareersDeck

Tournament sort

Tournament sort is a sorting algorithm. It improves upon the naive selection sort by using a priority queue to find the next element in the sort. In the naive selection sort, it takes O(n) operations to select the next element of n elements; in a tournament sort, it takes O(log n) operations (after building the initial tournament in O(n)). Tournament sort is a variation of heapsort. Source: e01 Encyclopedia. Licence: CC BY-SA 4.0.

Tournament sort is a sorting algorithm. It improves upon the naive selection sort by using a priority queue to find the next element in the sort. In the naive selection sort, it takes O(n) operations to select the next element of n elements; in a tournament sort, it takes O(log n) operations (after building the initial tournament in O(n)). Tournament sort is a variation of heapsort. import Data.Tree -- | Adapted from `TOURNAMENT-SORT!` in the Stepanov and Kershenbaum report. tournamentSort :: Ord t where go [] = [] go trees = (rootLabel winner) : (go (subForest winner)) where winner = playTournament trees -- | Adapted from `TOURNAMENT!` in the Stepanov and Kershenbaum report playTournament :: Ord t playTournament trees = playTournament (playRound trees []) -- | Adapted from `TOURNAMENT-ROUND!` in the Stepanov and Kershenbaum report playRound :: Ord t playRound [tree] done = tree:done playRound (tree0:tree1:trees) done = playRound trees (winner:done) where winner = playGame tree0 tree1 -- | Adapted from `TOURNAMENT-PLAY!` in the Stepanov and Kershenbaum report playGame :: Ord t => Tree t -- ^ Input: ... -> Tree t -- ^ ... two trees -> Tree t -- ^ Result: `promote winner loser`, where `winner` is -- the tree with the *lesser* root of the two inputs playGame tree1 tree2 | rootLabel tree1 Tree t -- ^ The `loser` -> Tree t -- ^ Result: a tree whose root is the root of `winner` -- and whose children are: -- * `loser`, -- * all the children of `winner` promote winner loser = Node { rootLabel = rootLabel winner, subForest = loser : subForest winner} main :: IO () main = print $ tournamentSort testList where testList = [0, 1, 2, 3, 4, 5]

source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Tournament sort". 1772461618. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1341321416

JSON record · API collection · BibTeX · RIS · CSL-JSON · citation policy

Save to pass ·