This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 二分查找 | |
def binarySearch(li, item): | |
# Search | |
print("[*] Binary Search Start") | |
timeStart = time.time() # Timer | |
n = 1 # Counter | |
low = 0 | |
high= len(li) - 1 | |
while low <= high: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bubble(bubbleList): | |
listLength = len(bubbleList) | |
while listLength > 0: | |
for i in range(listLength - 1): | |
if bubbleList[i] > bubbleList[i+1]: | |
bubbleList[i] = bubbleList[i] + bubbleList[i+1] | |
bubbleList[i+1] = bubbleList[i] - bubbleList[i+1] | |
bubbleList[i] = bubbleList[i] - bubbleList[i+1] | |
listLength -= 1 | |
print bubbleList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def selection_sort(list2): | |
for i in range(0, len (list2)-1): | |
min_ = i | |
for j in range(i + 1, len(list2)): | |
if list2[j] < list2[min_]: | |
min_ = j | |
list2[i], list2[min_] = list2[min_], list2[i] # swap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
Range = 100 | |
Length = 5 | |
list = random.sample(range(Range),Length) #在指定序列中随机获取指定长度片段 | |
print('before sort:',list) | |
for i in range(1,Length): #默认第一个元素已经在有序序列中,从后面元素开始插入 | |
for j in range(i,0,-1): #逆向遍历比较,交换位置实现插入 | |
if list[j] < list[j-1]: | |
list[j],list[j-1] = list[j-1],list[j] | |
print('after sort:',list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def MergeSort(lists): | |
if len(lists) <= 1: | |
return lists | |
num = int( len(lists) / 2 ) | |
left = MergeSort(lists[:num]) | |
right = MergeSort(lists[num:]) | |
return Merge(left, right) | |
def Merge(left,right): | |
r, l=0, 0 | |
result=[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/envpython | |
#-*-coding:utf-8-*- | |
def heap_sort(lst): | |
for startinrange((len(lst)-2)/2,-1,-1): | |
sift_down(lst,start,len(lst)-1) | |
for endinrange(len(lst)-1,0,-1): | |
lst[0],lst[end]=lst[end],lst[0] | |
sift_down(lst,0,end-1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#encoding=utf-8 | |
import math | |
def sort(a, radix=10): | |
"""a为整数列表, radix为基数""" | |
K = int(math.ceil(math.log(max(a), radix))) # 用K位数可表示任意整数 | |
bucket = [[] for i in range(radix)] # 不能用 [[]]*radix | |
for i in range(1, K+1): # K次循环 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def shell(arr): | |
n=len(arr) | |
h=1 | |
while h<n/3: | |
h=3*h+1 | |
while h>=1: | |
for i in range(h,n): | |
j=i | |
while j>=h and arr[j]<arr[j-h]: | |
arr[j], arr[j-h] = arr[j-h], arr[j] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# GET请求 | |
r = requests.get('https://api.github.com/events') | |
# POST请求 | |
r = requests.post('http://httpbin.org/post', data = {'key':'value'}) | |
# 其他 | |
r = requests.put('http://httpbin.org/put', data = {'key':'value'}) | |
r = requests.delete('http://httpbin.org/delete') | |
r = requests.head('http://httpbin.org/get') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 导入pymysql模块 | |
import pymysql | |
# 连接database | |
conn = pymysql.connect( | |
host=“你的数据库地址”, | |
user=“用户名”,password=“密码”, | |
database=“数据库名”, | |
charset=“utf8”) | |
OlderNewer