python开发之路Day17-算法设计(冒泡排序选择排序插入排序二叉树)
Posted 南非波波
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python开发之路Day17-算法设计(冒泡排序选择排序插入排序二叉树)相关的知识,希望对你有一定的参考价值。
s12-20160514-day17
pytho自动化开发 day17
Date:2016.05.14
@南非波波
课程大纲:
http://www.cnblogs.com/alex3714/articles/5474411.html
一、冒泡排序算法
#!/usr/local/env python3
\'\'\'
Author:@南非波波
Blog:http://www.cnblogs.com/songqingbo/
E-mail:qingbo.song@gmail.com
\'\'\'
import random,time
#m冒泡排序
def bubble_up1(array):
\'\'\'
m冒泡排序算法
:param array:
:return: count: 6190862 time: 6.706383466720581
\'\'\'
count = 0
for i in range(len(array)):
for j in range(len(array) - 1 - i):
if array[j] > array[j + 1]:
temp = array[j + 1]
array[j + 1] = array[j]
array[j] = temp
count += 1
print("count:", count)
print("array:", array)
def bubble_up2(array):
\'\'\'
m冒泡排序算法
:param array:
:return:count: 5000 time: 3.825218915939331
\'\'\'
count = 0
for i in range(len(array)):
for j in range(len(array) - 1 - i):
big_temp = j
if array[big_temp] > array[j + 1]:
big_temp = j + 1
temp = array[big_temp]
array[i] = array[big_temp]
array[big_temp] = temp
count += 1
print("count:", count)
print("array:", array)
if __name__ == \'__main__\':
array = []
for i in range(5000):
array.append(random.randrange(1000))
time_start = time.time()
bubble_up2(array)
time_end = time.time()
#
print(array[0:100])
print("time:",time_end - time_start)
二、 选择排序
def select1(array):
\'\'\'
选择排序算法v1
:param array:
:return:count: 1996750 time: 3.2961885929107666
\'\'\'
count = 0
for i in range(len(array)):
for j in range(i,len(array)):
if array[i] > array[j]:
temp = array[j]
array[j] = array[i]
array[i] = temp
count += 1
print("count:",count)
print("array:",array)
def select2(array):
\'\'\'
选择排序算法v2
:param array:
:return:count: 5000 time: 2.4801418781280518
\'\'\'
count = 0
for i in range(len(array)):
smallest_index = i
for j in range(i,len(array)):
if array[smallest_index] > array[j]:
smallest_index = j
temp = array[smallest_index]
array[smallest_index] = array[i]
array[i] = temp
count += 1
print("count:",count)
print("array:",array)
三、直接插入排序算法
def insert1(array):
\'\'\'
插入排序算法
:param array:
:return: count: 4999 time: 3.685210704803467
\'\'\'
count = 0
for index in range(1, len(array)):
current_val = array[index] # 先记下来每次大循环走到的第几个元素的值
position = index
while position > 0 and array[
position - 1] > current_val: # 当前元素的左边的紧靠的元素比它大,要把左边的元素一个一个的往右移一位,给当前这个值插入到左边挪一个位置出来
array[position] = array[position - 1] # 把左边的一个元素往右移一位
position -= 1 # 只一次左移只能把当前元素一个位置 ,还得继续左移只到此元素放到排序好的列表的适当位置 为止
array[position] = current_val # 已经找到了左边排序好的列表里不小于current_val的元素的位置,把current_val放在这里
count += 1
print("count:", count)
print(array)
四、快速排序
def quick_sort(array,start,end):
\'\'\'
快速排序算法
:param array:
:param start:
:param end:
:return:time: 0.03600192070007324
\'\'\'
if start >= end:
return
k = array[start]
left_flag = start
right_flag = end
while left_flag < right_flag:
while array[right_flag] > k:
right_flag -= 1
temp = array[right_flag]
array[left_flag] = array[right_flag]
array[right_flag] = temp
while array[left_flag] <= k:
left_flag += 1
temp = array[left_flag]
array[left_flag] = array[right_flag]
array[right_flag] = temp
quick_sort(array,start,left_flag - 1)
quick_sort(array,left_flag + 1,end)
以上是关于python开发之路Day17-算法设计(冒泡排序选择排序插入排序二叉树)的主要内容,如果未能解决你的问题,请参考以下文章