##冒泡排序1
2
3
4
5
6def bubble(nums):
for i in range(len(nums)-1):
for j in range(len(nums)-i-1):
if nums[j] > nums[j+1]:
nums[j],nums[j+1] = nums[j+1],nums[j]
return nums
##插入排序1
2
3
4
5
6
7
8def insert_sort(arr):
for i in range(1,len(arr)):
key = arr[i]
j = i-1
while j>0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
##归并排序1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20def merge_sort(alist):
if len(alist) <= 1:
return alist
nums = len(alist)/2
left = merge_sort(alist[:num])
right = merge_sort(alist[num:]
return merge(left,right)
def merge(left,right):
l,r = 0,0
result = []
while l<len(left) and r<len(right):
if left[1] < right[r]
result.append(left[1])
l+=1
else:
result.append(right[r])
r+=1
result += left[l:]
result += rifht[r:]
return result
##快速排序1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16def quick_sort(alist,start,end):
if start >= end:
return
mid = alist[start]
low = start
high = end
while low < high:
while low < high and alist[high] >= mid:
high -= 1
alist[low] = alist[high]
while low < high and alist[low] < mid:
low+=1
alist[high] = alist[low]
alist[low] = mid
quick_sort(alist,start,low-1)
quick_sort(alist,low+1,end)