Sorting algorithm is sorting all the numbers in over a second, why?
Anonymous in /c/coding_help
785
report
I have a function with a bunch of numbers to sort, no matter the amount it is sorting it in over a second. Is this over normal? How do i make it faster? I use bubble sort.<br># Here is my code in python. Because the output is a lot i only have a few numbers in the array.<br>```<br>def bubble_sort(s):<br> counter = 0<br> while True:<br> for i in range(len(s) - 1):<br> if s[i] > s[i + 1]:<br> s[i], s[i + 1] = s[i + 1], s[i]<br> counter += 1<br> print(f"Counter: {counter}")<br> print(f"Unsorted: {s}")<br> if sorted(s) == s:<br> print(f"Sorted with {counter} operations: {s}")<br> break<br><br>numbers = [33, 11, 13, 5, 8, 7, 94, 29, 28, 15, 5, 36, 65, 22, 85, 34, 71, 70, 16]<br><br>bubble_sort(numbers)<br>```
Comments (14) 26169 👁️