码迷,mamicode.com
首页 > 编程语言 > 详细

java算法----排序----(2)选择排序

时间:2018-04-09 21:20:26      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:src   内容   void   pos   ring   AC   for   position   []   

 1 package log;
 2 
 3 public class Test4 {
 4 
 5     /**
 6      * java算法---选择排序
 7      * 
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         // 需要排序的数组
12         int arr[] = { 49, 20, 36, 51, 18, 94, 61, 31, 50 };
13         // 循环输出该数组内容
14         System.out.println("排序之前:");
15         for (int a : arr) {
16             System.out.print(a + "\t");
17         }
18         System.out.println();
19 
20         int position = 0;
21         for (int i = 0; i < arr.length; i++) {
22             int j = i + 1;
23             position = i;
24             int temp = arr[i];
25             for (; j < arr.length; j++) {
26                 if (arr[j] < temp) {
27                     temp = arr[j];
28                     position = j;
29                 }
30             }
31             arr[position] = arr[i];
32             arr[i] = temp;
33         }
34         // 循环输出该数组内容
35         System.out.println("排序之后:");
36         for (int a : arr) {
37             System.out.print(a + "\t");
38         }
39         System.out.println();
40 
41     }
42 
43 }

下面这个是控制台输出

技术分享图片

java算法----排序----(2)选择排序

标签:src   内容   void   pos   ring   AC   for   position   []   

原文地址:https://www.cnblogs.com/javallh/p/8762047.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!