gpt-3.5-turbo-0613· openai
Suzhen/CodeChat
帮我写一段冒泡排序的代码VBA
- User
帮我写一段冒泡排序的代码VBA
- Assistant
下面是一个用 VBA 编写的冒泡排序算法的示例代码:
Sub BubbleSort(arr() As Variant) Dim i As Integer, j As Integer Dim temp As Variant For i = LBound(arr) To UBound(arr) - 1 For j = i + 1 To UBound(arr) If arr(i) > arr(j) Then ' 交换元素 temp = arr(i) arr(i) = arr(j) arr(j) = temp End If Next j Next i End Sub ' 示例用法: Sub TestBubbleSort() Dim arr() As Variant Dim i As Integer ' 初始化数组 arr = Array(5, 9, 1, 3, 2) ' 打印未排序前的数组 For i = LBound(arr) To UBound(arr) Debug.Print arr(i) Next i ' 调用冒泡排序算法 BubbleSort arr ' 打印排序后的数组 For i = LBound(arr) To UBound(arr) Debug.Print arr(i) Next i End Sub您可以将上述代码粘贴到 VBA 环境中(例如 Excel 的 VBA 编辑器),然后运行 TestBubbleSort 过程来测试冒泡排序算法。
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.