一、 【排序】
实现归并排序、快速排序、插入排序、冒泡排序、选择排序、堆排序(完成leetcode上的返回滑动窗口中的最大值(239),这是上一期第三天的任务进行保留(涉及队列可以对第二天进行整理复习))
归并排序:
def merge(a, b):
c = []
h = j = 0
while j < len(a) and h < len(b):
if a[j] < b[h]:
c.append(a[j])
j += 1
else:
c.append(b[h])
h += 1
if j == len(a):
for i in b[h:]:
c.append(i)
else:
for i in a[j:]:
c.append(i)
return c
def merge_sort(lists):
if len(lists) <= 1:
return lists
middle = len(lists)/2
left = merge_sort(lists[:middle])
right = merge_sort(lists[middle:])
return merge(left, right)
if __name__ == '__main__':
a = [4, 7, 8, 3, 5, 9]
print merge_sort(a)
快速排序:
data = [45,3,2,6,3,78,5,44,22,65,46]
def quickSort(data, start, end):
i = start
j = end
# i与j重合时,一次排序结束
if i >= j:
return
# 设置最左边的数为基准值
flag = data[start]
while i < j:
while i<j and data[j] >= flag:
j -= 1
# 找到右边第一个小于基准的数,赋值给左边i。此时左边i被记录在flag中
data[i] = data[j]
while i<j and data[i] <= flag:
i += 1
# 找到左边第一个大于基准的数,赋值给右边的j。右边的j的值和上面左边的i的值相同
data[j] = data[i]
# 由于循环以i结尾,循环完毕后把flag值放到i所在位置。
data[i] = flag
# 除去i之外两段递归
quickSort(data, start, i-1)
quickSort(data, i+1, end)
quickSort(data,0, len(data)-1)
print data
插入排序:
def insert_sort(ary):
n = len(ary)
for i in range(1,n):
if ary[i] < ary[i-1]:
temp = ary[i]
index = i #待插入的下标
for j in range(i-1,-1,-1): #从i-1 循环到 0 (包括0)
if ary[j] > temp :
ary[j+1] = ary[j]
index = j #记录待插入下标
else :
break
ary[index] = temp
return ary
冒泡排序:
def bubble_sort(list_1):
n = len(list_1)
for j in range(n-1):
for i in range(0,n-1-j):
if list_1[i]>list_1[i+1]:
list_1[i],list_1[i+1] = list_1[i+1],list_1[i]
if __name__ == '__main__':
list_1 = [2,23,42,1,9]
bubble_sort(list_1)
print(list_1)
输出结果:
[1, 2, 9, 23, 42]
选择排序:
#selectSort which is a time-consuming sort algorithm.Its Time-complexity is O(N**2)
#1、we just use the simple sort algorithm to get the smallest number per loop,which the time-consuming is O(n)
#2、next we can define a function that a loop to get the small value every loop,and the append the value into a new Array
#to get the minmum value
def getmin(arr):
min = arr[0];
min_index = 0;
for i in range(0,len(arr)):
if arr[i]<min:
min = arr[i]
min_index = i
return min_index
#SelectSort
def selectSort(arr):
newArr = [];
for i in range(0,len(arr)):
min = getmin(arr);
newArr.append(arr.pop(min))
return newArr;
#test the output
a = [4,6,9,1,3,87,41,5]
print(selectSort(a))
堆排序:
from collections import deque
def swap_param(L, i, j):
L[i], L[j] = L[j], L[i]
return L
def heap_adjust(L, start, end):
temp = L[start]
i = start
j = 2 * i
while j <= end:
if (j < end) and (L[j] < L[j + 1]):
j += 1
if temp < L[j]:
L[i] = L[j]
i = j
j = 2 * i
else:
break
L[i] = temp
def heap_sort(L):
L_length = len(L) - 1
first_sort_count = L_length / 2
for i in range(first_sort_count):
heap_adjust(L, first_sort_count - i, L_length)
for i in range(L_length - 1):
L = swap_param(L, 1, L_length - i)
heap_adjust(L, 1, L_length - i - 1)
return [L[i] for i in range(1, len(L))]
def main():
L = deque([50, 16, 30, 10, 60, 90, 2, 80, 70])
L.appendleft(0)
print heap_sort(L)
if __name__ == '__main__':
main()
这个代码没有跑出结果,报错!待修改
二、 编程实现 O(n) 时间复杂度内找到一组数据的第 K 大元素
【二分查找】
实现一个有序数组的二分查找算法:
#coding:utf8
def binary_search(list,item):
if not list and not item:
print 'he'
return None
low = 0
high = len(list)-1
while low <= high:
mid = (low + high)/2
print 'low %d' % low
print 'high %d' % high
print 'mid %d' % mid
guess = list[mid]
print 'guess %s' % guess
if guess == item:
return seach(list,item,mid)
elif guess > item:
high = mid - 1
elif guess < item:
low = mid + 1
def seach(list,item,mid):
for k,v in enumerate(list[mid:]):
if v == item:
continue
else:
return k+mid
int1 = [2,2,3,3,3,4,5,5,6,6,7,8,8]
intItem = 3
a = binary_search(int1,intItem)
实现模糊二分查找算法(比如大于等于给定值的第一个元素)
循环二分:
def binary_search_cycle(data_set, value):
start = 0
end = len(data_set) - 1
while True:
mid_idx = (start + end) / 2
if data_set[mid_idx] > value:
end = mid_idx
elif data_set[mid_idx] < value:
start = mid_idx
else:
return mid_idx