bubble sort list of int in python
- User
bubble sort list of int in python
- Assistant
def bubbleSort(arr): n = len(arr) swapped = False for i in range(n-1): for j in range(0, n-i-1): if arr[j] > arr[j + 1]: swapped = True arr[j], arr[j + 1] = arr[j + 1], arr[j] if not swapped: returnDriver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print("Sorted array is:") for i in range(len(arr)): print("% d" % arr[i], end=" ")
- User
I think the 'print' instruction in python doesn't have that syntax. The function bubbleSort is write into ''' ''' signs that means comment. If I copy and paste that code in my IDE as a beginner it won't work properly.
- Assistant
You are right in your comment about the triple quotations. Here is a revised version of the code:
def bubbleSort(arr): n = len(arr) swapped = False for i in range(n-1): for j in range(0, n-i-1): if arr[j] > arr[j + 1]: swapped = True arr[j], arr[j + 1] = arr[j + 1], arr[j]
if not swapped: returnDriver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
bubbleSort(arr)
print("Sorted array is:") for i in range(len(arr)): print("% d" % arr[i], end=" ")
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Identical first prompt
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →
- Human-written demonstration (OpenAssistant volunteers)Compare side by side →