Java_数组的选择排序

 1 class ArrayOperateDemo1
 2 {
 3     /*
 4     定义一个功能,为了遍历数组中的元素
 5     */
 6     public static void printArray(int[] arr)
 7     {
 8         for (int x=0;x<arr.length ;x++ )
 9         {
10             //去除行尾,
11             if (x!=arr.length-1)
12             {
13             System.out.println(arr[x]+",");
14             }
15             else
16                 System.out.println(arr[x]);
17         }
18     }
19 
20     /*
21     定义一个输出功能
22     */
23 
24     public static void sop(String str)
25     {
26         System.out.println(str);
27     }
28 
29     public static void main(String[] args) 
30     {
31         int[] arr={57,23,19,89,66,74};
32         sop("排序前:");
33         printArray(arr);
34 
35         selectSort(arr);
36 
37         sop("排序后:");
38         printArray(arr);
39 
40     }
41     /*
42     选择排序
43 
44     */
45     public static void selectSort(int[] arr)
46     {
47     for (int x=0;x<arr.length-1 ;x++ )
48     {
49         for (int y=x+1;y<arr.length ;y++ )
50         {
51             int temp=arr[x];
52             arr[x]=arr[y];
53             arr[y]=temp;
54         }
55     }
56     }
57 }
58 
59  

posted @ 2017-10-24 16:41  BirdieForLove  阅读(174)  评论(0编辑  收藏  举报