{"id":1074,"date":"2023-03-24T09:47:07","date_gmt":"2023-03-24T01:47:07","guid":{"rendered":""},"modified":"2023-03-24T09:47:07","modified_gmt":"2023-03-24T01:47:07","slug":"Python \u6392\u5e8f\u7b97\u6cd5","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/1074.html","title":{"rendered":"Python \u6392\u5e8f\u7b97\u6cd5"},"content":{"rendered":"
\n
Python \u6392\u5e8f\u7b97\u6cd5\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n \u5192\u6ce1\u6392\u5e8f<\/span> <\/body>
\n
\u6392\u5e8f\u7684\u91cd\u8981\u6027\u5728\u4e8e\uff0c\u5982\u679c\u4ee5\u6392\u5e8f\u65b9\u5f0f\u5b58\u50a8\u6570\u636e\uff0c\u5219\u53ef\u4ee5\u5c06\u6570\u636e\u641c\u7d22\u4f18\u5316\u5230\u5f88\u9ad8\u7684\u6c34\u5e73\u3002\u6392\u5e8f\u8fd8\u7528\u4e8e\u4ee5\u66f4\u5177\u53ef\u8bfb\u6027\u7684\u683c\u5f0f\u8868\u793a\u6570\u636e\u3002\u4e0b\u9762\u6211\u4eec\u770b\u5230\u5728python\u4e2d\u8fdb\u884c\u6392\u5e8f\u7684\u4e94\u4e2a\u6b64\u7c7b\u5b9e\u73b0\u3002\n <\/div>\n
\n \u5408\u5e76\u6392\u5e8f<\/span>
\n \u63d2\u5165\u6392\u5e8f<\/span>
\n \u5e0c\u5c14\u6392\u5e8f<\/span>
\n \u9009\u62e9\u6392\u5e8f<\/span> <\/p>\n\u5192\u6ce1\u6392\u5e8f<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>bubblesort(list):
# Swap the elements to arrange in <\/span>order
<\/span> for <\/span><\/span>iter_num in <\/span>range(len(list)-1,0,-1):
for <\/span><\/span>idx in <\/span>range(iter_num):
if <\/span>list[idx]>list[idx+1]:
temp = list[idx]
list[idx] = list[idx+1]
list[idx+1] = temp
list = [19,2,31,45,6,11,121,27]
bubblesort(list)
print(list)
<\/span><\/code><\/pre>\n<\/p><\/div>\n [2, 6, 11, 19, 27, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5408\u5e76\u6392\u5e8f<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>merge_sort(unsorted_list):
if <\/span>len(unsorted_list) <= 1:
return <\/span>unsorted_list
# Find the middle point and <\/span>devide it
<\/span> middle = len(unsorted_list) \/\/ 2
left_list = unsorted_list[:middle]
right_list = unsorted_list[middle:]
left_list = merge_sort(left_list)
right_list = merge_sort(right_list)
return <\/span>list(merge(left_list, right_list))
# Merge the sorted halves
<\/span> def <\/span>merge(left_half,right_half):
res = []
while <\/span>len(left_half) != 0 and <\/span>len(right_half) != 0:
if <\/span>left_half[0] < right_half[0]:
res.append<\/span>(left_half[0])
left_half.remove<\/span>(left_half[0])
else:<\/span>
res.append<\/span>(right_half[0])
right_half.remove<\/span>(right_half[0])
if <\/span>len(left_half) == 0:
res = res + right_half
else:<\/span>
res = res + left_half
return <\/span>res
unsorted_list = [64, 34, 25, 12, 22, 11, 90]
print(merge_sort(unsorted_list))
<\/span><\/code><\/pre>\n<\/p><\/div>\n [11, 12, 22, 25, 34, 64, 90]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u63d2\u5165\u6392\u5e8f<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>insertion_sort(InputList):
for <\/span><\/span>i in <\/span>range(1, len(InputList)):
j = i-1
nxt_element = InputList[i]
# Compare the current element with <\/span>next one
<\/span>
while <\/span>(InputList[j] > nxt_element) and <\/span>(j >= 0):
InputList[j+1] = InputList[j]
j=j-1
InputList[j+1] = nxt_element
list = [19,2,31,45,30,11,121,27]
insertion_sort(list)
print(list)
<\/span><\/code><\/pre>\n<\/p><\/div>\n [2, 11, 19, 27, 30, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u5e0c\u5c14\u6392\u5e8f<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>shellSort(input_list):
gap = len(input_list) \/\/ 2
while <\/span>gap > 0:
for <\/span><\/span>i in <\/span>range(gap, len(input_list)):
temp = input_list[i]
j = i
# Sort the sub list for <\/span><\/span>this <\/span>gap
<\/span> while <\/span>j >= gap and <\/span>input_list[j - gap] > temp:
input_list[j] = input_list[j - gap]
j = j-gap
input_list[j] = temp
# Reduce the gap for <\/span><\/span>the next element
<\/span> gap = gap\/\/2
list = [19,2,31,45,30,11,121,27]
shellSort(list)
print(list)
<\/span><\/code><\/pre>\n<\/p><\/div>\n [2, 11, 19, 27, 30, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n\u9009\u62e9\u6392\u5e8f<\/h2>\n
# Filename : example.py<\/span>
# Copyright : 2020 By Lidihuo<\/span>
# Author by : www.lidihuo.com<\/span>
# Date : 2020-08-15<\/span>
def <\/span>selection_sort(input_list):
for <\/span><\/span>idx in <\/span>range(len(input_list)):
min_idx = idx
for <\/span><\/span>j in <\/span>range( idx +1, len(input_list)):
if <\/span>input_list[min_idx] > input_list[j]:
min_idx = j
# Swap the minimum value with <\/span>the compared value
<\/span> input_list[idx], input_list[min_idx] = input_list[min_idx], input_list[idx]
l = [19,2,31,45,30,11,121,27]
selection_sort(l)
print(l)
<\/span><\/code><\/pre>\n<\/p><\/div>\n [2, 11, 19, 27, 30, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"Python \u6392\u5e8f\u7b97\u6cd5zh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[],"class_list":["post-1074","post","type-post","status-publish","format-standard","hentry","category-python3"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1074"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=1074"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/1074\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=1074"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=1074"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=1074"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}