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:\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---选择排序

评论关闭