两两比较大小,如果比较结果为true就进行交换,请对比sort()和sort1()有何不同
package javaBasic;
import java.util.Arrays;
public class BubbleSortTest{
public static void main(String[] args){
int[] array = {49,38,65,97,76,13,27};
int[] array1 = {49,38,65,97,76,13,27};
BubbleSortTest bs= new BubbleSortTest(array);
bs.sort();
BubbleSortTest bs1= new BubbleSortTest(array1);
bs1.sort1();
}
public BubbleSortTest(){
}
int arr[] = null;
public BubbleSortTest(int a[]){
this.arr = a;
System.out.println("BubbleSortInit:" + Arrays.toString(arr));
}
public void sort(){
int temp = 0;
for(int i = 0; i < arr.length;i++){
for (int j = 0; j < arr.length-i -1; j++){
if (j>=0 && arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
System.out.println(Arrays.toString(arr));
}
System.out.println("finally sort result:"+Arrays.toString(arr));
}
public void sort1(){
int temp = 0;
for(int i = 0; i < arr.length;i++){
for (int j = 0; j < arr.length-1; j++){
if (j>=0 && arr[j] > arr[j+1]){
temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
System.out.println(Arrays.toString(arr));
}
System.out.println("finally sort result:"+Arrays.toString(arr));
}
}