2_2冒泡排序

cpp实现

class BubbleSort {
public:
    int* bubbleSort(int* A, int n) {
        // write code here
        // int result[n] = {0};
        for(int i=0; i A[j+1]){
                    int tmp = *(A+j); 
                    *(A+j) = *(A+j+1);
                    *(A+j+1) = tmp;
                }
            }
        }
        return A;
    }
};

Python 实现

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

class BubbleSort:
    def bubbleSort(self, A, n):
        # write code here
        for i in xrange(n):
            for j in xrange(n-i-1):
                if A[j] > A[j+1]:
                    tmp = A[j]
                    A[j] = A[j+1]
                    A[j+1] = tmp
        return A

你可能感兴趣的:(2_2冒泡排序)