Sorting problems

Report a typo

The code below solves the following problem: given a list, check if it is sorted. However, this code works incorrectly. For example, it outputs "sorted" for input 2 1 3. Find and correct the mistake.

Tip: The mistake is connected to copying objects.

Sample Input 1:

2 1 3

Sample Output 1:

not sorted
Write a program in Python 3
# sorting function
def bubble_sort(a):
n = len(a)
for i in range(n - 1):
for j in range(n - i - 1):
if a[j] > a[j + 1]:
a[j], a[j + 1] = a[j + 1], a[j]
return a

# where copying takes place
arr = [int(i) for i in input().split()]
sorted_arr = bubble_sort(arr)

if arr == sorted_arr:
print('sorted')
else:
print('not sorted')
___

Create a free account to access the full topic