The code is as follows:
#-*-Coding:utf-8-*-
# Test various sorting algorithms
# link:www.bitsCN.com
# DATE:2013/2/2
#选择排序
def select_sort (Sort_array):
For I, Elem in Enumerate (Sort_array):
For J, Elem in Enumerate (sort_array[i:]):
If sort_array[i] > sort_array[j + i]:
#交换
Sort_array[i], sort_array[j + i] = sort_array[j + i], sort_array[i]
#冒泡排序
def bubble_sort (Sort_array):
For I, Elem in Enumerate (Sort_array):
For J, Elem in Enumerate (Sort_array[:len (Sort_array)-i-1]):
If SORT_ARRAY[J] > sort_array[j + 1]:
SORT_ARRAY[J], sort_array[j + 1] = sort_array[j + 1], sort_array[j]
#插入排序
def insert_sort (Sort_array):
For I, Elem in Enumerate (Sort_array):
For J, Elem in Enumerate (sort_array[:i]):
If SORT_ARRAY[J] > Sort_array[i]:
Sort_array.insert (J, Sort_array[i])
Del sort_array[i + 1]
#归并排序
def merge_sort_wrapper (Sort_array):
Merge_sort (Sort_array, 0, Len (sort_array)-1)
def merge_sort (Sort_array, left = 0, right = 0):
If left < right:
Center = (left + right)/2
Merge_sort (Sort_array, left, center)
Merge_sort (Sort_array, center + 1, right)
Merge (Sort_array, left, right, center)
def merge (Sort_array, left, right, center):
result = []
Arraya = Sort_array[left:center + 1]
Arrayb = sort_array[center + 1:right + 1]
while (len (Arraya > 0) and (len (Arrayb) > 0)):
if (Arraya[0] > arrayb[0]):
Result.append (Arrayb.pop (0))
Else
Result.append (Arraya.pop (0))
if (len (Arraya) > 0):
Result.extend (Arraya)
if (len (Arrayb) > 0):
Result.extend (Arrayb)
Sort_array[left:right + 1] = result
#快排
def quick_sort (sort_array):
if (len (Sort_array) < 2):
return
left = [x for x in sort_array[1: ] If x < sort_array[0]]
right = [x for x in sort_array[1:] if x >= sort_array[0]]
Quick_sort (left)
Quick _sort (right)
sort_array[:] = left + [sort_array[0]] + right
#shell排序
def shell_sort (sort_array):
Dist =len (Sort_array)/2
While dist > 0:
for i in Range (Dist,len (Sort_array)):
Tmp=sort_array[i]
J = i while J >= Dist and TMP < Sort_array[j-dist]:
Sort_array[j] = sort_array[j-dist]
J-= Dist
Sor T_ARRAY[J] = tmp
Dist/= 2
#基数排序, all integers, negative and repeat
def radix_sort (sort_array):
Max_elem = max (Sort_array)
Bucket_list = []
for I in Range (Max_elem):
Bucket_list.insert (i, 0)
for x in Sort_array:
bucket_l IST[X-1] =-1
sort_array[:] = [x + 1 for x in range (len (bucket_list)) if bucket_list[x] = = 1]
#堆排序
def heap_sort (Sort_array):
#没有写出来, think again.
Pass
#测试例子
def algo_sort_test (Sort_array, Sort_method):
Sort_method (Sort_array)
if __name__ = = ' __main__ ':
Sort_array = [1, 2, 3, 5,-4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
Algo_sort_test (Sort_array, Select_sort)
Print Sort_array
Sort_array = [1, 2, 3, 5,-4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
Algo_sort_test (Sort_array, Bubble_sort)
Print Sort_array
Sort_array = [1, 2, 3, 5,-4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
Algo_sort_test (Sort_array, Insert_sort)
Print Sort_array
Sort_array = [1, 2, 3, 5,-4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
Algo_sort_test (Sort_array, Merge_sort_wrapper)
Print Sort_array
Sort_array = [1, 2, 3, 5,-4, 4, 10, 300, 19, 13, 16, 18, 500, 190, 456, 23]
Algo_sort_test (Sort_array, Quick_sort)
Print Sort_array
Sort_array = [1, 2, 3, 5,-4, 4, 10, 3, 19, 13, 16, 18, 5, 190, 456, 23]
Algo_sort_test (Sort_array, Shell_sort)
Print Sort_array
Sort_array = [1, 2, 3, 5, 4, 10, 19, 13, 16, 18, 190, 456, 23]
Algo_sort_test (Sort_array, Radix_sort)
Print Sort_array
print ' OK '
Very basic knowledge of the content, selection, bubbling, inserting, merging, cardinality, and the fast line can be handwritten, but wrote once found the heap sort forgot how to do. To review.