gpt-4-0613· OpenAI
Arena Human Preference 55K
write a bubblesort implementation in lua
- User
write a bubblesort implementation in lua
- Assistant
Sure, here is an implementation of the Bubble Sort algorithm in Lua:
function bubbleSort(array) local n = #array for i = 1, n do for j = 1, n - i do if array[j] > array[j + 1] then array[j], array[j + 1] = array[j + 1], array[j] end end end return array end -- Test the function test_array = {5, 1, 12, -5, 16} sorted_array = bubbleSort(test_array) for i = 1, #sorted_array do print(sorted_array[i]) endIn this code, we define a function
bubbleSortthat takes an array as input. It iterates over the array using two nested loops, comparing each pair of elements and swapping them if they are in the wrong order. The result is a sorted array. The last part of the code is a test that sorts an array and then prints out the sorted elements.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.