算法的时间复杂度示例

本文是学习数据结构的笔记。

【效果图】

【代码】

复制代码
# example.py

# 算法时间复杂度示例


def func_01(n):
    ''' 时间复杂度O(Log(Log(N))) '''
    import math
    i = n
    count = 0
    while i > 1: 
        i = round(math.sqrt(i)) # 注意:sqrt(i)!
        count += 1
    return count

print('时间复杂度O(Log(Log(N))),N=2000000000000000000,循环{}次'.format(func_01(2000000000000000000)))




def func_02(n):
    ''' 时间复杂度O(Log(N)) '''
    i = n
    count = 0
    while i >= 1: 
        i = i // 2 # 注意:// 2!
        count += 1
    return count

print('时间复杂度O(Log(N)),N=100000000,循环{}次'.format(func_02(100000000)))





def func_03(n):
    ''' 时间复杂度O((Log(N))^2) '''
    i = 1
    count = 0
    while i <= n: 
        j = n
        while j > 0:
            j = j // 2 # 注意:// 2!
            count += 1
        i = i * 2 # 注意:* 2!
    return count

print('时间复杂度O((Log(N))^2),N=100000000,循环{}次'.format(func_03(100000000)))




def func_04_01(n):
    ''' 时间复杂度O(Sqrt(N)) '''
    i = s = 1
    count = 0
    while  s < n:
        i = i + 1
        s = s + i
        count += 1
    return count

print('时间复杂度O(Sqrt(N)),N=10000,循环{}次'.format(func_04_01(10000)))




def func_04_02(n):
    ''' 时间复杂度O(Sqrt(N)) '''
    i = 1
    count = 0
    while i * i < n:
        count = count + 1
        i = i + 1
    return count

print('时间复杂度O(Sqrt(N)),N=10000,循环{}次'.format(func_04_02(10000)))




def func_05(n):
    ''' 时间复杂度O(N) '''
    count = 0
    for i in range(1, n): 
        count += 1
    return count

print('时间复杂度O(N),N=100,循环{}次'.format(func_05(100)))




def func_06_01(n):
    ''' 时间复杂度O(N*Log(N)) '''
    count = 0
    for i in range(1, n): 
        j = 1
        while j <= n:
            j = j * 2 # 注意:* 2!
            count += 1
    return count

print('时间复杂度O(N*Log(N)),N=1000,循环{}次'.format(func_06_01(1000)))


def func_06_02(n):
    ''' 时间复杂度O(N*Log(N)) '''
    count = 0
    for i in range(1, n):
        j = 1
        while j < n:
            j = j + i # 注意: + i!
            count = count + 1
    return count

print('时间复杂度O(N*Log(N)),N=1000,循环{}次'.format(func_06_02(1000)))


def func_06_03(n):
    ''' 时间复杂度O(N*Log(N)) '''
    count = 0
    for i in range(1, n // 3): # 注意:// 3!
        j = 1
        while j <= n:
            j = j + 4 # 注意:+ 4!
            count = count + 1
    return count

print('时间复杂度O(N*Log(N)),N=1000,循环{}次'.format(func_06_03(1000)))




def func_07(n):
    ''' 时间复杂度O(N*(Log(N))^2) '''
    count = 0
    for i in range(1, n):
        j = 1
        while j <= n:
            k = 1
            while k <= n:
                count += 1
                k = k * 2 # 注意:* 2!
            j = j * 2 # 注意:* 2!
    return count

print('时间复杂度O((N*Log(N))^2),N=100,循环{}次'.format(func_07(100)))



def func_08(n):
    ''' 时间复杂度O(N^2) '''
    count = 0
    for i in range(n):
        for j in range(n):
            count += 1
    return count

print('时间复杂度O((N^2),N=100,循环{}次'.format(func_08(100)))



def func_09(n):
    ''' 时间复杂度O(N^3) '''
    count = 0
    for i in range(n):
        for j in range(n):
            for k in range(n):
                count += 1
    return count

print('时间复杂度O((N^3),N=50,循环{}次'.format(func_09(50)))
复制代码

 

本文转自罗兵博客园博客,原文链接:http://www.cnblogs.com/hhh5460/p/4391340.html ,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
AVL树是一种自平衡的二叉搜索树,它的查找算法时间复杂度为O(log n),其中n是树中节点的数量。 AVL树通过在插入或删除节点时进行旋转操作来保持树的平衡性。在AVL树中,每个节点都有一个平衡因子,它表示该节点的左子树高度与右子树高度之差。当插入或删除节点后,如果某个节点的平衡因子超过了1或-1,就需要进行旋转操作来恢复平衡。 由于AVL树保持了树的平衡性,所以在最坏情况下,AVL树的高度为O(log n)。因此,查找操作的时间复杂度为O(log n)。 下面是一个AVL树的查找算法示例代码: ```python class Node: def __init__(self, key): self.key = key self.left = None self.right = None self.height = 1 class AVLTree: def __init__(self): self.root = None def insert(self, key): self.root = self._insert(self.root, key) def _insert(self, root, key): if root is None: return Node(key) elif key < root.key: root.left = self._insert(root.left, key) else: root.right = self._insert(root.right, key) root.height = 1 + max(self._get_height(root.left), self._get_height(root.right)) balance = self._get_balance(root) if balance > 1 and key < root.left.key: return self._rotate_right(root) if balance < -1 and key > root.right.key: return self._rotate_left(root) if balance > 1 and key > root.left.key: root.left = self._rotate_left(root.left) return self._rotate_right(root) if balance < -1 and key < root.right.key: root.right = self._rotate_right(root.right) return self._rotate_left(root) return root def _get_height(self, root): if root is None: return 0 return root.height def _get_balance(self, root): if root is None: return 0 return self._get_height(root.left) - self._get_height(root.right) def _rotate_left(self, z): y = z.right T2 = y.left y.left = z z.right = T2 z.height = 1 + max(self._get_height(z.left), self._get_height(z.right)) y.height = 1 + max(self._get_height(y.left), self._get_height(y.right)) return y def _rotate_right(self, z): y = z.left T3 = y.right y.right = z z.left = T3 z.height = 1 + max(self._get_height(z.left), self._get_height(z.right)) y.height = 1 + max(self._get_height(y.left), self._get_height(y.right)) return y def search(self, key): return self._search(self.root, key) def _search(self, root, key): if root is None or root.key == key: return root if key < root.key: return self._search(root.left, key) return self._search(root.right, key) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值