Python与Go选择排序

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# 选择排序
# 时间复杂度O(n^2)


def selection_sort(array):
    length = len(array)
    for m in range(length):
        k = 0
        for i in range(1, length - m):
            if array[i] > array[k]:
                k = i

        array[length - 1 - m], array[k] = array[k], array[length - 1 - m]


if __name__ == '__main__':
    aa = [1, 4, 3, 5, 6, 2, 6, 0, 0]
    selection_sort(aa)
    print(aa)
package main

// 选择排序
// 时间复杂度O(n^2)
import "fmt"

func selectionSort(array []int)  {
    length := len(array)
    for i := 0; i < length; i ++ {
        k := 0
        for m := 1; m < length - i; m ++ {
            if array[m] < array[k] {
                k = m
            }
        }
        array[length - 1 - i], array[k] = array[k], array[length - 1 - i]
    }
}

func main()  {
    aa := []int{1, 4, 3, 5, 6, 2, 6, 0, 0}
    selectionSort(aa)
    fmt.Println(aa)
}

 

posted @ 2018-10-23 11:00  失落的黎明  阅读(320)  评论(0编辑  收藏  举报