python---选择排序
Posted aguncn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python---选择排序相关的知识,希望对你有一定的参考价值。
感觉没有简单多少。
# coding = utf-8 # 选择排序 def selection_sort(a_list): loop_count = 0 swap_count = 0 for fill_slot in range(0, len(a_list)): loop_count += 1 pos_of_min = fill_slot for location in range(fill_slot+1, len(a_list)): loop_count += 1 if a_list[pos_of_min] > a_list[location]: pos_of_min = location swap_count += 1 a_list[fill_slot], a_list[pos_of_min] = a_list[pos_of_min], a_list[fill_slot] print(a_list) print(‘======== selection_sort loop_count========‘, loop_count) print(‘========selection_sort swap_count=========‘, swap_count) my_list = [4, 5, 7, 2, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] selection_sort(my_list) print(my_list)
C:UsersSahara.virtualenvs estScriptspython.exe C:/Users/Sahara/PycharmProjects/test/python_search.py [2, 5, 7, 4, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 7, 5, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 9, 7, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 54, 765, 45, 9876, 34, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 34, 765, 45, 9876, 54, 12343, 36] [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 9876, 54, 12343, 765] [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 9876, 54, 12343, 765] [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 54, 9876, 12343, 765] [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 54, 765, 12343, 9876] [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 54, 765, 9876, 12343] [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 54, 765, 9876, 12343] ======== selection_sort loop_count======== 105 ========selection_sort swap_count========= 14 [2, 4, 5, 7, 7, 9, 9, 34, 36, 45, 54, 765, 9876, 12343] Process finished with exit code 0
以上是关于python---选择排序的主要内容,如果未能解决你的问题,请参考以下文章