Skip to content

Instantly share code, notes, and snippets.

View Xifeng2009's full-sized avatar
🎯
Focusing

Xifeng2009 Xifeng2009

🎯
Focusing
View GitHub Profile
# 二分查找
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:
@Xifeng2009
Xifeng2009 / InsertionSort
Created September 14, 2018 03:05
插入排序
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)
@Xifeng2009
Xifeng2009 / MergeSort
Created September 14, 2018 03:13
归并排序
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=[]
@Xifeng2009
Xifeng2009 / SelectionSort
Last active September 14, 2018 03:14
选择排序
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
@Xifeng2009
Xifeng2009 / BubbleSort
Last active September 14, 2018 04:04
冒泡排序
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
@Xifeng2009
Xifeng2009 / HeapSort
Last active September 14, 2018 05:16
堆排序
#!/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)
@Xifeng2009
Xifeng2009 / RadixSort
Created September 14, 2018 05:19
基数排序
#!/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次循环
@Xifeng2009
Xifeng2009 / ShellSort
Created September 14, 2018 05:28
希尔排序
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]
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')
# 导入pymysql模块
import pymysql
# 连接database
conn = pymysql.connect(
host=“你的数据库地址”,
user=“用户名”,password=“密码”,
database=“数据库名”,
charset=“utf8”)