一、常用的一些算法
程序员文章站
2024-02-21 15:10:10
...
最近重新学习一下数据结构与算法,同时也学习一下python,就用python来实现一些常见的算法。工欲善其事,必先利其器,先用python实现一些数据生成与数据校验的方法。
#生成随机数组
def buildArr( num, min_value, max_value):
return_arr = []
for i in range(0, num):
return_arr.append(random.randint(min_value, max_value))
return return_arr
#判断两个数组是否元素完全一致
def isArrSame( first_arr, second_arr):
if len(first_arr) == 0 and len(second_arr) == 0:
return True
if len(first_arr) != len(second_arr):
return False
buf_arr = first_arr.copy()
for i in second_arr:
try:
buf_arr.remove(i)
except Exception:
return False
if len(buf_arr) == 0:
return True
return False
#判断两个数组元素是否顺序一致
def isArrAllSame(first_arr, second_arr):
if len(first_arr) != len(second_arr):
return False
for i in range(len(first_arr)):
if first_arr[i] != second_arr[i]:
return False
return True
以下是几个常用的算法实现
#冒泡排序
def BubbleSort( sortArr ):
for index in range(len(sortArr)):
isOrder = True
for j in range(len(sortArr) - index -1):
if sortArr[j] > sortArr[j+1]:
sortArr[j], sortArr[j+1] = sortArr[j+1], sortArr[j]
isOrder = False
if isOrder:
break
#快速排序
def QuickSort( sortArr, min_index, max_index):
if min_index >= max_index:
return
if len(sortArr) < 1:
return
buf_value = sortArr[min_index]
left = min_index
right = max_index-1
while left < right:
while left < right and sortArr[right] > buf_value:
right -= 1
while left < right and sortArr[left] <= buf_value:
left += 1
if left < right:
sortArr[left], sortArr[right] = sortArr[right], sortArr[left]
sortArr[min_index], sortArr[left] = sortArr[left], sortArr[min_index]
QuickSort(sortArr, min_index, left)
QuickSort(sortArr, left + 1, max_index)
#用快排思想找出第N大的数
def QuicSortN(sortArr, min_index, max_index, n):
if min_index >= max_index:
return sortArr[min_index]
if len(sortArr) < 1:
return -1
buf_value = sortArr[min_index]
left = min_index
right = max_index - 1
while left < right:
while left < right and sortArr[right] > buf_value:
right -= 1
while left < right and sortArr[left] <= buf_value:
left += 1
if left < right:
sortArr[left], sortArr[right] = sortArr[right], sortArr[left]
sortArr[min_index], sortArr[left] = sortArr[left], sortArr[min_index]
if left == n:
return sortArr[left]
elif left > n:
return QuicSortN(sortArr, min_index, left, n)
else:
return QuicSortN(sortArr, left + 1, max_index, n)
#插入排序
def InsortSort( sortArr: []):
for i in range(1, len(sortArr)):
curData = sortArr[i]
index = i -1
while index >= 0:
if sortArr[index] > curData:
sortArr[index+1] = sortArr[index]
index -= 1
else:
break
sortArr[index+1] = curData
#归并排序
def MergeSort( sortArr: []):
if len(sortArr) <= 1:
return
mid = len(sortArr) // 2
left_arr = sortArr[:mid]
right_arr = sortArr[mid:]
MergeSort(left_arr)
MergeSort(right_arr)
left_index = 0
right_index = 0
index = 0
while left_index < len(left_arr):
if right_index >= len(right_arr):
sortArr[index] = left_arr[left_index]
left_index += 1
index += 1
while right_index < len(right_arr):
if left_index >= len(left_arr):
sortArr[index] = right_arr[right_index]
right_index += 1
index += 1
elif left_arr[left_index] < right_arr[right_index]:
sortArr[index] = left_arr[left_index]
left_index += 1
index += 1
else:
sortArr[index] = right_arr[right_index]
right_index += 1
index += 1
#选择排序
def SelectSort( sortArr: []):
for i in range(len(sortArr)):
curData = sortArr[i]
index = i
for j in range(i+1, len(sortArr)):
if sortArr[j] < curData:
index = j
curData = sortArr[j]
sortArr[i], sortArr[index] = sortArr[index], sortArr[i]
以下是算法的使用以及性能分析
if __name__ == "__main__":
listArr = []
data_len = 1000
for i in range(data_len):
listArr.append(random.randint(0, data_len))
quic_sort_arr = listArr.copy()
bubble_sort_arr = listArr.copy()
insert_sort_arr = listArr.copy()
quic_sort_n_arr = listArr.copy()
select_sort_arr = listArr.copy()
merge_sort_arr = listArr.copy()
print(QuicSortN(quic_sort_n_arr, 0, data_len, 0))
curTime = time.time()
QuickSort(quic_sort_arr, 0, data_len)
print("quick sort : ", time.time() - curTime)
curTime = time.time()
BubbleSort(bubble_sort_arr)
print("bubble sort : ", time.time() - curTime)
curTime = time.time()
InsortSort(insert_sort_arr)
print("insert sort : ", time.time() - curTime)
curTime = time.time()
SelectSort(select_sort_arr)
print("select sort : ", time.time() - curTime)
curTime = time.time()
MergeSort(merge_sort_arr)
print("merge sort : ", time.time() - curTime)
结果如下:
上一篇: Spark提交代码的两种方式
下一篇: PHP一些配置信息