加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java选择排序

发布时间:2020-12-15 01:56:43 所属栏目:Java 来源:网络整理
导读:1 public class 选择排序 { 2 public static void main(String[] args) { 3 int []arr= new int []{5,4,8,7,3,9,1 }; 4 for ( int i=0;iarr.length-1;i++ ){ 5 for ( int j=i+1;jarr.length;j++ ){ 6 if (arr[j] arr[i]){ 7 int temp= arr[i]; 8 arr[i]= arr
 1 public class 选择排序 {
 2     public static void main(String[] args) {
 3         int[]arr=new int[]{5,4,8,7,3,9,1};
 4         for (int i=0;i<arr.length-1;i++){
 5             for (int j=i+1;j<arr.length;j++){
 6                 if (arr[j]<arr[i]){
 7                 int temp=arr[i];
 8                 arr[i]=arr[j];
 9                 arr[j]=temp;
10             }
11             }
12         }
13         for (int i=0;i<arr.length;i++) {
14             System.out.print(arr[i]);
15         }

拆分选择排序算法:

 1  public static void main(String[] args) {
 2         int[] arr = new int[]{5,1};
 3         for (int j = 1; j < arr.length; j++) {
 4             if (arr[j] < arr[0]) {
 5                 int temp = arr[0];
 6                 arr[0] = arr[j];
 7                 arr[j] = temp;
 8 
 9             }
10         }
11         for (int j = 2; j < arr.length; j++) {
12             if (arr[j] < arr[1]) {
13                 int temp = arr[1];
14                 arr[1] = arr[j];
15                 arr[j] = temp;
16 
17             }
18         }
19         for (int j = 3; j < arr.length; j++) {
20             if (arr[j] < arr[2]) {
21                 int temp = arr[2];
22                 arr[2] = arr[j];
23                 arr[j] = temp;
24 
25             }
26         }
27         for (int j = 4; j < arr.length; j++) {
28             if (arr[j] < arr[3]) {
29                 int temp = arr[3];
30                 arr[3] = arr[j];
31                 arr[j] = temp;
32 
33             }
34         }
35         for (int j = 5; j < arr.length; j++) {
36             if (arr[j] < arr[4]) {
37                 int temp = arr[4];
38                 arr[4] = arr[j];
39                 arr[j] = temp;
40 
41             }
42         }
43         for (int j = 6; j < arr.length; j++) {
44             if (arr[j] < arr[5]) {
45                 int temp = arr[5];
46                 arr[5] = arr[j];
47                 arr[j] = temp;
48 
49             }
50         }
51         for (int i = 0; i < arr.length; i++) {
52             System.out.print(arr[i]);
53         }
54 
55     }
56 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读