快速排序:
# 非递归版本
def quick_sort(arr):
if len(arr) < 2:
return arr
position = [0, len(arr)-1]
stack = []
stack.append(position)
while stack:
l, r = stack.pop()
index = partition(arr, l, r)
if l < index - 1:
stack.append([l, index - 1])
if r > index + 1:
stack.append([index + 1, r])
def partition(arr, start, end):
# 分区操作,返回基准线下标
pivot = arr[start]
# 从两边来,左边移动一个,就到右边移动一个,保持平衡
while start < end:
while start < end and arr[end] >= pivot:
end -= 1
arr[start] = arr[end]
while start < end and arr[start] <= pivot:
start += 1
arr[end] = arr[start]
# 此时start = end
arr[start] = pivot
return start
f = quick_sort(p)
print(p)
############################## 递归版本没啥用 ########################################
# quick
def q_sort(list):
less, mid, more = [], [], []
if len(list) <= 1:
return list
else:
m = list[0]
for i in list:
if i > m:
more.append(i)
elif i < m:
less.append(i)
else:
mid.append(i)
less = q_sort(less)
more = q_sort(more)
return less + mid + more
def q_sort_1(L):
if len(L) < 2:
return L
pivot_element = random.choice(L)
small = [i for i in L if i < pivot_element]
medium = [i for i in L if i == pivot_element]
large = [i for i in L if i > pivot_element]
return q_sort(small) + medium + q_sort(large)
并归排序:
# 非递归实现
def merge(seq,low,mid,height):
"""合并两个已排序好的列表,产生一个新的已排序好的列表"""
# 通过low,mid height 将[low:mid) [mid:height)提取出来
left = seq[low:mid]
right = seq[mid:height]
print('left:%s'% left, 'right:%s' % right)
k = 0 #left的下标
j = 0 #right的下标
result = [] #保存本次排序好的内容
#将最小的元素依次添加到result数组中
while k < len(left) and j < len(right):
if left[k] <= right[j]:
result.append(left[k])
k += 1
else:
result.append(right[j])
j += 1
#将对比完后剩余的数组内容 添加到已排序好数组中
result += left[k:]
result += right[j:]
#将原始数组中[low:height)区间 替换为已经排序好的数组
seq[low:height] = result
print("seq:", seq)
seq = [5, 4, 3, 0, 1, 2, 7, 5, 11,9]
i = 1
while i < len(seq):
print('子数组 长度 : ',i)
low = 0
while low < len(seq):
mid = low + i
height = min(low + 2 * i, len(seq))
if mid < height:
print('low ',low,'mid:',mid,'height:',height)
merge(seq,low,mid,height)
low += 2*i
i *= 2
########################## 递归实现 ##########################
# merge
def m_sort(list):
if len(list) <= 1:
return list
mid = len(list) // 2
left = m_sort(list[:mid])
right = m_sort(list[mid:])
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:]
print(result)
return result
冒泡排序:
# bubble
def b_sort(list):
n = len(list)
for i in range(n):
changed = False
for j in range(1, n - i):
if list[j - 1] > list[j]:
list[j - 1], list[j] = list[j], list[j - 1]
changed = True
if not changed:
break
return list
插入排序:
# insert
def i_sort(list):
n = len(list)
for i in range(1, n):
key = list[i]
j = i - 1
while j >= 0:
if list[j] > key:
list[j], list[j + 1] = key, list[j]
else:
break
j -= 1
return list
选择排序:
# select
def s_sort(list):
n = len(list)
for i in range(n):
# min = i
for j in range(i + 1, n):
if list[j] < list[i]:
# min = j
list[i], list[j] = list[j], list[i]
print(list)
return list
def s_sort_1(list):
n = len(list)
for i in range(n):
min = i
for j in range(i + 1, n):
if list[j] < list[min]:
min = j
list[i], list[min] = list[min], list[i]
print(list)
return list
桶排序:
def t_sort(list):
n = len(list)
max, min = list[0], list[0]
for i in list:
if i > max:
max = i
elif i < min:
min = i
s = [0 for i in range(max - min + 1)]
for i in list:
s[i - min] += 1
list2 = []
# from ipdb import set_trace
# set_trace()
for j in range(len(s)):
key = s[j]
while key > 0:
list2.append(min + j)
key -= 1
return list2
if __name__ == '__main__':
list = [6, 5, 3, 1, 8, 7, 2, 4, 4, 4, 5, 7, 89, 455, 765, 23, 1, 23, 435, 546, 567, 56, 8567, 10]
print(b_sort(list))
print(q_sort(list))
print(i_sort(list))
print(s_sort(list))
print(m_sort(list))
print(t_sort(list))
啥玩意啊
def h(str1):
str1 = '#'.join(str1)
n = len(str1)
key = {}
max = 0
for i in range(n):
left = i + 1
right = n - i
list1 = [str1[i]]
# if left < right:
# for j in range(1,left):
# if str1[i-j] == str1[i+j]:
# list1.append(str1[i+j])
# else:
# break
# else:
# for j in range(1,right):
# if str1[i-j] == str1[i+j]:
# list1.append(str1[i+j])
# else:
# break
for j in range(1, min(left, right)):
if str1[i - j] == str1[i + j]:
list1.append(str1[i + j])
else:
break
if len(list1) > max:
max = len(list1)
key['max'] = list1
return key