Last active
September 22, 2016 09:31
-
-
Save CliffordAnderson/bf6997360d784b4a61e5 to your computer and use it in GitHub Desktop.
Quicksort in XQuery
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--reference implementation in Haskell | |
qsort' :: (Ord a) => [a] -> [a] | |
qsort' [] = [] | |
qsort' (x:xs) = qsort' smaller ++ [x] ++ qsort' larger | |
where larger = [a | a <- xs, a > x] | |
smaller = [b | b <- xs, b <= x] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.0"; | |
declare function local:qsort($nums as xs:integer*) as xs:integer* | |
{ | |
let $pivot := $nums[1] | |
let $smaller := local:qsort($nums[position()=(2 to last())][. < $pivot]) | |
let $larger := local:qsort($nums[position()=(2 to last())][. >= $pivot]) | |
where fn:not(fn:empty($pivot)) | |
return ($smaller, $pivot, $larger) | |
}; | |
local:qsort((9,1,3,7,2,6,2,4,6,5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
(: No FLWOR needed :) | |
declare function local:qsort($nums as xs:integer*) as xs:integer* | |
{ | |
if (fn:not(fn:empty($nums))) then | |
( | |
local:qsort(fn:tail($nums)[. < fn:head($nums)]), | |
fn:head($nums), | |
local:qsort(fn:tail($nums)[. >= fn:head($nums)]) | |
) | |
else () | |
}; | |
local:qsort((9,1,3,7,2,6,2,4,6,5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
(: Using fn:head() and fn:tail() from XQuery 3.1 :) | |
declare function local:qsort($nums as xs:integer*) as xs:integer* | |
{ | |
let $pivot := fn:head($nums) | |
let $smaller := local:qsort(fn:tail($nums)[. < $pivot]) | |
let $larger := local:qsort(fn:tail($nums)[. >= $pivot]) | |
where fn:not(fn:empty($pivot)) | |
return ($smaller, $pivot, $larger) | |
}; | |
local:qsort((9,1,3,7,2,6,2,4,6,5)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xquery version "3.1"; | |
declare function local:qsort($nums as xs:integer*) as xs:integer* | |
{ | |
let $pivot := $nums[1] | |
let $smaller := local:qsort($nums[position()=(2 to last())][. < $pivot]) | |
let $larger := local:qsort($nums[position()=(2 to last())][. >= $pivot]) | |
where fn:not(fn:empty($pivot)) | |
return ($smaller, $pivot, $larger) | |
}; | |
let $rng := fn:random-number-generator('permute') | |
let $nums := $rng('permute')(1 to 100) | |
return local:qsort($nums) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment