Fork me on GitHub

java选择排序

Posted on 2019-07-24 18:56  攀一座山  阅读(123)  评论(0编辑  收藏  举报
 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, 4, 8, 7, 3, 9, 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 }

 

Copyright © 2024 攀一座山
Powered by .NET 8.0 on Kubernetes