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

数据结构-选择排序

发布时间:2020-12-15 07:40:52 所属栏目:Java 来源:网络整理
导读:1 package com.datastack.search; 2 3 import java.util.Arrays; 4 5 // 选择排序 6 public class SelectSort { 7 public static void main(String[] args) { 8 int [] arr = new int [] {5,3,2,54,5,1,23,65 }; 9 selectSort(arr); 10 11 System.out.printl
 1 package com.datastack.search;
 2 
 3 import java.util.Arrays;
 4 
 5 //选择排序
 6 public class SelectSort {
 7     public static void main(String[] args) {
 8         int[] arr = new int[] {5,3,2,54,5,1,23,65};
 9         selectSort(arr);
10         
11         System.out.println(Arrays.toString(arr));
12     }
13     //选择排序
14     public static void selectSort(int[] arr){
15         for(int i=0;i<arr.length;i++){
16             int minIndex=i;
17             for(int j=i+1;j<arr.length;j++){
18                 if(arr[j]<arr[minIndex]){
19                     minIndex=j;
20                 }
21             }
22             if(i!=minIndex){
23                 int temp = arr[i];
24                 arr[i] = arr[minIndex];
25                 arr[minIndex] = temp;
26             }
27         }
28         
29     }
30 }

(编辑:李大同)

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

    推荐文章
      热点阅读