Task 3:排序 + 二分查找 (3天)

一、 【排序】
实现归并排序、快速排序、插入排序、冒泡排序、选择排序、堆排序(完成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
项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还[基于Python]自己写的一个微信跳一跳自动游戏程序(针对安卓手机)。 全自动运行 自动适应不同分辨率 自动调整各个参数误差.zip行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值