daily pastebin goal
77%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. :: ********** 14. insertion-sort **********
  2. |=  to-sort=(list @)
  3. =/  sorted=(list @)  ~
  4. |-
  5. ?~  to-sort                                       :: if nothing left to sort
  6.   sorted                                          :: return sorted list
  7. ?~  sorted                                        :: if sorted is empty list
  8.   $(sorted [i.to-sort sorted], to-sort ^-((list @) t.to-sort))      :: recurse w to-sort[0] removed, inserted into sorted
  9. ?:  (lte i.to-sort i.sorted)                      :: if to-sort[0] <= sorted[0]
  10.   $(sorted [i.to-sort sorted], to-sort t.to-sort) :: prepend to-sort[0] to sorted[0]
  11. :: Else, item to sort needs to be compared to next item in the sorted list and checked until it finds position
  12. |-
  13. =/  inner-sorted=(list @)  sorted                  :: creating a copy of sorted for inner loop so doesn't change in outer
  14. ?:  (lte i.to-sort i.inner-sorted)
  15.   [i.to-sort inner-sorted]
  16. ?~  t.inner-sorted
  17.   [i.inner-sorted i.to-sort t.inner-sorted]
  18. (sorted [i.inner-sorted $(inner-sorted t.inner-sorted)], to-sort t.to-sort)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top