Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function bubbleSort(t)
- for i, _ in pairs(t) do
- firstindex = i
- break
- end
- temp = nil
- for i = firstindex, (#t-1) do
- for j = firstindex, (#t-1) do
- if (t[j] > t[j+1]) then
- temp = t[j]
- t[j] = t[j+1]
- t[j+1] = temp
- end
- end
- end
- end
- function visualize(t)
- for i, v in pairs(t) do
- if type(v) == "number" then
- print(i, string.rep("=", v))
- end
- end
- end
- function randomize(t, st, en)
- for i=st, en do
- t[i] = math.random(st, en)
- end
- end
- mass1 = {}
- print("Before sorting:")
- randomize(mass1, 1, 50)
- visualize(mass1)
- print("After sorting:")
- bubbleSort(mass1)
- visualize(mass1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement