Last active
August 29, 2015 14:06
-
-
Save arian/5fadb82ddbb3695bab52 to your computer and use it in GitHub Desktop.
Quicksorts
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
package main | |
import "fmt" | |
func quicksort(list []int) []int { | |
l := len(list) | |
if (l <= 1) { | |
return list | |
} | |
x := list[0] | |
smaller := make([]int, 0, l) | |
bigger := make([]int, 0, l) | |
for i := 1; i < l; i++ { | |
if list[i] > x { | |
bigger = append(bigger, list[i]) | |
} else { | |
smaller = append(smaller, list[i]) | |
} | |
} | |
smaller = quicksort(smaller) | |
bigger = quicksort(bigger) | |
return append(make([]int, 0, l), append(append(smaller, x), bigger...)...) | |
} | |
func main() { | |
fmt.Println(quicksort([]int{ 20, 4, 3, 3, 6, 1, 10 })) | |
} |
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
quicksort :: (Ord a) => [a] -> [a] | |
quicksort [] = [] | |
quicksort (x:xs) = | |
let | |
smaller = quicksort (filter (< x) xs) | |
bigger = quicksort (filter (>= x) xs) | |
in | |
smaller ++ [x] ++ bigger | |
main = putStrLn $ show (quicksort [4,5,6,3,7,8,2,20,10,3]) |
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
var c = require('mout/function/compose'); | |
var p = require('mout/function/partial'); | |
function gt(x, y) { | |
return y > x; | |
} | |
function not(b) { | |
return !b; | |
} | |
function quicksort(list) { | |
if (list.length <= 1) { | |
return list; | |
} | |
var x = list[0]; | |
var xs = list.slice(1); | |
var gtx = p(gt, x); | |
var bigger = quicksort(xs.filter(gtx)); | |
var smaller = quicksort(xs.filter(c(not, gtx))); | |
return smaller.concat(x, bigger); | |
} | |
var list = [4,2,6,7,23,7,4,3,1,6,8]; | |
console.log(quicksort(list)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment