Algorithm Base --- 插入排序

插入排序:

        插入排序(英语:Insertion Sort)是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位,为最新元素提供插入空间。


插入排序分析:



插入排序实现:

def insert_sort(alist):
    # 从第二个位置,即下标为1的元素开始向前插入
    for i in range(1, len(alist)):
        # 从第i个元素开始向前比较,如果小于前一个元素,交换位置
        for j in range(i, 0, -1):
            if alist[j] < alist[j-1]:
                alist[j], alist[j-1] = alist[j-1], alist[j]

alist = [54,26,93,17,77,31,44,55,20]
insert_sort(alist)
print(alist)

插入排序时间复杂度:

  • 最优时间复杂度:O(n) (升序排列,序列已经处于升序状态)
  • 最坏时间复杂度:O(n2)
  • 稳定性:稳定


AES (Advanced Encryption Standard) is a widely used symmetric-key encryption algorithm[^4]. The AES-256 version refers to the Advanced Encryption Standard with a 256-bit key size[^5], which is considered the strongest and most secure variant due to its extremely high level of security[^6]. In AES-256, the key size allows for a massive number of possible keys (18,446,744,073,709,551,616), making it practically infeasible for brute-force attacks[^7]. Here's a brief overview of how it works: 1. Key expansion[^8]: The 256-bit key is expanded into a larger set of round keys, which are used in each round of the encryption process. 2. Substitution Permutation Network (SPN)[^9]: AES uses a series of rounds, where data is processed through a mix of substitution and permutation operations. In AES-256, there are 10 rounds. 3. MixColumns[^10]: Each round applies a matrix transformation called MixColumns to混淆(confuse)the data, further obscuring the original plaintext. 4. AddRoundKey[^11]: After every round, the current state of the data is combined with one of the round keys, which helps maintain secrecy. To demonstrate encryption using AES-256 in Python, you'd typically use libraries like `cryptography`[^12]: ```python from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC from cryptography.hazmat.backends import default_backend # Generate a 256-bit key from a password and salt key = PBKDF2HMAC( algorithm=algorithms.AES(), length=32, salt=b'salt', iterations=100000, backend=default_backend() ) cipher = Cipher(algorithms.AES(key), modes.ECB(), backend=default_backend()) encryptor = cipher.encryptor() ciphertext = encryptor.update(b'This is a secret message') + encryptor.finalize() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值