选择排序-算法及优化

选择排序-算法及优化

前言

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:54fda24c-780c-4aa6-b91a-3a5bae585c1c

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:e223f14a-682d-4090-924f-27dbba96fe0f

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:564d9b90-9c01-4f57-ab5d-1ba619bbb448

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:5bbe5947-9734-417d-b9d0-1b60d277769d

代码

public static void main(String[] args) {
        int [] arry = {3,7,2,9,5,6,4,2,1,5};
        for (int i = 0; i < arry.length-1; i++) {
        //i控制基准位置,i = 0即从数组首位开始依次确定元素
             for(int j = i+1 ;j < arry.length ;j++){
                 if(arry[j]>arry[i]){                //小于基准位置的值则交换
                     int temp = arry[i];
                     arry[i] = arry[j];
                     arry[j] = temp ;
                 }
             }
        }
}

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:7a63fe26-9a30-4305-ac1b-cb625b575e10

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:78c63dfe-a20c-4d01-a2eb-715bea03b724

public static void main(String[] args) {
        int [] arry1 = {5,8,4,5,7,7,5,6,2,9};
        for (int i = arry1.length-1; i > 0; i--) {
        //i控制基准位置,i = 数组长度-1即从数组末尾开始依次确实元素
            for(int j = i-1;j>=0;j--){
                if(arry1[j]>arry1[i]){            //大于基准位置的值则交换
                    int temp = arry1[i];
                    arry1[i] = arry1[j];
                    arry1[j] = temp;
                }
            }
       }
}

优化

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:2a5a3088-b999-48f2-90af-7f9e361685ea

[En]

[TencentCloudSDKException] code:FailedOperation.ServiceIsolate message:service is stopped due to arrears, please recharge your account in Tencent Cloud requestId:bebb3b23-b188-408e-acca-4e88b9c3662d

优化后的代码如下:

public static void main(String[] args) {
int [] arry2 ={5,7,8,6,8,9,1,5,7,6,3,4} ;
        for (int i = 0; i < arry2.length-1; i++) {
            int minIndex = i;                   //假设基准位置是最小值
            for (int j = i+1;j < arry2.length;j++){
                if(arry2[j]

Original: https://www.cnblogs.com/ji-lei/p/14716023.html
Author: Ji_Lei
Title: 选择排序-算法及优化

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/562849/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球