2_3选择排序

2017-09-06  本文已影响6人  X_Y

c++实现

class SelectionSort {
public:
    int* selectionSort(int* A, int n) {
        // write code here
        for(int i=0; i<n; i++){
            int min_val = A[i];
            for(int j=i+1; j<n; j++){
                if(min_val>A[j]){
                    int tmp = A[j];
                    A[j] = min_val;
                    min_val = tmp;
                }
            }
            A[i] = min_val;
        }
        return A;
    }
};

python 实现

# -*- coding:utf-8 -*-

class SelectionSort:
    def selectionSort(self, A, n):
        # write code here
        for i in xrange(n):
            min_val = A[i]
            for j in xrange(i+1, n):
                if A[j] < min_val:
                    tmp = min_val
                    min_val = A[j]
                    A[j] = tmp
            A[i] = min_val
        return A
上一篇下一篇

猜你喜欢

热点阅读