首页 > 代码库 > 冒泡排序java算法

冒泡排序java算法

public class maopao {    public static void main(String[] args) {        int[] array = {332,94,65,4,15,64,16,5,4};        int temp = 0 ;        /**         * i:0-8 9 个         * 第一次: i=0 the inner for should loop 8 times so the number is          * */        for(int i =0 ;i<array.length;i++){            for (int j = 0; j < array.length-i-1; j++) {                                if(array[j]>array[j+1]){                    temp = array[j];                    array[j] = array[j+1];                    array[j+1] = temp ;                }                            }        }        for(int k :array){            System.out.println(k);        }    }}

I directly use the  algorithm in C ,but the jvm throws exception .

yes,when it go last j wil be the last num of array ,and it should compare with array[j+1] so it happend exception .

冒泡排序java算法