lintcode:整数排序

题目

给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。

解题

冒泡排序

public class Solution {
    /**
     * @param A an integer array
     * @return void
     */
    public void sortIntegers(int[] A) {
        // Write your code here
        if(A==null || A.length<=1)
            return;
        int n = A.length;
        for(int i=n-1;i>=0;i--){
            
            for(int j=0;j<i;j++){
                if(A[j]>A[j+1]){
                    swap(A,j,j+1);
                }
            }
        }
    }
    public void swap(int[] A,int i,int j){
        int tmp = A[i];
        A[i] = A[j];
        A[j] = tmp;
    }
}
原文地址:https://www.cnblogs.com/theskulls/p/5650779.html