码迷,mamicode.com
首页 > 编程语言 > 详细

lintcode:整数排序

时间:2016-07-07 18:59:06      阅读:346      评论:0      收藏:0      [点我收藏+]

标签:

题目

给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 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;
    }
}

 

lintcode:整数排序

标签:

原文地址:http://www.cnblogs.com/theskulls/p/5650779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!