sort_bubble.py
python_old/sort_bubble.py
'''
Bubble Sort
Time Complexity of Bubble Sort
Case Comparisons/Swaps Time Complexity
Best Case No swaps needed (already sorted) O(n)
Average Case Regular comparisons/swaps O(n²)
Worst Case Reverse sorted array O(n²)
Space Complexity
O(1) Bubble sort is an in-place sorting algorithm (uses no extra space except a few variables).
https://visualgo.net/en/sorting
'''
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Track whether any swap was made in this pass
swapped = False
for j in range(0, n - i - 1): # Last i elements are already sorted
if arr[j] > arr[j + 1]:
# Swap if the current item is greater than the next
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
# If no swaps were made, the list is already sorted
if not swapped:
break
# Example usage:
arr = [64, 34, 25, 12, 22, 11, 1, 23, 7, 5, 9]
print("Original array:", arr)
bubble_sort(arr)
print("Sorted array: ", arr)
相关文章
01_classical_caesar_cipher.py
01_classical_caesar_cipher.py — python source code from the python old learning materials (python_old/Cryptography/01_classical_caesar_cipher.py).
阅读文章 →02_classical_monoalphabetic.py
02_classical_monoalphabetic.py — python source code from the python old learning materials (python_old/Cryptography/02_classical_monoalphabetic.py).
阅读文章 →03_classical_rail_fence.py
03_classical_rail_fence.py — python source code from the python old learning materials (python_old/Cryptography/03_classical_rail_fence.py).
阅读文章 →04_frequency_analysis.py
04_frequency_analysis.py — python source code from the python old learning materials (python_old/Cryptography/04_frequency_analysis.py).
阅读文章 →05_cryptographic_hashing.py
05_cryptographic_hashing.py — python source code from the python old learning materials (python_old/Cryptography/05_cryptographic_hashing.py).
阅读文章 →06_password_hashing.py
06_password_hashing.py — python source code from the python old learning materials (python_old/Cryptography/06_password_hashing.py).
阅读文章 →