您的位置:首页 > 其它

冒泡排序

2014-08-23 17:27 281 查看
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(",");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: