算法
一些具有特定逻辑的代码,通过代码告诉计算机处理一些问题的执行步骤,不断地将数据取出计算然后存入到地址中,这种的编程代码称为算法。
-
算法的特点
- 输入可有可无
- 输出结果一到多个
- 有穷性: 有一定的循环次数,非死循环
- 确定性:代码无二义性,明确每行代码作用
- 可行性:代码一定的执行意义
算法复杂度
- 衡量标准
- 算法在执行中所消耗的时间
- 算法所占用资源的大小,例如:内存占用大小
- 算法的易理解性,易实现性 - 执行时间的影响
- 数据的输入时间
- 算法的编码成可执行程序的时间
- 计算机执行指令的时间
- 代码语句的重复次数
时间复杂度
时间复杂度是同一问题可用不同算法解决,而一个算法的质量优劣将影响到算法乃至程序的效率。算法分析的目的在于选择合适算法和改进算法。
算法的时间复杂度,用来度量算法的运行时间,记作: T(n) = O(f(n))。它表示随着 输入大小n 的增大,算法执行需要的时间的增长速度可以用 f(n) 来描述。
在各种不同的算法中,若算法语句的执行次数为常数,则算法的时间复杂度为O(1),
按数量级递增排列,常见的时间复杂度量有:
(1)O(1):常量阶,运行时间为常量
(2)O(logn):对数阶,如二分搜索算法
(3)O(n):线性阶,如n个数内找最大值
(4)O(nlogn):对数阶,如快速排序算法
(5)O(n^2):平方阶,如选择排序,冒泡排序
(6)O(n^3):立方阶,如两个n阶矩阵的乘法运算
(7)O(2^n):指数阶,如n个元素集合的所有子集的算法
(8)O(n!):阶乘阶,如n个元素全部排列的算法
所消耗的时间从小到大
O(1) < O(logn) < O(n) < O(nlogn) < O(n2) < O(n3) < O(2n) < O(n!) < O(nn)
单向链表
class Node:
def __init__(self, data):
self.data = data
self.next = None
def __str__(self):
return str(self.data)
# 通过单链表构建一个list的结构: 添加 删除 插入 查找 获取长度 判断是否为空...
# list1 = [] list1.append(5) [5,] slist = SingleList() slist.append(5)
class SingleList:
def __init__(self, node=None):
self._head = node
def isEmpty(self):
return self._head == None
def append(self, item):
# 尾部添加
node = Node(item)
if self.isEmpty():
self._head = node
else:
cur = self._head
while cur.next != None:
cur = cur.next
cur.next = node
# 求长度
def len(self):
cur = self._head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
# 遍历
def print_all(self):
cur = self._head
while cur != None:
print(cur)
cur = cur.next
def pop(self, index):
if index < 0 or index >= self.len():
raise IndexError('index Error')
if index == 0:
self._head = self._head.next
else:
cur = self._head
# 找到当前下标的前一个元素
while index - 1:
cur = cur.next
index -= 1
# 修改的next的指向位置
cur.next = cur.next.next
def insert(self, index, item):
if index < 0 or index >= self.len():
raise IndexError('index Error')
if isinstance(item, Node):
raise TypeError('不能是Node类型')
else:
node = Node(item)
if index == 0:
node.next = self._head
self._head = node
else:
cur = self._head
while index - 1:
cur = cur.next
index -= 1
node.next = cur.next
cur.next = node
def update(self, index, new_item):
if index < 0 or index >= self.len():
raise IndexError('index Error')
if isinstance(new_item, Node):
raise TypeError('不能是Node类型')
else:
node = Node(new_item)
if index == 0:
self._head = node
else:
cur = self._head
# 找到当前的元素
while index:
cur = cur.next
index -= 1
cur.data = node.data
def remove(self, item):
index = self.len()
cur = self._head
if cur.data == item:
self._head = cur.next
else:
while index:
if cur.next.data == item:
# print("index--------->", index)
break
cur = cur.next
index -= 1
# print("--------->", cur.data)
cur.next = cur.next.next
if __name__ == '__main__':
slist = SingleList()
print(slist.isEmpty()) # True
print(slist.len())
slist.append(5)
print(slist.isEmpty()) # False
print(slist.len()) # 1
slist.append(8)
slist.append(6)
slist.append(3)
slist.append(1)
print(slist.isEmpty()) # True
print(slist.len())
print('---------------------')
slist.print_all()
print('----------pop-------------')
slist.pop(2)
slist.print_all()
print('--------insert-------')
slist.insert(1, 19)
slist.print_all()
print('--------update-------')
slist.update(1, 100)
slist.print_all()
print('--------remove-------')
slist.remove(100)
slist.print_all()
双向链表
'''
双向链表
'''
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
def __str__(self):
return str(self.data)
class DoubleList:
def __init__(self):
self._head = None
def isEmpty(self):
return self._head == None
def append(self, item):
# 尾部添加
node = Node(item)
if self.isEmpty():
self._head = node
else:
cur = self._head
while cur.next != None:
cur = cur.next
cur.next = node
# 求长度
def add(self, item):
node = Node(item)
if self.isEmpty():
self._head = node
else:
node.next = self._head
self._head.prev = node
self._head = node
def len(self):
cur = self._head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def print_all(self):
cur = self._head
while cur != None:
print(cur)
cur = cur.next
def insert(self, index, item):
node = Node(item)
cur = self._head
while index - 1:
cur = cur.next
index -= 1
node.next = cur.next
node.prev = cur
cur.next.prev = node
cur.next = node
def remove(self, item):
if self.isEmpty():
print("为空")
else:
if self._head.data == item:
self._head = self._head.next
else:
cur = self._head
while cur != None:
if cur.data == item:
cur.prev.next = cur.next
cur.next.prev = cur.prev
break
cur = cur.next
def update(self, index, item):
if self.isEmpty():
print("为空")
else:
if index == 0:
self._head.data = item
else:
count = 0
cur = self._head
while cur != None:
if count == index:
cur.data = item
break
cur = cur.next
count += 1
if __name__ == '__main__':
dlist = DoubleList()
print(dlist.len())
print(dlist.isEmpty())
# dlist.append(6)
# dlist.append(9)
# dlist.append(5)
# print(dlist.len())
# print(dlist.isEmpty())
# dlist.print_all()
dlist.add(5)
dlist.add(4)
dlist.add(3)
dlist.add(2)
dlist.add(1)
dlist.print_all()
print("------------insert-----")
dlist.insert(2, 100)
dlist.print_all()
print("------------remove-----")
dlist.remove(2)
dlist.print_all()
print("------------updata-----")
dlist.update(1,200)
dlist.print_all()