Python内置类型性能分析
队列
class Queue(object):
def __init__(self):
self.__list = []
def enqueue(self, item):
self.__list.append(item)
def dequeue(self):
return self.__list.pop(0)
def is_empty(self):
return self.__list == []
def size(self):
return len(self.__list)
单向链表
class Node(object):
def __init__(self, elem):
self.elem = elem
self.next = None
class SingleLinkList(object):
def __init__(self, node=None):
self._head = node
def is_empty(self):
return self._head == None
def length(self):
cur = self._head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def travel(self):
cur = self._head
while cur != None:
print(cur.elem, end=" ")
cur = cur.next
def add(self, item):
node = Node(item)
node.next = self._head
self._head = node
def append(self, item):
node = Node(item)
if self.is_empty():
self._head = node
else:
cur = self._head
while cur.next != None:
cur = cur.next
cur.next = node
def insert(self, pos, item):
if pos <= 0:
self.add(item)
elif pos > (self.length()-1):
self.append(item)
else:
node = Node(item)
pre = self._head
count = 0
while count < (pos-1):
count += 1
pre = pre.next
node.next = pre.next
pre.next = node
def remove(self, item):
cur = self._head
pre = None
while cur != None:
if cur.elem == item:
if cur == self._head:
self._head = cur.next
else:
pre.next = cur.next
break
else:
pre = cur
cur = cur.next
def search(self, item):
cur = self._head
while cur != None:
if cur.elem == item:
return True
else:
cur = cur.next
return False
单向循环链表
class Node(object):
def __init__(self, elem):
self.elem = elem
self.next = None
class SingleLinkList(object):
def __init__(self, node=None):
self._head = node
if node:
node.next = node
def is_empty(self):
return self._head == None
def length(self):
if self.is_empty():
return 0
cur = self._head
count = 1
while cur.next != self._head:
count += 1
cur = cur.next
return count
def travel(self):
if self.is_empty():
return
cur = self._head
while cur.next != self._head:
print(cur.elem, end=" ")
cur = cur.next
print(cur.elem)
def add(self, item):
node = Node(item)
if self.is_empty():
self._head = node
node.next = node
else:
cur = self._head
while cur.next != self._head:
cur = cur.next
node.next = self._head
self._head = node
cur.next = self._head
def append(self, item):
node = Node(item)
if self.is_empty():
self._head = node
node.next = node
else:
cur = self._head
while cur.next != self._head:
cur = cur.next
cur.next = node
node.next = self._head
def insert(self, pos, item):
if pos <= 0:
self.add(item)
elif pos > (self.length()-1):
self.append(item)
else:
node = Node(item)
pre = self._head
count = 0
while count < (pos-1):
count += 1
pre = pre.next
node.next = pre.next
pre.next = node
def remove(self, item):
if self.is_empty():
return
cur = self._head
pre = None
while cur.next != self._head:
if cur.elem == item:
if cur == self._head:
rear = self._head
while rear.next != self._head:
rear = rear.next
self._head = cur.next
rear.next = self._head
else:
pre.next = cur.next
return
else:
pre = cur
cur = cur.next
if cur.elem == item:
if cur == self._head:
self._head = None
else:
pre.next = self._head
def search(self, item):
if self.is_empty():
return False
cur = self._head
while cur.next != self._head:
if cur.elem == item:
return True
else:
cur = cur.next
if cur.elem == item:
return True
return False
双向链表
class Node(object):
def __init__(self, item):
self.elem = item
self.next = None
self.prev = None
class DoubleLinkList(object):
def __init__(self, node=None):
self.__head = node
def is_empty(self):
return self.__head is None
def length(self):
cur = self.__head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def travel(self):
cur = self.__head
while cur != None:
print(cur.elem, end=" ")
cur = cur.next
print("")
def add(self, item):
node = Node(item)
node.next = self.__head
self.__head = node
node.next.pre = node
def append(self, item):
node = Node(item)
if self.is_empty():
self.__head = node
else:
cur = self.__head
while cur.next != None:
cur = cur.next
cur.next = node
node.pre = cur
def insert(self, pos, item):
if pos <= 0:
self.add(item)
elif pos > (self.length()-1):
self.append(item)
else:
count = 0
cur = self.__head
while count < pos:
count += 1
cur = cur.next
node = Node(item)
node.next = cur
node.pre = cur.pre
cur.pre.next = node
cur.pre = node
def remove(self, item):
cur = self.__head
while cur != None:
if cur.elem == item:
if cur == self.__head:
self.__head = cur.next
if cur.next:
cur.next.prev = None
else:
cur.prev.next = cur.next
if cur.next:
cur.next.prev = cur.prev
break
else:
cur = cur.next
def search(self, item):
cur = self.__head
while cur != None:
if cur.elem = item:
return True
else:
cur = cur.next
return False
冒泡排序
def bubble_sort(alist):
"""冒泡排序"""
n = len(alist)
for j in range(n-1):
count = 0
for i in range(0, n-1-j):
# 班长从头走到尾
if alist[i] > alist[i+1]:
alist[i],alist[i+1] = alist[i+1], alist[i]
count += 1
if 0 == count:
return
选择排序
def select_sort(alist):
"""选择排序"""
n = len(alist)
for j in range(n-1): # j: 0 ~ n-2
min_index = j
for i in range(j+1, n):
if alist[min_index] > alist[i]:
min_index = i
alist[j], alist[min_index] = alist[min_index], alist[j]