0

I use python 3.10. Once I tried to write bubble sort, but it works in unexpected way-it fills the array with copies of some of it's elements. For example, array, I wanted to sort: [43, 33, 68, 80, 90, 15, 21, 17, 21, 48] "Sorted" array: [15, 15, 15, 15, 15, 15, 17, 17, 21, 48] Here is the code:

for i in range(N - 1):  # N-number of elements
    for j in range(N - i - 1):
        if a[j] > a[j + 1]:
            a[j] = a[j + 1]
            a[j + 1] = a[j]

Please help me find the error here...

Morty
  • 1
  • 1
    You're probably trying to swap the elements in the if statement, but consider what `a[j + 1] = a[j]` means. What will be put in `a[j + 1]`? Hint, look at the line above. – Ted Klein Bergman Dec 27 '21 at 20:27

0 Answers0