Skip to content

Instantly share code, notes, and snippets.

@leque
Last active October 8, 2021 14:15
Show Gist options
  • Save leque/4bc71093ab3394ef0604c8f209274b3f to your computer and use it in GitHub Desktop.
Save leque/4bc71093ab3394ef0604c8f209274b3f to your computer and use it in GitHub Desktop.
# visualize "Is this the simplest (and most surprising) sorting algorithm ever?" https://arxiv.org/abs/2110.01111
require 'curses'
def show_array(i, j, arr)
Curses.clear
Curses.setpos(i, 0)
Curses.addstr('i')
Curses.setpos(j, 1)
Curses.addstr('j')
arr.each_with_index do |x, idx|
Curses.setpos(idx, 3)
Curses.addstr('*' * x)
end
Curses.refresh
end
sleep_sec = 0.05
begin
Curses.init_screen
arr = (1..15).to_a.shuffle
show_array(0, 0, arr)
arr.length.times do |i|
arr.length.times do |j|
factor = 1
show_array(i, j, arr)
sleep(sleep_sec)
if arr[i] < arr[j] then
arr[i], arr[j] = arr[j], arr[i]
factor = 5
end
show_array(i, j, arr)
sleep(sleep_sec * factor)
end
end
sleep(5)
ensure
Curses.close_screen
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment