{"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 <\/head>
\n <\/p>\n

\n

Python \u6392\u5e8f\u7b97\u6cd5<\/h1>\n

Python \u6392\u5e8f\u7b97\u6cd5\u8be6\u7ec6\u64cd\u4f5c\u6559\u7a0b<\/span>\n <\/div>\n

\n \u6392\u5e8f\u662f\u6307\u4ee5\u7279\u5b9a\u683c\u5f0f\u6392\u5217\u6570\u636e\u3002\u6392\u5e8f\u7b97\u6cd5\u6307\u5b9a\u4e86\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5217\u6570\u636e\u7684\u65b9\u5f0f\u3002\u6700\u5e38\u89c1\u7684\u987a\u5e8f\u662f\u6309\u6570\u5b57\u987a\u5e8f\u6216\u5b57\u5178\u987a\u5e8f\u3002
\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

\u5192\u6ce1\u6392\u5e8f<\/span>
\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
\n \u8fd9\u662f\u4e00\u79cd\u57fa\u4e8e\u6bd4\u8f83\u7684\u7b97\u6cd5\uff0c\u5176\u4e2d\u6bd4\u8f83\u6bcf\u5bf9\u76f8\u90bb\u5143\u7d20\uff0c\u5982\u679c\u5143\u7d20\u987a\u5e8f\u4e0d\u6b63\u786e\uff0c\u5219\u5c06\u5176\u4ea4\u6362\u3002\n <\/div>\n
\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
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 [2, 6, 11, 19, 27, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5408\u5e76\u6392\u5e8f<\/h2>\n
\n \u5408\u5e76\u6392\u5e8f\u9996\u5148\u5c06\u6570\u7ec4\u5206\u6210\u76f8\u7b49\u7684\u4e24\u534a\uff0c\u7136\u540e\u4ee5\u6392\u5e8f\u7684\u65b9\u5f0f\u5c06\u5b83\u4eec\u5408\u5e76\u3002\n <\/div>\n
\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
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 [11, 12, 22, 25, 34, 64, 90]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u63d2\u5165\u6392\u5e8f<\/h2>\n
\n \u63d2\u5165\u6392\u5e8f\u6d89\u53ca\u5728\u6392\u5e8f\u5217\u8868\u4e2d\u627e\u5230\u7ed9\u5b9a\u5143\u7d20\u7684\u6b63\u786e\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0c\u4ece\u4e00\u5f00\u59cb\u6211\u4eec\u5c31\u6bd4\u8f83\u524d\u4e24\u4e2a\u5143\u7d20\uff0c\u5e76\u901a\u8fc7\u6bd4\u8f83\u5c06\u5b83\u4eec\u6392\u5e8f\u3002\u7136\u540e\uff0c\u6211\u4eec\u9009\u62e9\u7b2c\u4e09\u4e2a\u5143\u7d20\u5e76\u627e\u5230\u5176\u5728\u524d\u4e24\u4e2a\u6392\u5e8f\u5143\u7d20\u4e2d\u7684\u6b63\u786e\u4f4d\u7f6e\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6211\u4eec\u9010\u6e10\u5c06\u66f4\u591a\u5143\u7d20\u6dfb\u52a0\u5230\u5df2\u7ecf\u6392\u5e8f\u7684\u5217\u8868\u4e2d\uff0c\u65b9\u6cd5\u662f\u5c06\u5b83\u4eec\u653e\u7f6e\u5728\u9002\u5f53\u7684\u4f4d\u7f6e\u3002\n <\/div>\n
\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
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 [2, 11, 19, 27, 30, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u5e0c\u5c14\u6392\u5e8f<\/h2>\n
\n Shell Sort\u6d89\u53ca\u5bf9\u5176\u4ed6\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u7684\u5143\u7d20\u3002\u6211\u4eec\u5bf9\u7ed9\u5b9a\u5217\u8868\u7684\u4e00\u4e2a\u5927\u5b50\u5217\u8868\u8fdb\u884c\u6392\u5e8f\uff0c\u7136\u540e\u7ee7\u7eed\u51cf\u5c0f\u5217\u8868\u7684\u5927\u5c0f\uff0c\u76f4\u5230\u5bf9\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u4e0b\u9762\u7684\u7a0b\u5e8f\u901a\u8fc7\u5c06\u5176\u7b49\u4e8e\u5217\u8868\u5927\u5c0f\u957f\u5ea6\u7684\u4e00\u534a\u6765\u627e\u5230\u95f4\u9699\uff0c\u7136\u540e\u5f00\u59cb\u5bf9\u5176\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\u7136\u540e\u6211\u4eec\u7ee7\u7eed\u91cd\u7f6e\u95f4\u9699\uff0c\u76f4\u5230\u5bf9\u6574\u4e2a\u5217\u8868\u8fdb\u884c\u6392\u5e8f\u3002\n <\/div>\n
\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
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 [2, 11, 19, 27, 30, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

\u9009\u62e9\u6392\u5e8f<\/h2>\n
\n \u5728\u9009\u62e9\u6392\u5e8f\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5728\u7ed9\u5b9a\u5217\u8868\u4e2d\u627e\u5230\u6700\u5c0f\u503c\uff0c\u7136\u540e\u5c06\u5176\u79fb\u5230\u6392\u5e8f\u5217\u8868\u4e2d\u3002\u7136\u540e\uff0c\u6211\u4eec\u5bf9\u672a\u6392\u5e8f\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5176\u4f59\u5143\u7d20\u91cd\u590d\u8be5\u8fc7\u7a0b\u3002\u5c06\u8fdb\u5165\u6392\u5e8f\u5217\u8868\u7684\u4e0b\u4e00\u4e2a\u5143\u7d20\u4e0e\u73b0\u6709\u5143\u7d20\u8fdb\u884c\u6bd4\u8f83\uff0c\u5e76\u653e\u7f6e\u5728\u5176\u6b63\u786e\u4f4d\u7f6e\u3002\u56e0\u6b64\uff0c\u6700\u540e\u5bf9\u672a\u6392\u5e8f\u5217\u8868\u4e2d\u7684\u6240\u6709\u5143\u7d20\u8fdb\u884c\u6392\u5e8f\u3002\n <\/div>\n
\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
\n \u8fd0\u884c\u7ed3\u679c\u5982\u4e0b\uff1a\n <\/div>\n
\n
 [2, 11, 19, 27, 30, 31, 45, 121]
<\/span><\/code><\/pre>\n<\/p><\/div>\n

<\/body>
\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}]}}