Python implementation of various sorting algorithm code _python

Source: Internet
Author: User

Copy Code code as follows:

#-*-Coding:utf-8-*-
# Test various sorting algorithms
# link:www.jb51.net
# 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
SORT_ARRAY[J] = tmp
Dist/= 2

#基数排序, all integers, negative numbers and duplicates are not supported
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_LIST[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 '

A very basic knowledge of content, selection, bubbling, inserting, merging, cardinality, and quick-row can be handwritten out, but wrote the discovery heap sort forgot how to do it. To review.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.