首页 > 代码库 > java 语言实现冒泡排序

java 语言实现冒泡排序

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class BubbleSort{
    public static void main(String[] args){
        int[] sortArray = new int[]{5,7,4,2,9,8,3,6};
        System.out.println("before sorting ,the numbers are:");
        show(sortArray);
        bubbleSort(sortArray);
        System.out.println("after sorting,the numbers are:");
        show(sortArray);
    }
    public static void bubbleSort(int[] intArray){
        for(int i =0;i<intArray.length-1;i++){
            for(int j=0;j<intArray.length -1-i;j++){
                if(intArray[j]> intArray[j+1]){
                int temp =intArray[j];
                intArray[j] =intArray[j+1];
                intArray[j+1] =temp;
                }
            }
        }
    }
    public static void show(int[] intArray){
        for(int i=0;i<intArray.length;i++){
            System.out.print(intArray[i]+"\t");
        }
        System.out.println();
    }
}