To implement an insertion sort that sorts an array with optional block to determine sorting order, how can I improve this code? (Seeking best practices and code correctness)
def custom_insertion_sort(arr)
for i in (1..arr.length-1)
curr = arr[i]
compare = i-1
if block_given?
sorted = yield arr, compare, curr
else
sorted = arr[compare] < curr
end
while compare >= 0 && sorted
arr[compare+1] = arr[compare]
compare -= 1
if block_given?
sorted = yield arr, compare, curr
else
sorted = arr[compare] < curr
end
end
arr[compare+1] = curr
end
arr
end