首页 > 其他 > 详细

冒泡排序

时间:2014-08-23 17:40:31      阅读:287      评论:0      收藏:0      [点我收藏+]

 

package com.learning.algorithm;

public class BubbleSort {
    
    public int[] bubbleSort1(int[] arrValue){
        int temp =0;
        int length = arrValue.length;
        for(int i=length-1;i>1;i--){
            for(int j=0;j<i;j++){
                if(arrValue[j]>arrValue[j+1]){
                    temp = arrValue[j];
                    arrValue[j] = arrValue[j+1];
                    arrValue[j+1] = temp;
                }
            }
        }
        return arrValue;
    }
    
    public int[] bubbleSort2(int[] arrValue){
        int temp = 0;
        int length = arrValue.length;
        for(int i=0;i<length-1;i++){
            for(int j=0;j<length-1-i;j++){
                if(arrValue[j]>arrValue[j+1]){
                    temp = arrValue[j];
                    arrValue[j] = arrValue[j+1];
                    arrValue[j+1] = temp;
                }
            }
        }
        return arrValue;
    }
    
    public static void main(String[] args) {
        int[] arrValue = {89,39,56,93,2,58,43,51,33,67};
        BubbleSort bs = new BubbleSort();
        int[] arrResult = bs.bubbleSort1(arrValue);
                
        for(int value:arrResult){
            System.out.print(value);
            System.out.print(",");
        }
        System.out.println();
        System.out.println("--------------------------------");
        
        int[] arrResult1 = bs.bubbleSort1(arrValue);
        for(int value:arrResult1){
            System.out.print(value);
            System.out.print(",");
        }
    }
}

 

冒泡排序

原文:http://www.cnblogs.com/ryanwangblog/p/3931521.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!