Advertisement
xX-AAAAAAAAAA-Xx

ComputerCraft Sorting Algorithims

Aug 20th, 2024 (edited)
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.49 KB | None | 0 0
  1. -- Define the width and height of the screen
  2. local screenWidth, screenHeight = term.getSize()
  3.  
  4. -- Create and store the list of numbers 1 to 19
  5. local numberList = {}
  6. for i = 1, 19 do
  7.     table.insert(numberList, i)
  8. end
  9.  
  10. -- Function to draw a vertical bar
  11. local function drawBar(x, height, color)
  12.     term.setTextColor(color or colors.white)
  13.     for y = 1, height do
  14.         term.setCursorPos(x, screenHeight - y + 1)
  15.         term.write("|")
  16.     end
  17. end
  18.  
  19. -- Function to draw buttons on the left side of the screen
  20. local function drawButtons()
  21.     term.setCursorPos(5, 1)
  22.     term.write("X")
  23.  
  24.     term.setCursorPos(5, 3)
  25.     term.write("Shuffle")
  26.  
  27.     term.setCursorPos(5, 5)
  28.     term.write("BubbleSort")
  29.  
  30.     term.setCursorPos(5, 7)
  31.     term.write("SelectionSort")
  32.  
  33.     term.setCursorPos(5, 9)
  34.     term.write("InsertionSort")
  35.  
  36.     term.setCursorPos(5, 11)
  37.     term.write("MergeSort")
  38.  
  39.     term.setCursorPos(5, 13)
  40.     term.write("QuickSort")
  41. end
  42.  
  43. -- Function to check if the mouse click is within a button area
  44. local function isButtonClicked(x, y, buttonY)
  45.     return x >= 5 and x <= 14 and y == buttonY
  46. end
  47.  
  48. -- Function to pause execution for visualization
  49. local function pause(seconds)
  50.     local endTime = os.clock() + seconds
  51.     while os.clock() < endTime do
  52.         os.queueEvent("monitor_touch")
  53.         os.pullEvent()
  54.     end
  55. end
  56.  
  57. -- Function to play a note with frequency based on position
  58. local function playNoteBasedOnPosition(position)
  59.     local speaker = peripheral.find("speaker")
  60.     if speaker then
  61.         local pitch = 0.5 + (position / #numberList)
  62.         speaker.playNote("harp", 1, pitch)
  63.     end
  64. end
  65.  
  66. -- Function to shuffle the list
  67. local function shuffleList()
  68.     for i = 1, #numberList - 1 do
  69.         local j = math.random(i + 1, #numberList)
  70.         numberList[i], numberList[j] = numberList[j], numberList[i]
  71.     end
  72. end
  73.  
  74. -- Function to draw bars with the current number list
  75. local function drawBars()
  76.     for i = 1, 19 do
  77.         local barHeight = numberList[i]
  78.         drawBar(20 + i, barHeight)
  79.     end
  80. end
  81.  
  82. -- Sorting Algorithms
  83.  
  84. -- Bubble Sort
  85. local function bubbleSort()
  86.     local n = #numberList
  87.     for i = 1, n - 1 do
  88.         for j = 1, n - i do
  89.             term.clear()
  90.             drawBars()
  91.             drawButtons()
  92.             drawBar(20 + j, numberList[j], colors.green)
  93.             drawBar(20 + j + 1, numberList[j + 1], colors.green)
  94.             playNoteBasedOnPosition(j)
  95.             pause(0.05)
  96.             if numberList[j] > numberList[j + 1] then
  97.                 drawBar(20 + j, numberList[j], colors.red)
  98.                 drawBar(20 + j + 1, numberList[j + 1], colors.red)
  99.                 playNoteBasedOnPosition(j)
  100.                 pause(0.05)
  101.                 numberList[j], numberList[j + 1] = numberList[j + 1], numberList[j]
  102.             end
  103.             term.clear()
  104.         end
  105.     end
  106. end
  107.  
  108. -- Selection Sort
  109. local function selectionSort()
  110.     local n = #numberList
  111.     for i = 1, n - 1 do
  112.         local minIndex = i
  113.         for j = i + 1, n do
  114.             term.clear()
  115.             drawBars()
  116.             drawButtons()
  117.             drawBar(20 + j, numberList[j], colors.green)
  118.             drawBar(20 + minIndex, numberList[minIndex], colors.green)
  119.             playNoteBasedOnPosition(j)
  120.             pause(0.05)
  121.             if numberList[j] < numberList[minIndex] then
  122.                 minIndex = j
  123.             end
  124.             term.clear()
  125.         end
  126.         if minIndex ~= i then
  127.             drawBar(20 + i, numberList[i], colors.red)
  128.             drawBar(20 + minIndex, numberList[minIndex], colors.red)
  129.             playNoteBasedOnPosition(minIndex)
  130.             pause(0.05)
  131.             numberList[i], numberList[minIndex] = numberList[minIndex], numberList[i]
  132.         end
  133.         term.clear()
  134.     end
  135. end
  136.  
  137. -- Insertion Sort
  138. local function insertionSort()
  139.     local n = #numberList
  140.     for i = 2, n do
  141.         local key = numberList[i]
  142.         local j = i - 1
  143.         while j > 0 and numberList[j] > key do
  144.             term.clear()
  145.             drawBars()
  146.             drawButtons()
  147.             drawBar(20 + j, numberList[j], colors.green)
  148.             drawBar(20 + j + 1, numberList[j + 1], colors.red)
  149.             playNoteBasedOnPosition(j)
  150.             pause(0.05)
  151.             numberList[j + 1] = numberList[j]
  152.             j = j - 1
  153.             term.clear()
  154.         end
  155.         numberList[j + 1] = key
  156.     end
  157. end
  158.  
  159. local function merge(low, mid, high)
  160.     local n1 = mid - low + 1
  161.     local n2 = high - mid
  162.     local L = {}
  163.     local R = {}
  164.     for i = 1, n1 do
  165.         L[i] = numberList[low + i - 1]
  166.     end
  167.     for j = 1, n2 do
  168.         R[j] = numberList[mid + j]
  169.     end
  170.     local i = 1
  171.     local j = 1
  172.     local k = low
  173.     while i <= n1 and j <= n2 do
  174.         term.clear()
  175.         drawBars()
  176.         drawButtons()
  177.         drawBar(20 + k, numberList[k], colors.green)
  178.         playNoteBasedOnPosition(k)
  179.         pause(0.05)
  180.         if L[i] <= R[j] then
  181.             numberList[k] = L[i]
  182.             i = i + 1
  183.         else
  184.             numberList[k] = R[j]
  185.             j = j + 1
  186.         end
  187.         k = k + 1
  188.         term.clear()
  189.     end
  190.     while i <= n1 do
  191.         numberList[k] = L[i]
  192.         i = i + 1
  193.         k = k + 1
  194.     end
  195.     while j <= n2 do
  196.         numberList[k] = R[j]
  197.         j = j + 1
  198.         k = k + 1
  199.     end
  200. end
  201.  
  202. -- Merge Sort
  203. local function mergeSort(low, high)
  204.     if low < high then
  205.         local mid = math.floor((low + high) / 2)
  206.         mergeSort(low, mid)
  207.         mergeSort(mid + 1, high)
  208.         merge(low, mid, high)
  209.     end
  210. end
  211.  
  212.  
  213. local function partition(low, high)
  214.     local pivot = numberList[high]
  215.     local i = low - 1
  216.     for j = low, high - 1 do
  217.         term.clear()
  218.         drawBars()
  219.         drawButtons()
  220.         drawBar(20 + j, numberList[j], colors.green)
  221.         playNoteBasedOnPosition(j)
  222.         pause(0.05)
  223.         if numberList[j] < pivot then
  224.             i = i + 1
  225.             drawBar(20 + i, numberList[i], colors.red)
  226.             drawBar(20 + j, numberList[j], colors.red)
  227.             pause(0.05)
  228.             numberList[i], numberList[j] = numberList[j], numberList[i]
  229.         end
  230.     end
  231.     numberList[i + 1], numberList[high] = numberList[high], numberList[i + 1]
  232.     return i + 1
  233. end
  234.  
  235. -- Quick Sort
  236. local function quickSort(low, high)
  237.     if low < high then
  238.         local pi = partition(low, high)
  239.         quickSort(low, pi - 1)
  240.         quickSort(pi + 1, high)
  241.     end
  242. end
  243.  
  244.  
  245. -- Main program
  246. term.clear()
  247. drawBars()
  248. drawButtons()
  249.  
  250. while true do
  251.     local event, button, x, y = os.pullEvent("mouse_click")
  252.     if isButtonClicked(x, y, 1) then
  253.         break
  254.     elseif isButtonClicked(x, y, 3) then
  255.         shuffleList()
  256.         term.clear()
  257.         drawBars()
  258.         drawButtons()
  259.     elseif isButtonClicked(x, y, 5) then
  260.         bubbleSort()
  261.         term.clear()
  262.         drawBars()
  263.         drawButtons()
  264.     elseif isButtonClicked(x, y, 7) then
  265.         selectionSort()
  266.         term.clear()
  267.         drawBars()
  268.         drawButtons()
  269.     elseif isButtonClicked(x, y, 9) then
  270.         insertionSort()
  271.         term.clear()
  272.         drawBars()
  273.         drawButtons()
  274.     elseif isButtonClicked(x, y, 11) then
  275.         mergeSort(1, #numberList)
  276.         term.clear()
  277.         drawBars()
  278.         drawButtons()
  279.     elseif isButtonClicked(x, y, 13) then
  280.         quickSort(1, #numberList)
  281.         term.clear()
  282.         drawBars()
  283.         drawButtons()
  284.     end
  285. end
  286.  
  287. term.clear()
  288. term.setCursorPos(1, 1)
  289.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement