java数组从小到大排序_JAVA数组从小到大排序代码

class SortArray

{

public static void main(String[] args)

{

int[] arr = new int[]{1,6,3,34,3,54,7,66,19};

System.out.print("sort the array:");

printArr(arr);

System.out.print("sort result is:");

printArr(sortArr(arr));

}//end of method main

//对一个数组按照从小到大的顺序进行排序

public static int[] sortArr(int[] arr)

{

for (int x = 0; x

{

for (int y = x + 1; y

{

if (arr[x]>arr[y])

{

int temp = arr[x];

arr[x] = arr[y];

arr[y] = temp;

}

}

}

return arr;

}//end of method sortArr

//打印一个数组

public static void printArr(int[] arr)

{

for (int x = 0; x

{

if (x == arr.length - 1)

{

System.out.println(arr[x]);

break;

}

System.out.print(arr[x] + ",");

}

}//end of method printArr

}//end of class SortArray

class SortArray { public static void main(String[] args) { int[] arr = new int[]{1,6,3,34,3,54,7,66,19}; System.out.print("sort the array:"); printArr(arr); System.out.print("sort result is:"); printArr(sortArr(arr)); }//end of method main //对一个数组按照从小到大的顺序进行排序 public static int[] sortArr(int[] arr) { for (int x = 0; x { for (int y = x + 1; y { if (arr[x]>arr[y]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } return arr; }//end of method sortArr //打印一个数组 public static void printArr(int[] arr) { for (int x = 0; x { if (x == arr.length - 1) { System.out.println(arr[x]); break; } System.out.print(arr[x] + ","); } }//end of method printArr }//end of class SortArray
经验分享 程序员 微信小程序 职场和发展