- class Demo
- {//选择排序 从小到大
- public static void main (String [] args)
- {int temp[]={23,345,65,76,87,29};
- System.out.println("之前为....");
- printArr(temp);
- System.out.println("之后为....");
- getMax(temp);
- printArr(temp);
- }
- public static void getMax(int arr[])
- {
- //n个数排列,就会比较n-1论,外层循环控制轮数
- for (int i=1;i<=arr.length-1 ;i++ )
- {//内层循环六个数,一个数要跟其他的数循环比五次,才能比完
- for (int j=0;j<arr.length-1 ;j++ )
- {
- if (arr[j]>arr[j+1])
- {
- int temp=arr[j+1];
- arr[j+1]=arr[j];
- arr[j]=temp;
- }
- }
- }
- }
- public static void printArr(int arr[])
- {
- for (int i=0;i<=arr.length-1 ;i++ )
-
- {
- System.out.print(arr[i]+"__");
- }
- }
- }
复制代码 |