## 二、代码实现

```package com.example.algorithmdemo.sortingAlgorithm;

/**
* 选择排序
*/
public class SelectionSort {

/**
* 数组排序
* @param a
*/
public static void sort(Comparable[] a){
for(int i = 0;i<a.length-1;i++){
//假设本次遍历，最小值的索引为i
int minIndex = i;
for(int j = i + 1;j < a.length;j++){
if(greater(a[minIndex],a[j])){
//更换最小值索引
minIndex = j;
}
}
//交换i索引和minIndex索引的值
exchange(a,i,minIndex);
}
}

/**
* 判断参数a是否比参数b大
* 返回true/false
* @param a
* @param b
* @return
*/
private static boolean greater(Comparable a,Comparable b){
return a.compareTo(b)>0;
}

/**
* 数组元素i和j交换位置
* @param a
* @param i
* @param j
*/
private static void exchange(Comparable[] a,int i,int j){
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
```

## 三、测试

```package com.example.algorithmdemo.test;

import com.example.algorithmdemo.sortingAlgorithm.SelectionSort;

import java.util.Arrays;

public class SelectionTest {
public static void main(String[] args) {
Integer[] a = {3,2,6,8,1,4,5,7};
SelectionSort.sort(a);
System.out.println(Arrays.toString(a));
}
}
```