0%

Sort Algorithm 1 - Bubble Sort

1
2
3
4
5
6
7
8
9
def bubbleSort(nums):
for i in range(len(nums)-1):
for j in range(len(nums)-1-i):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
return nums
if __name__ == '__main__':
nums = [10, 17, 50, 7, 30, 24, 30, 45, 15, 5, 36, 21]
print(bubbleSort(nums))

(1) average time cost T(n): O(n^2)
(2) average space cost S(n): O(1)
(3) in-place sort
(4) stable
(5) best T(n): O(n); Worst T(n): O(n^2)

-------------End of blogThanks for your reading-------------