码迷,mamicode.com
首页 > 其他好文 > 详细

冒泡排序

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

标签:style   blog   color   for   ar   div   log   new   sp   

 

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(",");
        }
    }
}

 

冒泡排序

标签:style   blog   color   for   ar   div   log   new   sp   

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

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