gpt-3.5-turbo-0301· openai
Suzhen/CodeChat
python写一个冒泡排序
- User
python写一个冒泡排序
- Assistant
这是一个Python实现的冒泡排序例子:
def bubble_sort(arr): n = len(arr) # 遍历所有数组元素 for i in range(n): # Last i elements are already in place for j in range(0, n-i-1): # 如果当前元素大于下一个元素,则交换它们 if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] # 测试示例 arr = [64, 34, 25, 12, 22, 11, 90] bubble_sort(arr) print("排序后的数组:") for i in range(len(arr)): print("%d" %arr[i]),输出结果:
排序后的数组: 11 12 22 25 34 64 90
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.