Hacker News new | ask | show | jobs
by umanwizard 1453 days ago
Not true.

Clojure code:

    (defn swap [items i j]
      (assoc items
             i (items j)
             j (items i)))
    
    (defn weirdsort [items]
      (let [ct (count items)
            indices (for [i (range ct)
                          j (range ct)]
                      [i j])]
        (reduce (fn [items [i j]]
                  (let [ith (nth items i)
                        jth (nth items j)]
                    (if (< ith jth)
                      (swap items i j)
                      items)))
                items indices))
      )
Then in the repl:

    my-namespace> (def items (shuffle (range 20)))
    #'my-namespace/items
    my-namespace> items
    [19 0 13 16 14 18 15 7 10 17 9 11 6 1 3 4 12 2 5 8]
    my-namespace> (weirdsort items)
    [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19]
1 comments

Sorry I meant to write ascending. It is weird that it swaps when i < j, but the list ends up ascending