Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Define the width and height of the screen
- local screenWidth, screenHeight = term.getSize()
- -- Create and store the list of numbers 1 to 19
- local numberList = {}
- for i = 1, 19 do
- table.insert(numberList, i)
- end
- -- Function to draw a vertical bar
- local function drawBar(x, height, color)
- term.setTextColor(color or colors.white)
- for y = 1, height do
- term.setCursorPos(x, screenHeight - y + 1)
- term.write("|")
- end
- end
- -- Function to draw buttons on the left side of the screen
- local function drawButtons()
- term.setCursorPos(5, 1)
- term.write("X")
- term.setCursorPos(5, 3)
- term.write("Shuffle")
- term.setCursorPos(5, 5)
- term.write("BubbleSort")
- term.setCursorPos(5, 7)
- term.write("SelectionSort")
- term.setCursorPos(5, 9)
- term.write("InsertionSort")
- term.setCursorPos(5, 11)
- term.write("MergeSort")
- term.setCursorPos(5, 13)
- term.write("QuickSort")
- end
- -- Function to check if the mouse click is within a button area
- local function isButtonClicked(x, y, buttonY)
- return x >= 5 and x <= 14 and y == buttonY
- end
- -- Function to pause execution for visualization
- local function pause(seconds)
- local endTime = os.clock() + seconds
- while os.clock() < endTime do
- os.queueEvent("monitor_touch")
- os.pullEvent()
- end
- end
- -- Function to play a note with frequency based on position
- local function playNoteBasedOnPosition(position)
- local speaker = peripheral.find("speaker")
- if speaker then
- local pitch = 0.5 + (position / #numberList)
- speaker.playNote("harp", 1, pitch)
- end
- end
- -- Function to shuffle the list
- local function shuffleList()
- for i = 1, #numberList - 1 do
- local j = math.random(i + 1, #numberList)
- numberList[i], numberList[j] = numberList[j], numberList[i]
- end
- end
- -- Function to draw bars with the current number list
- local function drawBars()
- for i = 1, 19 do
- local barHeight = numberList[i]
- drawBar(20 + i, barHeight)
- end
- end
- -- Sorting Algorithms
- -- Bubble Sort
- local function bubbleSort()
- local n = #numberList
- for i = 1, n - 1 do
- for j = 1, n - i do
- term.clear()
- drawBars()
- drawButtons()
- drawBar(20 + j, numberList[j], colors.green)
- drawBar(20 + j + 1, numberList[j + 1], colors.green)
- playNoteBasedOnPosition(j)
- pause(0.05)
- if numberList[j] > numberList[j + 1] then
- drawBar(20 + j, numberList[j], colors.red)
- drawBar(20 + j + 1, numberList[j + 1], colors.red)
- playNoteBasedOnPosition(j)
- pause(0.05)
- numberList[j], numberList[j + 1] = numberList[j + 1], numberList[j]
- end
- term.clear()
- end
- end
- end
- -- Selection Sort
- local function selectionSort()
- local n = #numberList
- for i = 1, n - 1 do
- local minIndex = i
- for j = i + 1, n do
- term.clear()
- drawBars()
- drawButtons()
- drawBar(20 + j, numberList[j], colors.green)
- drawBar(20 + minIndex, numberList[minIndex], colors.green)
- playNoteBasedOnPosition(j)
- pause(0.05)
- if numberList[j] < numberList[minIndex] then
- minIndex = j
- end
- term.clear()
- end
- if minIndex ~= i then
- drawBar(20 + i, numberList[i], colors.red)
- drawBar(20 + minIndex, numberList[minIndex], colors.red)
- playNoteBasedOnPosition(minIndex)
- pause(0.05)
- numberList[i], numberList[minIndex] = numberList[minIndex], numberList[i]
- end
- term.clear()
- end
- end
- -- Insertion Sort
- local function insertionSort()
- local n = #numberList
- for i = 2, n do
- local key = numberList[i]
- local j = i - 1
- while j > 0 and numberList[j] > key do
- term.clear()
- drawBars()
- drawButtons()
- drawBar(20 + j, numberList[j], colors.green)
- drawBar(20 + j + 1, numberList[j + 1], colors.red)
- playNoteBasedOnPosition(j)
- pause(0.05)
- numberList[j + 1] = numberList[j]
- j = j - 1
- term.clear()
- end
- numberList[j + 1] = key
- end
- end
- local function merge(low, mid, high)
- local n1 = mid - low + 1
- local n2 = high - mid
- local L = {}
- local R = {}
- for i = 1, n1 do
- L[i] = numberList[low + i - 1]
- end
- for j = 1, n2 do
- R[j] = numberList[mid + j]
- end
- local i = 1
- local j = 1
- local k = low
- while i <= n1 and j <= n2 do
- term.clear()
- drawBars()
- drawButtons()
- drawBar(20 + k, numberList[k], colors.green)
- playNoteBasedOnPosition(k)
- pause(0.05)
- if L[i] <= R[j] then
- numberList[k] = L[i]
- i = i + 1
- else
- numberList[k] = R[j]
- j = j + 1
- end
- k = k + 1
- term.clear()
- end
- while i <= n1 do
- numberList[k] = L[i]
- i = i + 1
- k = k + 1
- end
- while j <= n2 do
- numberList[k] = R[j]
- j = j + 1
- k = k + 1
- end
- end
- -- Merge Sort
- local function mergeSort(low, high)
- if low < high then
- local mid = math.floor((low + high) / 2)
- mergeSort(low, mid)
- mergeSort(mid + 1, high)
- merge(low, mid, high)
- end
- end
- local function partition(low, high)
- local pivot = numberList[high]
- local i = low - 1
- for j = low, high - 1 do
- term.clear()
- drawBars()
- drawButtons()
- drawBar(20 + j, numberList[j], colors.green)
- playNoteBasedOnPosition(j)
- pause(0.05)
- if numberList[j] < pivot then
- i = i + 1
- drawBar(20 + i, numberList[i], colors.red)
- drawBar(20 + j, numberList[j], colors.red)
- pause(0.05)
- numberList[i], numberList[j] = numberList[j], numberList[i]
- end
- end
- numberList[i + 1], numberList[high] = numberList[high], numberList[i + 1]
- return i + 1
- end
- -- Quick Sort
- local function quickSort(low, high)
- if low < high then
- local pi = partition(low, high)
- quickSort(low, pi - 1)
- quickSort(pi + 1, high)
- end
- end
- -- Main program
- term.clear()
- drawBars()
- drawButtons()
- while true do
- local event, button, x, y = os.pullEvent("mouse_click")
- if isButtonClicked(x, y, 1) then
- break
- elseif isButtonClicked(x, y, 3) then
- shuffleList()
- term.clear()
- drawBars()
- drawButtons()
- elseif isButtonClicked(x, y, 5) then
- bubbleSort()
- term.clear()
- drawBars()
- drawButtons()
- elseif isButtonClicked(x, y, 7) then
- selectionSort()
- term.clear()
- drawBars()
- drawButtons()
- elseif isButtonClicked(x, y, 9) then
- insertionSort()
- term.clear()
- drawBars()
- drawButtons()
- elseif isButtonClicked(x, y, 11) then
- mergeSort(1, #numberList)
- term.clear()
- drawBars()
- drawButtons()
- elseif isButtonClicked(x, y, 13) then
- quickSort(1, #numberList)
- term.clear()
- drawBars()
- drawButtons()
- end
- end
- term.clear()
- term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement