快速排序:

  1. # 非递归版本
  2. def quick_sort(arr):
  3. if len(arr) < 2:
  4. return arr
  5. position = [0, len(arr)-1]
  6. stack = []
  7. stack.append(position)
  8. while stack:
  9. l, r = stack.pop()
  10. index = partition(arr, l, r)
  11. if l < index - 1:
  12. stack.append([l, index - 1])
  13. if r > index + 1:
  14. stack.append([index + 1, r])
  15. def partition(arr, start, end):
  16. # 分区操作,返回基准线下标
  17. pivot = arr[start]
  18. # 从两边来,左边移动一个,就到右边移动一个,保持平衡
  19. while start < end:
  20. while start < end and arr[end] >= pivot:
  21. end -= 1
  22. arr[start] = arr[end]
  23. while start < end and arr[start] <= pivot:
  24. start += 1
  25. arr[end] = arr[start]
  26. # 此时start = end
  27. arr[start] = pivot
  28. return start
  29. f = quick_sort(p)
  30. print(p)
  31. ############################## 递归版本没啥用 ########################################
  32. # quick
  33. def q_sort(list):
  34. less, mid, more = [], [], []
  35. if len(list) <= 1:
  36. return list
  37. else:
  38. m = list[0]
  39. for i in list:
  40. if i > m:
  41. more.append(i)
  42. elif i < m:
  43. less.append(i)
  44. else:
  45. mid.append(i)
  46. less = q_sort(less)
  47. more = q_sort(more)
  48. return less + mid + more
  49. def q_sort_1(L):
  50. if len(L) < 2:
  51. return L
  52. pivot_element = random.choice(L)
  53. small = [i for i in L if i < pivot_element]
  54. medium = [i for i in L if i == pivot_element]
  55. large = [i for i in L if i > pivot_element]
  56. return q_sort(small) + medium + q_sort(large)

并归排序:

  1. # 非递归实现
  2. def merge(seq,low,mid,height):
  3. """合并两个已排序好的列表,产生一个新的已排序好的列表"""
  4. # 通过low,mid height 将[low:mid) [mid:height)提取出来
  5. left = seq[low:mid]
  6. right = seq[mid:height]
  7. print('left:%s'% left, 'right:%s' % right)
  8. k = 0 #left的下标
  9. j = 0 #right的下标
  10. result = [] #保存本次排序好的内容
  11. #将最小的元素依次添加到result数组中
  12. while k < len(left) and j < len(right):
  13. if left[k] <= right[j]:
  14. result.append(left[k])
  15. k += 1
  16. else:
  17. result.append(right[j])
  18. j += 1
  19. #将对比完后剩余的数组内容 添加到已排序好数组中
  20. result += left[k:]
  21. result += right[j:]
  22. #将原始数组中[low:height)区间 替换为已经排序好的数组
  23. seq[low:height] = result
  24. print("seq:", seq)
  25. seq = [5, 4, 3, 0, 1, 2, 7, 5, 11,9]
  26. i = 1
  27. while i < len(seq):
  28. print('子数组 长度 : ',i)
  29. low = 0
  30. while low < len(seq):
  31. mid = low + i
  32. height = min(low + 2 * i, len(seq))
  33. if mid < height:
  34. print('low ',low,'mid:',mid,'height:',height)
  35. merge(seq,low,mid,height)
  36. low += 2*i
  37. i *= 2
  38. ########################## 递归实现 ##########################
  39. # merge
  40. def m_sort(list):
  41. if len(list) <= 1:
  42. return list
  43. mid = len(list) // 2
  44. left = m_sort(list[:mid])
  45. right = m_sort(list[mid:])
  46. return merge(left, right)
  47. def merge(left, right):
  48. l, r = 0, 0
  49. result = []
  50. while l < len(left) and r < len(right):
  51. if left[l] < right[r]:
  52. result.append(left[l])
  53. l += 1
  54. else:
  55. result.append(right[r])
  56. r += 1
  57. result += left[l:]
  58. result += right[r:]
  59. print(result)
  60. return result

冒泡排序:

  1. # bubble
  2. def b_sort(list):
  3. n = len(list)
  4. for i in range(n):
  5. changed = False
  6. for j in range(1, n - i):
  7. if list[j - 1] > list[j]:
  8. list[j - 1], list[j] = list[j], list[j - 1]
  9. changed = True
  10. if not changed:
  11. break
  12. return list

插入排序:

  1. # insert
  2. def i_sort(list):
  3. n = len(list)
  4. for i in range(1, n):
  5. key = list[i]
  6. j = i - 1
  7. while j >= 0:
  8. if list[j] > key:
  9. list[j], list[j + 1] = key, list[j]
  10. else:
  11. break
  12. j -= 1
  13. return list

选择排序:

  1. # select
  2. def s_sort(list):
  3. n = len(list)
  4. for i in range(n):
  5. # min = i
  6. for j in range(i + 1, n):
  7. if list[j] < list[i]:
  8. # min = j
  9. list[i], list[j] = list[j], list[i]
  10. print(list)
  11. return list
  12. def s_sort_1(list):
  13. n = len(list)
  14. for i in range(n):
  15. min = i
  16. for j in range(i + 1, n):
  17. if list[j] < list[min]:
  18. min = j
  19. list[i], list[min] = list[min], list[i]
  20. print(list)
  21. return list

桶排序:

  1. def t_sort(list):
  2. n = len(list)
  3. max, min = list[0], list[0]
  4. for i in list:
  5. if i > max:
  6. max = i
  7. elif i < min:
  8. min = i
  9. s = [0 for i in range(max - min + 1)]
  10. for i in list:
  11. s[i - min] += 1
  12. list2 = []
  13. # from ipdb import set_trace
  14. # set_trace()
  15. for j in range(len(s)):
  16. key = s[j]
  17. while key > 0:
  18. list2.append(min + j)
  19. key -= 1
  20. return list2
  21. if __name__ == '__main__':
  22. 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]
  23. print(b_sort(list))
  24. print(q_sort(list))
  25. print(i_sort(list))
  26. print(s_sort(list))
  27. print(m_sort(list))
  28. print(t_sort(list))

啥玩意啊

  1. def h(str1):
  2. str1 = '#'.join(str1)
  3. n = len(str1)
  4. key = {}
  5. max = 0
  6. for i in range(n):
  7. left = i + 1
  8. right = n - i
  9. list1 = [str1[i]]
  10. # if left < right:
  11. # for j in range(1,left):
  12. # if str1[i-j] == str1[i+j]:
  13. # list1.append(str1[i+j])
  14. # else:
  15. # break
  16. # else:
  17. # for j in range(1,right):
  18. # if str1[i-j] == str1[i+j]:
  19. # list1.append(str1[i+j])
  20. # else:
  21. # break
  22. for j in range(1, min(left, right)):
  23. if str1[i - j] == str1[i + j]:
  24. list1.append(str1[i + j])
  25. else:
  26. break
  27. if len(list1) > max:
  28. max = len(list1)
  29. key['max'] = list1
  30. return key