sort_selection.py
python_old/sort_selection.py
'''
Selection Sort
Time and Space Complexity
Case Time Complexity
Best Case O(n²)
Average O(n²)
Worst Case O(n²)
Space Complexity O(1)
https://visualgo.net/en/sorting
'''
def insertion_sort(arr):
# Traverse from index 1 to end of the array
for i in range(1, len(arr)):
key = arr[i] # Current element to be inserted in sorted part
j = i - 1
# Move elements of arr[0..i-1] that are greater than key one position ahead
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
# Insert the key at its correct position
arr[j + 1] = key
# Example usage:
arr = [12, 11, 13, 5, 6, 2, 1, 17, 29, 10]
print("Original array:", arr)
insertion_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).
記事を読む →