/** * @CLassName BubbleSort * @Description 冒泡排序 * @Author wangpengfei * @Email wangpengfei4113@163.com * @Date 2018/12/10 19:00 * @Version 1.0 **/ public class BubbleSort { public static void main(String args[]) { int[] arr = {1,3,2,9,5,7,6,8,4}; System.out.println("没有排序之前的数组为:"); for (int num : arr) { System.out.print(num+" "); } for (int i = 0; i <arr.length ; i++) { for (int j = 0; j <arr.length-1-i ; j++) { if(arr[j]>arr[j+1]){ int temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } System.out.println(); System.out.println("排序后的数组为:"); for (int num : arr) { System.out.print(num+" "); } } }