package com.itjob.algorithm;

public class SelectionSort {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		int[] array = {8, 7, 6, 5, 2, 1, 4, 3, 10, 9};
		selectionSort(array);
		for (int i = 0; i < array.length; i++)
		{
			System.out.println(array[i]);
		}
	}

	/**
	 * 选择排序
	 * @param array
	 */
	static void selectionSort(int[] array) {
		for (int i = 0; i < array.length - 1; i++) {
			int min_idx = i;
			for (int j = i + 1; j < array.length; j++) {
				if (array[j] < array[min_idx]) {
					min_idx = j;
				}
			}
			
			if (min_idx != i) {
				// 交换数据
				int temp = array[min_idx];
				array[min_idx] = array[i];
				array[i] = temp;
			}
		}
	}
}
