Skip to content

Instantly share code, notes, and snippets.

@m0n4d1
Last active May 25, 2019 17:31
Show Gist options
  • Save m0n4d1/b119f6ae707542a6abcd167848028e21 to your computer and use it in GitHub Desktop.
Save m0n4d1/b119f6ae707542a6abcd167848028e21 to your computer and use it in GitHub Desktop.
Treadmill Sort
# -------------------------
# Treadmill Sort
# -------------------------
# written in ruby 2.5.3
# recursive
# -------------------------
# reduces an array into its lowest value item
def min arr
if arr.length > 1
arr[0] < arr[1] ? min([arr[0]].concat(arr.drop(2))) : min(arr.drop(1))
else
arr[0]
end
end
def treadmill (arr, min=min(arr))
# if there are enough items to sort
if arr.length > 1
# if the first item is the smallest
if arr[0] == min
#take that item and concatenate the next round
[arr[0]].concat(treadmill(arr[1..-1]))
# if the first item is not the smallest
else
#call next round with first item moved to the end
# as no items are removed the current min will be used for the next round
treadmill((arr[1..-1]).concat([arr[0]]), min)
end
#if there are not enough items to sort
else
#return array
arr
end
end
# -------------------------
# p treadmill *array goes here*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment