Write me a bubble sort function in Powershell
- User
Write me a bubble sort function in Powershell
Assistant
Here is a bubble sort function in Powershell:
function BubbleSort { param ( [int[]]$array ) for ($i = 0; $i -le $array.Count - 2; $i++) { for ($j = 0; $j -le $array.Count - 2; $j++) { if ($array[$j] -gt $array[$j + 1]) { $temp = $array[$j] $array[$j] = $array[$j + 1] $array[$j + 1] = $temp } } } return $array }This function takes in an array of integers and sorts them in ascending order using the bubble sort algorithm. It iterates over the array multiple times, comparing adjacent elements and swapping them if they are in the wrong order.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.