package com.cc.dataconsttuct.com;
/***
 * 简单选择排序:令A[0,n-1]有n个数据元素的数组,将数组A排列为一个非降序的有序数组;
 * 算法:首先在n个元素中找到最小元素,将其放在A[0]中,然后将剩下的n-1个元素中找到最小的放在A[1]中,这个过程不断进行下去。。。。
 * @author caocx
 *
 */
public class SelectSort {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] a = {12,23,9,24,15,3,18};
		//排序前输出
		for (int i = 0; i < a.length; i++) {
			if(i == a.length-1){
				System.out.println(a[i]);
			}
			else{
				System.out.print(a[i]+" ");
			}
			
		}
		//调用方法
		SelectSortMain(a);
		//排序后输出
		System.out.println("选择排序后的数据");
		for (int i = 0; i < a.length; i++) {
			if(i == a.length-1){
				System.out.println(a[i]);
			}
			else{
				System.out.print(a[i]+" ");
			}
		}
	}
	
	//函数方法
	private static void SelectSortMain(int[] arr) {
		int temp;
		//eg:就比如n个数,前n-1个数都可以当做min 依次与后面的数进行比较
		for (int j = 0; j < arr.length-1; j++) {
			int min = j;
			//代表第j+1个数的各个数,拿第j个数与j+1 到arr的最后一个数进行对比
			//j是最小的数,依次与后面的进行比较,如果后面的数比最小数还要小 就对换位置
			for (int k = j+1; k < arr.length; k++)
				if(arr[k]<arr[min])
					min = k;
			//j不是最小的数,把最小的数值对换到j的位置
				if(j!=min){
					temp = arr[j];
					arr[j] = arr[min];
					arr[min] = temp;
				}
		}
	}
}
原文:http://www.cnblogs.com/caocx/p/7609783.html