经典排序算法——冒泡排序

对于一个int数组,请编写一个冒泡排序算法,对数组元素排序。

给定一个int数组A及数组的大小n,请返回排序后的数组。 

测试样例:
输入数组: [1,2,3,5,2,3],6
输出数组: [1,2,2,3,3,5]

class BubbleSort {
public:
    int* bubbleSort(int* A, int n) {
        // write code here
        bool flag=true;
        for(int i=0;i<n && flag;++i)
        {
            for(int j=n-1;j>i;--j)
            {
                if(A[j]<A[j-1])
                {
                    int temp=A[j];
                    A[j]=A[j-1];
                    A[j-1]=temp;
                    flag=true;
                }
            }
        }
        return A;
    }
};

经典排序算法——冒泡排序_第1张图片

你可能感兴趣的:(经典排序算法——冒泡排序)