Java基础知识强化53:经典排序之选择排序(SelectionSort)

1.选择排序的原理图:

 

2. 选择排序代码实现:

  1 package cn.itcast_02;
  2 
  3 /*
  4  * 数组排序之选择排序:
  5  *         从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
  6  */
  7 public class ArrayDemo {
  8     public static void main(String[] args) {
  9         // 定义一个数组
 10         int[] arr = { 24, 69, 80, 57, 13 };
 11         System.out.println("排序前:");
 12         printArray(arr);
 13 
 14         /*
 15         // 第一次
 16         int x = 0;
 17         for (int y = x + 1; y < arr.length; y++) {
 18             if (arr[y] < arr[x]) {
 19                 int temp = arr[x];
 20                 arr[x] = arr[y];
 21                 arr[y] = temp;
 22             }
 23         }
 24         System.out.println("第一次比较后:");
 25         printArray(arr);
 26 
 27         // 第二次
 28         x = 1;
 29         for (int y = x + 1; y < arr.length; y++) {
 30             if (arr[y] < arr[x]) {
 31                 int temp = arr[x];
 32                 arr[x] = arr[y];
 33                 arr[y] = temp;
 34             }
 35         }
 36         System.out.println("第二次比较后:");
 37         printArray(arr);
 38 
 39         // 第三次
 40         x = 2;
 41         for (int y = x + 1; y < arr.length; y++) {
 42             if (arr[y] < arr[x]) {
 43                 int temp = arr[x];
 44                 arr[x] = arr[y];
 45                 arr[y] = temp;
 46             }
 47         }
 48         System.out.println("第三次比较后:");
 49         printArray(arr);
 50 
 51         // 第四次
 52         x = 3;
 53         for (int y = x + 1; y < arr.length; y++) {
 54             if (arr[y] < arr[x]) {
 55                 int temp = arr[x];
 56                 arr[x] = arr[y];
 57                 arr[y] = temp;
 58             }
 59         }
 60         System.out.println("第四次比较后:");
 61         printArray(arr);
 62         */
 63         
 64         /*
 65         //通过观察发现代码的重复度太高,所以用循环改进
 66         for(int x=0; x<arr.length-1; x++){
 67             for(int y=x+1; y<arr.length; y++){
 68                 if(arr[y] <arr[x]){
 69                     int temp = arr[x];
 70                     arr[x] = arr[y];
 71                      arr[y] = temp;
 72                 }
 73             }
 74         }
 75         System.out.println("排序后:");
 76         printArray(arr);
 77         */
 78         
 79         //用方法改进
 80         selectSort(arr);
 81         System.out.println("排序后:");
 82         printArray(arr);
 83 
 84     }
 85     
 86     public static void selectSort(int[] arr){
 87         for(int x=0; x<arr.length-1; x++){
 88             for(int y=x+1; y<arr.length; y++){
 89                 if(arr[y] <arr[x]){
 90                     int temp = arr[x];
 91                     arr[x] = arr[y];
 92                      arr[y] = temp;
 93                 }
 94             }
 95         }
 96     }
 97 
 98     // 遍历功能
 99     public static void printArray(int[] arr) {
100         System.out.print("[");
101         for (int x = 0; x < arr.length; x++) {
102             if (x == arr.length - 1) {
103                 System.out.print(arr[x]);
104             } else {
105                 System.out.print(arr[x] + ", ");
106             }
107         }
108         System.out.println("]");
109     }
110 }

 

posted on 2015-09-24 09:34  鸿钧老祖  阅读(256)  评论(0编辑  收藏  举报

导航