gpt-3.5-turbo-0613· OpenAI
Arena Human Preference 55K
Write a python code for a sorting algorithm
- User
Write a python code for a sorting algorithm
- Assistant
Certainly! Here's an example of a simple implementation of the bubble sort algorithm in Python:
def bubble_sort(arr): n = len(arr) # Traverse through all array elements for i in range(n-1): # Last i elements are already in place for j in range(0, n-i-1): # Traverse the array from 0 to 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: array = [64, 34, 25, 12, 22, 11, 90] bubble_sort(array) print("Sorted array:", array)This code uses the bubble sort algorithm to sort an array in ascending order. The algorithm repeatedly compares adjacent elements and swaps them if they are in the wrong order. It continues to do this until the entire array is sorted.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.