Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f214a0f

Browse files
committedOct 1, 2024·
feat(sorts): add bubble sort for beginners with doctests
1 parent ea06a9b commit f214a0f

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed
 

‎sorts/simple_bubble_sort.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from typing import List
2-
3-
4-
def bubble_sort(nums: List[int]) -> List[int]:
1+
def bubble_sort(nums: list[int]) -> list[int]:
52
"""
63
param nums: the array of integers(int) because this application does not use decimal numbers, but if needed, use float type.
7-
:return: the same nums List ordered by ascending
4+
:return: the same nums list ordered by ascending
5+
6+
Examples:
7+
>>> bubble_sort([8, 2, 4, 5, 7, 0])
8+
[0, 2, 4, 5, 7, 8]
89
910
step 01: create the nums array
1011
step 02: collect the dimension of array
@@ -21,7 +22,10 @@ def bubble_sort(nums: List[int]) -> List[int]:
2122
nums[j], nums[j + 1] = nums[j + 1], nums[j]
2223
return nums
2324

24-
2525
nums = [65, 66, 12, 4, 9, 10, 32, 2] # 01
2626
ordered_nums = bubble_sort(nums) # 06
2727
print("The result:", ordered_nums)
28+
29+
if __name__ == "__main__":
30+
import doctest
31+
doctest.testmod()

0 commit comments

Comments
 (0)
Please sign in to comment.