Unnecessary Function
Your swap
function is unnecessary. Simply replace the function call with this line:
nums[j], nums[j + 1] = nums[j + 1], nums[j]
This does the swapping for you.
Spacing
There should be spaces between values in lists
[1, 2, 3, 4, 5]
between numbers/strings and operators
if nums[j] > nums[j + 1]
and between parameters in a function call
for j in range(0, len(nums) - 1 - i, 1):
Type Hints
Your function header can look like this:
from typing import List, Union
def bubble_sort(nums: List[Union[int, float]]) -> List[Union[int, float]]:
What this says is that the function accepts a list of integers/floats, and returns a list of integers/floats. It adds another layer of descriptiveness to your code.
Docstrings
You should include a docstring
at the beginning of every class/method/module you write. This allows you to describe in words what your code is doing.
def bubble_sort(nums: List[Union[int, float]]) -> List[Union[int, float]]:
"""
A bubble sort algorithm, etc etc etc
:param nums -> List: A list of integers/floats to sort
:return List: The sorted list of integers/floats, from smallest -> biggest
"""
a
list - this may not be intended. You might want to copy a list or create a new one and fill with results. stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list \$\endgroup\$