package question; public class Test04 { /***对给定数组进行排序//冒泡排序 * 原理是依次比较相邻的两个数,将小数放在前面,大数放在后面 * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub int arr[]={2,6,1,9,7,45,13}; print(arr); arraysort(arr); print(arr); } private static void arraysort(int[] arr) { // TODO Auto-generated method stub for (int x=0;x<arr.length-1;x++){ for (int y = 0; y < arr.length-x-1; y++) { if(arr[y]>arr[y+1]) swap(arr,y,y+1); } } } private static void swap(int[] arr, int x, int y) { // TODO Auto-generated method stub int temp; temp=arr[x]; arr[x]=arr[y]; arr[y]=temp; } private static void print(int[] arr) { // TODO Auto-generated method stub System.out.print("["); for (int i = 0; i < arr.length; i++) { if (i!=arr.length-1) System.out.print(arr[i]+","); else System.out.println(arr[i]+"]"); } } }