lintcode-463-整数排序

463-整数排序

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

样例

对于数组 [3, 2, 1, 4, 5], 排序后为:[1, 2, 3, 4, 5]。

标签

排序

思路

使用插入排序,不过这题的初始代码的形参没使用引用方式,要主动修改

code

class Solution {
public:
    /*
     * @param A: an integer array
     * @return: 
     */
    void sortIntegers(vector<int> &A) {
        // write your code here
        int size = A.size();
        if (size <= 1) {
            return;
        }
        for (int i = 0; i < size - 1; i++) {
            for (int j = i + 1; j > 0; j--) {
                if (A[j] < A[j - 1]) {
                    swap(A[j], A[j - 1]);
                }
                else {
                    break;
                }
            }
        }
    }
};
posted @ 2017-08-20 19:39  LiBaoquan  阅读(218)  评论(0编辑  收藏  举报