码迷,mamicode.com
首页 > 编程语言 > 详细

python---选择排序

时间:2019-04-10 23:18:49      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:+=   location   cat   select   count   选择   char   pytho   排序   

  感觉没有简单多少。

# 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:\Users\Sahara\.virtualenvs\test\Scripts\python.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---选择排序

标签:+=   location   cat   select   count   选择   char   pytho   排序   

原文地址:https://www.cnblogs.com/aguncn/p/10686757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!