#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/8/3 11:52 PM # @Author : Blake import random def selection_sorting(n): number_list = list(range(1, n+1)) random.shuffle(number_list) print(selectionSort(number_list)[1]) def findSmalllest(arr): smallest = arr[0] smallest_index = 0 count = 0 for i in range(1, len(arr)): if arr[i] < smallest: smallest = arr[i] smallest_index = i count += 1 return smallest_index, count def selectionSort(arr): newArr = [] count = 0 for i in range(len(arr)): smallest, n = findSmalllest(arr) newArr.append(arr.pop(smallest)) count += n return newArr, count selection_sorting(10)
通过Python实现选择算法
最新推荐文章于 2024-07-03 12:54:30 发布