美文网首页
2_3选择排序

2_3选择排序

作者: X_Y | 来源:发表于2017-09-06 16:42 被阅读6次

    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
    

    相关文章

      网友评论

          本文标题:2_3选择排序

          本文链接:https://www.haomeiwen.com/subject/vddvjxtx.html