根据前面两天的积累,我们使用python写几种简单的排序算法。比较常见的排序有:插入排序,交换排序(冒泡、快速)排序、选择(堆、鸡尾酒)排序等。当然了,还有各种各样的归并排序、基数排序等等,请恕哥们基础太差,这些我也没想过折腾,时间允许的话,我不介意再折腾折腾。话不多唠,按习惯上代码。
1,插入排序---insert_sort, shell_sort
# !/usr/bin/env python
# -*- coding : utf-8 -*-
def insert_sort (l) :
print l
for i in range (1, len(l)) :
value = l[i]
j = i
while l[j] < l[j-1] and j > 0 :
l[j] = l[j-1]
l[j-1] = value
j -= 1
return l
if __name__ == "__main__":
ll = insert_sort([8,4,3,1,6,9,5,2,7])
print "insert_sort :\n" + str(ll)
啰嗦一句,插入排序在于将前面的数据视为有序,然后将后面的数据依次插入到前面序列,并保证前面有序就OK了。
# !/usr/bin/env python
# -*- coding : utf-8 -*-
def shell_sort (l):
print l
h = len(l) / 2
while h > 0:
i = h
while i < len(l) :
value = l[i]
j = i - h
while j >= 0 and value < l[j]:
l[j + h] = l[j]
j -= h
l[j+h] = value
i += 1
h /= 2
return l
if __name__ == "__main__":
ll = shell_sort([8,4,3,1,6,9,5,2,7])
print "shell_sort:\n" + str(ll)
分区间排序
2,交换排序---bubble_sort, quick_sort
# !/usr/bin/env python
# -*- coding : utf-8 -*-
def bubble_sort (l):
print l
for j in range (1, len(l)):
escape = 0
for i in range (1, len(l)):
if l[i] < l[i-1] :
value = l[i]
l[i] = l[i-1]
l[i-1] = value
escape = 1
if not escape:
return l
if __name__ == "__main__" :
ll = bubble_sort([8,4,3,1,6,9,5,2,7])
print "bubble_sort:\n" + str(ll)
冒泡排序,实在没什么好说,我这人就是这样,简单的看不上,难一点的看不上我(毛病,要改...)。
# !/usr/bin/env python
# -*- coding : utf-8 -*-
def quick_sort (l, start, end):
print l
i = start
j = end
while i < j:
while i < j and l[i] < l[j]: #右侧扫描
j -= 1
if i < j :
l[i], l[j] = l[j], l[i]
while i < j and l[i] < l[j]: #左侧扫描
i += 1
if i < j:
l[i], l[j] = l[j], l[i]
if i - start > 1:
quick_sort (l, start, i - 1)
if end - j > 1 :
quick_sort (l, j + 1, end)
return l
if __name__ == "__main__":
l = [8,4,3,1,6,9,5,2,7]
ll = quick_sort(l, 0, len(l) - 1)
print "quick_sort:\n" + str(ll)
快速排序,网上一大堆例子,我 也是班门弄斧,见谅见谅!
3,选择排序select_sort, heap_sort, cocktail_sort
# !/usr/bin/env python
# -*- coding : utf-8 -*-
def select_sort (l):
print l
for i in range(len(l)):
value = l[i]
pos = i
for j in range (i+1, len(l)):
if l[j] < value :
value = l[j]
pos = j
l[pos] = l[i]
l[i] = value
return l
if __name__ == "__main__" :
ll = select_sort([8,4,3,1,6,9,5,2,7])
print "select_sort:\n" + str(ll)
# !/usr/bin/env python
# -*- coding : utf-8 -*-
def cocktail_sort(l, start, end):
print l
while start < end:
min_pos = l.index(min(l[start:end]))
max_pos = l.index(max(l[start:end]))
l[start], l[min_pos] = l[min_pos], l[start]
l[end-1], l[max_pos] = l[max_pos], l[end-1]
start += 1
end -= 1
return l
if __name__ == "__main__" :
l = [8,4,3,1,6,9,5,2,7]
ll = cocktail_sort(l, 0, len(l))
print "cocktail_sort:\n" + str(ll)
鸡尾酒排序,每次将选出的最大值和最小值放在序列最后和最前
# !/usr/bin/env python
# -*- coding : utf-8 -*-
def heapify(ll, heap_size, index):
left = 2*index + 1
right = left + 1
large = index
if right < heap_size and ll[large] < ll[right]:
large = right
if left < heap_size and ll[large] < ll[left]:
large = left
if large != index:
ll[large], ll[index] = ll[index], ll[large]
heapify(ll, heap_size, large)
print ll #打印排序过程,因为自己可能没把握
def build_max_heap(ll):
heap_size = len(ll)
for i in range(heap_size/2 - 1, -1, -1):
heapify(ll, heap_size, i)
def heap_sort(ll):
build_max_heap(ll)
for i in range(len(ll) - 1, -1, -1):
ll[0], ll[i] = ll[i], ll[0]
heapify(ll, i, 0)
return ll
if __name__ == "__main__":
l = [6,2,4,0,8,5,7,3,9,1]
l = heap_sort(l)
print l
昨天写好的博客,一直迟迟不敢发表就是因为最后一个堆排序,没把握...
真的实力不太行把!