前段时间为准备百度面试恶补的东西,虽然最后还是被刷了,还是把那几天的“战利品”放点上来,算法一直是自己比较薄弱的地方,以后还要更加努力啊。
下面用Python实现了几个常用的排序,如快速排序,选择排序,以及二路并归排序等等。
复制代码 代码如下:
#encoding=utf-8
import random
from copy import copy
def directInsertSort(seq):
""" 直接插入排序 """
size = len(seq)
for i in range(1,size):
tmp, j = seq[i], i
while j > 0 and tmp < seq[j-1]:
seq[j], j = seq[j-1], j-1
seq[j] = tmp
return seq
def directSelectSort(seq):
""" 直接选择排序 """
size = len(seq)
for i in range(0,size - 1):
k = i;j = i+1
while j < size:
if seq[j] < seq[k]:
k = j
j += 1
seq[i],seq[k] = seq[k],seq[i]
return seq
def bubbleSort(seq):
"""冒泡排序"""
size = len(seq)
for i in range(1,size):
for j in range(0,size-i):
if seq[j+1] < seq[j]:
seq[j+1],seq[j] = seq[j],seq[j+1]
return seq
def _divide(seq, low, high):
"""快速排序划分函数"""
tmp = seq[low]
while low != high:
while low < high and seq[high] >= tmp: high -= 1
if low < high:
seq[low] = seq[high]
low += 1
while low < high and seq[low] <= tmp: low += 1
if low < high:
seq[high] = seq[low]
high -= 1
seq[low] = tmp
return low
def _quickSort(seq, low, high):
"""快速排序辅助函数"""
if low >= high: return
mid = _divide(seq, low, high)
_quickSort(seq, low, mid - 1)
_quickSort(seq, mid + 1, high)
def quickSort(seq):
"""快速排序包裹函数"""
size = len(seq)
_quickSort(seq, 0, size - 1)
return seq
def merge(seq, left, mid, right):
tmp = []
i, j = left, mid
while i < mid and j <= right:
if seq[i] < seq[j]:
tmp.append(seq[i])
i += 1
else:
tmp.append(seq[j])
j += 1
if i < mid: tmp.extend(seq[i:])
if j <= right: tmp.extend(seq[j:])
seq[left:right+1] = tmp[0:right-left+1]
def _mergeSort(seq, left, right):
if left == right:
return
else:
mid = (left + right) / 2
_mergeSort(seq, left, mid)
_mergeSort(seq, mid + 1, right)
merge(seq, left, mid+1, right)
#二路并归排序
def mergeSort(seq):
size = len(seq)
_mergeSort(seq, 0, size - 1)
return seq
if __name__ == '__main__':
s = [random.randint(0,100) for i in range(0,20)]
print s
print "\n"
print directSelectSort(copy(s))
print directInsertSort(copy(s))
print bubbleSort(copy(s))
print quickSort(copy(s))
print mergeSort(copy(s))
运行结果如下:
复制代码 代码如下:
E:\python_project\practice>sorting.py
[10, 47, 56, 76, 64, 84, 26, 8, 47, 51, 88, 81, 32, 95, 91, 29, 28, 69, 61, 45]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
Python,常用排序算法
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]