数据结构-选择排序

 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,5,3,2,3,1,5,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 }

猜你喜欢

转载自www.cnblogs.com/linux777/p/11566043.html