Created
April 24, 2017 20:55
-
-
Save larstvei/0c7c36e2dac5585ab8eb9d7c89625c6f to your computer and use it in GitHub Desktop.
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
(ns inf1010-oblig4.core | |
(:require [clojure.core.reducers :as r])) | |
(defn split [xs x] | |
(split-with (comp neg? #(compare % x)) xs)) | |
(defn add-sorted [xs x] | |
(let [[smaller larger] (split xs x)] | |
(reduce into [] [smaller [x] larger]))) | |
(defn merge-sorted | |
([] []) | |
([xs ys] (merge-sorted xs ys [])) | |
([xs ys res] | |
(let [x (first xs) | |
y (first ys)] | |
(cond (empty? xs) (into res ys) | |
(empty? ys) (into res xs) | |
(neg? (compare x y)) (recur (rest xs) ys (conj res x)) | |
:else (recur xs (rest ys) (conj res y)))))) | |
(defn single-threaded-sort [xs] | |
(reduce add-sorted [] xs)) | |
(defn threaded-sort [xs n] | |
(let [chunk-size (-> xs count (quot n) inc)] | |
(r/fold chunk-size merge-sorted add-sorted xs))) | |
(let [[threads in-file out-file] *command-line-args* | |
words (->> in-file slurp clojure.string/split-lines rest (into []))] | |
(->> (threaded-sort words (Integer. threads)) | |
time (clojure.string/join "\n") (spit out-file))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment