选择排序是一种简单直观的排序算法。
工作原理:
首先在未拍序列中找到最小的元素,存放到排序序列的起始位置,然后在从剩下的元素中继续寻找最小的元素,放到排序序列末尾,以此类推,直到所有的元素均排序完毕
package SortAlgorithm;
/**
* Created by Promacanthus on 2017/6/12.
*/
public class SelectionSort {
private int[] ints;
SelectionSort(int[] ints){
this.ints = ints;
}
public void selectionsort(int[] ints) {
for (int i = 0; i < ints.length; i++) {
for (int j = i + 1; j < ints.length; j++) {
if (ints[i] > ints[j]) {
swap(ints, i, j);
}
}
}
}
private void swap(int[] ints, int i, int j) {
int temp = ints[i];
ints[i] = ints[j];
ints[j] = temp;
}
public static void main(String[] args){
int[] a = {4, 2, 1, 6, 3, 6, 0, -5, 1, 1};
SelectionSort selectionSort = new SelectionSort(a);
selectionSort.selectionsort(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}