bubble.py-20180418

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 25 15:18:43 2018

@author: vicky
"""
#冒泡
def bubbleSort(l):
    for i in range(len(l)):
        for j in range(1, len(l) - i):
            if l[j - 1] > l[j]:
                l[j - 1], l[j] = l[j], l[j - 1]#相当于c++中的swap,如果分开写要引入一个中间值
    return l

unsorted_list = [6, 5, 3, 1, 8, 7, 2, 4]
print(bubbleSort(unsorted_list))

#冒泡优化:添加一个标记,在排序已完成时,停止排序。
def bubble_sort_flag(l):
    length = len(l)
    for index in range(length):
        flag = True #标志位
        for j in range(1, length - index):
            if l[j - 1] > l[j]:
                l[j - 1], l[j] = l[j], l[j - 1]
                flag = False
        if flag:
            return l # 没有发生交换,直接返回list
    return l
print(bubble_sort_flag(unsorted_list))

#选择排序
def selection_sort(l):
    n=len(l)
    for i in range (n):
        min = i
        for j in range(i+1,n):
            if l[j]<l[min]: 
                min=j #选出比min小的元素的序号,遍历得到最小值
                l[min],l[i]=l[i],l[min] #交换最小值和第i个值
    return l
print(selection_sort(unsorted_list))

#插入
def insert_sort(l):
    n = len(l)
    for i in range(1, n):
        if l[i] < l[i - 1]:# 后一个元素和前一个元素比较,如果比前一个小
            temp = l[i]# 将这个数取出
            index = i # 保存下标
            for j in range(i - 1, -1, -1): # 从后往前依次比较每个元素
                if l[j] > temp:# 和比取出元素大的元素交换
                    l[j + 1] = l[j] #往后移一个
                    index = j
                else:
                    break
            l[index] = temp # 插入元素
    return l
print(insert_sort(unsorted_list))

#希尔排序
def shell_sort(l):
    n = len(l)
    gap = round(n / 2) #初始步长
    while gap > 0:
        for i in range(gap, n):
            temp = l[i] #每个步长进行插入排序
            j = i
            while j>=gap and l[j-gap]>temp: #插入排序
                l[j] = l[j - gap]
                j -= gap
            l[j] = temp
        gap = round(gap / 2)# 得到新的步长
    return l
print(shell_sort(unsorted_list))

#堆排序
def heap_sort(l):
    # 创建最大堆
    for start in range((len(l) - 2) // 2, -1, -1):
        sift_down(l, start, len(l) - 1)

    # 堆排序
    for end in range(len(l) - 1, 0, -1):
        l[0], l[end] = l[end], l[0]
        sift_down(l, 0, end - 1)
    return l

# 最大堆调整
def sift_down(lst, start, end):
    root = start
    while True:
        child = 2 * root + 1
        if child > end:
            break
        if child + 1 <= end and lst[child] < lst[child + 1]:
            child += 1
        if lst[root] < lst[child]:
            lst[root], lst[child] = lst[child], lst[root]
            root = child
        else:
            break
print(heap_sort(unsorted_list))

#归并排序:把两个已经排列好的序列合并为一个序列。以第一个list为基准,第二个向第一个插空
def merge_sort(list1,list2):
    length_list1=len(list1)
    length_list2=len(list2)
    list3=[]
    j=0
    for i in range(length_list1):
        while list2[j]<list1[i] and j<length_list2:
            list3.append(list2[j])
            j=j+1
        list3.append(list1[i])
    if j<(length_list2-1):
        for k in range(j,length_list2):
            list3.append(list2[k])
    return list3
#测试
list1=[1,3,5,10]
list2=[2,4,6,8,9,11,12,13,14]
print(merge_sort(list1,list2))

# 递归法
def merge_sort(l):
    # 认为长度不大于1的数列是有序的
    if len(l) <= 1:
        return l
    # 二分列表
    middle = len(l) // 2
    left = merge_sort(l[:middle])
    right = merge_sort(l[middle:])
    # 最后一次合并
    return merge(left, right)
# 合并
def merge(left, right):
    l,r=0,0
    result=[]
    while l<len(left) and r<len(right):
        if left[l] <right[r]:
            result.append(left[l])
            l+=1
        else:
            result.append(right[r])
            r +=1
        result+=left[l:]
        result+=right[r:]                
    return result

#快速排序
def quick_sort(l):
    low = []
    mid = []
    high = []
    if len(l) <= 1: # 递归出口
        return l
    else:
        base = list[0] # 将第一个值做为基准
        for i in l:
            if i < base:  # 将比急转小的值放到low数列
                low.append(i)
            elif i > base: # 将比基准大的值放到high数列
                high.append(i)
            else:  # 将和基准相同的值保存在基准数列
                mid.append(i)
        low = quick_sort(low) # 对low数列和high数列继续进行排序
        high = quick_sort(high)
        return low + mid + high

nums = [6,1,2,7,9,3,3,4,5,10,10,8]
print(quick_sort(nums))

#计数排序
def count_sort(l):
    min = 2147483647
    max = 0
    # 取得最大值和最小值
    for x in l:
        if x < min:
            min = x
        if x > max:
            max = x
    # 创建数组C
    count = [0] * (max - min +1)
    for index in l:
        count[index - min] += 1
    index = 0
    # 填值
    for a in range(max - min+1):
        for c in range(count[a]):
            l[index] = a + min
            index += 1
    return l

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解释下面代码 addBubble: function (bubble) { var thisBubble = this.getBubble(bubble.x, bubble.y); thisBubble.color = bubble.color; }, setBubble: function (x, y, color) { this.getBubble(x, y).color = color; }, getBubble: function (x, y) { if (x < 0 || y < 0 || x > game.cellCount || y > game.cellCount) return null; return this.bubbles[y][x]; }, isEmpty: function (x, y) { var bubble = this.getBubble(x, y); return !bubble.color; }, }; var Cell = function (x, y) { this.x = x; this.y = y; } var Bubble = function (x, y, color) { this.x = x; this.y = y; this.px = game.cellWidth * (this.x + 1) - game.cellWidth / 2; this.py = game.cellWidth * (this.y + 1) - game.cellWidth / 2; this.color = color; this.light = 10; }; Bubble.prototype.draw = function () { if (!this.color) { return; } var ctx = game.ctx; ctx.beginPath(); //console.log("x:" + px + "y:" + py); var gradient = ctx.createRadialGradient(this.px - 5, this.py - 5, 0, this.px, this.py, this.light); gradient.addColorStop(0, "white"); gradient.addColorStop(1, this.color); ctx.arc(this.px, this.py, 11, 0, Math.PI * 2); ctx.strokeStyle = this.color; ctx.fillStyle = gradient; ctx.fill(); ctx.stroke(); }; Bubble.prototype.play = function () { var me = this; var isUp = true; game.play("light_" + this.x + "" + this.y, function () { if (isUp) { me.light += 3; } if (!isUp) { me.light -= 3; } if (me.light >= 30) { isUp = false; } if (me.light <= 10) { isUp = true; } }, 50); }; Bubble.prototype.stop = function () { //this.light = 10; var me = this; game.stop("light" + this.x + "" + this.y); game.play("restore" + this.x + "" + this.y, function () { if (me.light > 10) { me.light--; } else { me.light = 10; game.stop("restore" + me.x + "_" + me.y); } }, 50); }; game.start(); </script> <div style="text-align:center;"> </div> <script src="http://www.mycodes.net/js/tongji.js"></script> <script src="http://www.mycodes.net/js/youxia.js" type="text/javascript">
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值