排序算法
1.交换排序
1.1冒泡排序
1. # 冒泡排序
2. def bubble_sort_1(alist):
3. for j in range(len(alist)-1,0,-1):
4. for i in range(i):
5. if alist[i]>alist[i+1]:
6. alist[i],alist[i+1]=alist[i+1],alist[i]
7. return alist
8. alist = [54,26,93,17,77,31,44,55,20]
9. print(bubble_sort_1(alist))
1.2快排
1. #一趟快排
2. def quick_sort_pass(arr,low,high):
3. temp = arr[low]
4. i = low
5. j = high
6. while(i<j):
7. while(i<j and arr[j]>temp) :
8. j-=1
9. if(i<j):
10. arr[i] = arr[j]
11. i += 1
12. while(i<j and arr[i]<temp):
13. i += 1
14. if(i<j):
15. arr[j] = arr[i]
16. j -= 1
17.
18. arr[i] = temp
19. return i
20.
21. #快排
22. def quick_sort(arr,low,high):
23. base_point = 0
24. if(low<high):
25. base_point = quick_sort_pass(arr,low,high)
26. quick_sort(arr,low,base_point-1) #对左子序列递归快排
27. quick_sort(arr,base_point+1,high)#对右子序列递归快排
2.插入排序
2.1插入排序
1. #一个数组列表左为有序(只有一个数也为有序),右为无序,将无序部分的第一个数插入左侧有序数组的合适位置
2. def insertionSort(arr):
3. for i in range(1, len(arr)):
4. key = arr[i]
5. j = i-1
6. while j >=0 and key < arr[j] :
7. arr[j+1] = arr[j]
8. j -= 1
9. arr[j+1] = key
10. return arr
11.
12. arr = [54,26,93,17,31,44,55,20,12,342,77]
print(insertionSort(arr))
2.2折半插入排序
1. def binaryInsert(a):
2. # 折半插入排序: 小->大
3. # 在直接插入排序的基础上使用了折半查找的方法
4. for i in xrange(1, len(a)):
5. index = a[i]
6. low = 0
7. hight = i - 1
8. while low <= hight:
9. mid = (low + hight) / 2
10. if index > a[mid]:
11. low = mid + 1
12. else:
13. hight = mid - 1
14. # 跳出循环后 low, mid 都是一样的, hight = low - 1
15. for j in xrange(i, low, -1):
16. a[j] = a[j - 1]
17. a[low] = index
18. return a
2.3 希尔排序
1. #插入排序升级版,希尔排序(缩小增量排序),时间复杂度介于O(n)和O(n^2)之间
2. #把待排序序列分成若干个子序列(使用等比递减的gap实现序列划分,最终gap=1)分别排序,
3. #待整个序列基本有序后,对整个序列进行最后的排序
4. def shellSort(arr):
5.
6. n = len(arr)
7. gap = int(n/2)
8. while gap > 0:
9.
10. for i in range(gap,n):
11.
12. temp = arr[i]
13. j = i
14. while j >= gap and arr[j-gap] >temp:
15. arr[j] = arr[j-gap]
16. j -= gap
17. arr[j] = temp
18. gap = int(gap/2)
19. return arr
20.
21. arr = [ 12, 34, 54, 2, 3]
22. print(shellSort(arr))
3.选择排序
3.1 简单选择排序
1. # 简单选择排序
2. def select_sort(alist):
3. for i in range(len(alist)):
4. k = i
5. for j in range(k,len(alist)):
6. if alist[k]>alist[j]:
7. k = j
8. alist[i],alist[k]=alist[k],alist[i]
9. return alist
10. alist = [54,26,93,17,77,31,44,55,20]
11. print(select_sort(alist))
3.2堆排序
1. def heapify(arr, n, i):
2. largest = i
3. l = 2 * i + 1 # left = 2*i + 1
4. r = 2 * i + 2 # right = 2*i + 2
5.
6. if l < n and arr[i] < arr[l]:
7. largest = l
8.
9. if r < n and arr[largest] < arr[r]:
10. largest = r
11.
12. if largest != i:
13. arr[i],arr[largest] = arr[largest],arr[i] # 交换
14.
15. heapify(arr, n, largest)
16.
17. def heapSort(arr):
18. n = len(arr)
19.
20. # Build a maxheap.
21. for i in range(n, -1, -1):
22. heapify(arr, n, i)
23.
24. # 一个个交换元素
25. for i in range(n-1, 0, -1):
26. arr[i], arr[0] = arr[0], arr[i] # 交换
27. heapify(arr, i, 0)
28.
29. arr = [ 12, 11, 13, 5, 6, 7]
30. heapSort(arr)
31. n = len(arr)
32. print ("排序后")
33. for i in range(n):
34. print ("%d" %arr[i])
4.归并排序
1. def merge(a, b):
2. c = []
3. h = j = 0
4. while j < len(a) and h < len(b):
5. if a[j] < b[h]:
6. c.append(a[j])
7. j += 1
8. else:
9. c.append(b[h])
10. h += 1
11.
12. if j == len(a):
13. for i in b[h:]:
14. c.append(i)
15. else:
16. for i in a[j:]:
17. c.append(i)
18.
19. return c
20.
21.
22. def merge_sort(lists):
23. if len(lists) <= 1:
24. return lists
25. middle = len(lists)/2
26. left = merge_sort(lists[:middle])
27. right = merge_sort(lists[middle:])
28. return merge(left, right)
29.
30.
31. if __name__ == '__main__':
32. a = [4, 7, 8, 3, 5, 9]
33. print merge_sort(a)