# 2021-10-25

1 篇文章 0 订阅

## 优化选择排序

public class Selection {

//优化选择排序,一次i循环找出最小值和最大值。
public static void main(String[] args) {
int arr[] = {5,3,2,1,4,7,9,8,6};
int al = arr.length-1;
for (int i = 0;i<al;i++){
for(int e = i+1;e<al-i;e++){
int temp;
if(arr[i]>arr[e]){
temp = arr[i];
arr[i] = arr[e];
arr[e] = temp;
}
if(arr[al-i]<arr[al-e]){
temp = arr[al-i];
arr[al-i] = arr[al-e];
arr[al-e] = temp;
}
}

}
//打印数组
System.out.print("[");
for(int j = 0;j<arr.length;j++){
if(j!=arr.length-1)
System.out.print(arr[j]+",");
else
System.out.print(arr[j]);
}
System.out.print("]");
}
}


 public static void main(String[] args) {
int arr[] = {5,3,2,1,4,7,9,8,6};
int al = arr.length-1;
for (int i = 0;i<al;i++){
for(int e = i+1;e<al-1;e++){
int temp;
if(arr[i]>arr[e]){
temp = arr[i];
arr[i] = arr[e];
arr[e] = temp;
}
}
}
//打印数组
System.out.print("[");
for(int j = 0;j<arr.length;j++){
if(j!=arr.length-1)
System.out.print(arr[j]+",");
else
System.out.print(arr[j]);
}
System.out.print("]");
}
}


public class Selection {

//优化选择排序,一次i循环找出最小值和最大值。
public static void main(String[] args) {

});
});
}

//创建无序数组
public static int[] getArray(){
int[] arr = new int[10000];
Random random = new Random(500000);
for (int i = 0;i<10000;i++){
arr[i] = random.nextInt();
}
return arr;
}

public static Long 原始(){
int arr[] = getArray();

int al = arr.length-1;
Long time = System.currentTimeMillis();
for (int i = 0;i<al;i++){

for(int e = i+1;e<al-1;e++){

int temp;
if(arr[i]>arr[e]){
temp = arr[i];
arr[i] = arr[e];
arr[e] = temp;
}
}
}
return System.currentTimeMillis()-time;
}

public static Long 优化(){
int arr[] = getArray();

int al = arr.length-1;
Long time = System.currentTimeMillis();
for (int i = 0;i<al;i++){

for(int e = i+1;e<al-i;e++){

int temp;
if(arr[i]>arr[e]){
temp = arr[i];
arr[i] = arr[e];
arr[e] = temp;
}
if(arr[al-i]<arr[al-e]){
temp = arr[al-i];
arr[al-i] = arr[al-e];
arr[al-e] = temp;
}
}
}
return System.currentTimeMillis()-time;
}



• 1
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

03-06 2536
11-10 1507
05-07 752
©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。