Python Bubble Sort Example
Created on: September 28, 2024
Answered using OpenAI 01 Preview by OpenAI01
Question
Wrtie me a simple python bubble sort.
Thought
오픈AI 정책 준수
사용자가 "파이썬 버블 정렬을 작성해"라고 요청하고 있어. 정책을 따라 추가 내용은 포함하지 않고 간단한 Python 구현을 제공 중이야.
Answer
Here's a simple implementation of bubble sort in Python:
def bubble_sort(arr): n = len(arr) # Traverse through all array elements for i in range(n): # Last i elements are already sorted for j in range(0, n - i - 1): # Swap if the element found is greater than the next element if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] # Example usage: numbers = [64, 34, 25, 12, 22, 11, 90] bubble_sort(numbers) print("Sorted array is:", numbers)
Output:
Sorted array is: [11, 12, 22, 25, 34, 64, 90]