- package com.itheima;
- class ArrSortTest
- {
- public static void main(String[] args)
- {
- int[] arr1={0,1,2,3,5,4,8,7,9,10};
- int[] arr2={1,2,3,4,5,9,9,8};
- int[] arr3={1,2,3,5,77,444,55,66,88,77,99};
- int[] arr=arr1;
- xuanArr(arr);
- arr=arr3;
- xuanArr(arr);
- arr=arr2;
- xuanArr(arr);
-
- }
- public static void xuanArr(int[] arr)
- {
- System.out.println("下边是数组"+arr+"的"+"遍历情况");
- readArr(arr);
- bubbleSort(arr);
- readArr(arr);
- selectSort(arr);
- readArr(arr);
- System.out.println("-------------华丽的分割线------------");
- }
- //遍历数组
- public static void readArr(int[] arr)
- {
- for (int x=0;x<arr.length ;x++ )
- {
- if(x!=arr.length-1)
- System.out.print(arr[x]+"; ");
- else
- System.out.println(arr[x]);
- }
- }
- //冒泡排序,小到大
- public static void bubbleSort(int[] arr)
- {
- for (int x=0; x<arr.length;x++ )
- {
- for (int y=0;y<arr.length-x-1 ;y++ )
- {
- if(arr[y]>arr[y+1])
- {
- diaohuan(arr,y,y+1);
- /*
- int temp=arr[y];
- arr[y]=arr[y+1];
- arr[y+1]=temp;
- */
- }
- }
- }
- System.out.println("-------下边是冒泡排序,小到大-------");
- }
- //选择排序,大到小
- public static void selectSort(int[] arr)
- {
- for (int x=0;x<arr.length-1 ;x++ )
- {
- for (int y=x+1;y<arr.length ;y++ )
- {
- if(arr[x]<arr[y])
- {
- diaohuan(arr,x,y);
- /*
- int temp=arr[x];
- arr[x]=arr[y];
- arr[y]=temp;
- */
- }
- }
- }
- System.out.println("-------下边是选择排序,大到小-------");
- }
- //将调换的代码封装成函数
- public static void diaohuan(int[] arr,int a,int b)
- {
- {
- int temp=arr[a];
- arr[a]=arr[b];
- arr[b]=temp;
- }
- }
- }
复制代码
我看了,没哟普问题啊,你i昂首神马?搞不懂 |