`
tw5566
  • 浏览: 448558 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

算法学习(二)---选择排序

阅读更多
package com.tw.ds.sort;


/**
 * <p>选择排序法
 * 选择排序算法的一般策略:搜索整个值列,以找到最小值。
 *  将该值与值列中第一个位置上的值进行交换。搜索剩下的值列(第一个除外),以找到其中的最小值,
 *  然后将其与值列中第二个位置上的值进行交换。对值列中的每个位置重复该过程。
 *  在算法结束时,就完成了对值列的排序。
 * </p>
 * @author tangw 2010-11-22
 *
 */
public class SelectSortMain {
	
	/**
	 * 主方法
	 * @param args
	 */
	public static void main(String[] args) {
		int ar[] ={12,88,10,2,3,5,6,11,3};
		selectSort(ar);
		for( int i=0;i<ar.length;i++){
			System.out.println("---i="+i+"  values:"+ar[i]);
		}

	}//end method main
	
	/**
	 * <p>选择排序方法</p>
	 * @param arData
	 */
	public static void selectSort(int[] arData){
		int len = arData.length;
		for(int i=0;i<len;i++){//外循环
			//最小值索引
			int minIndex = i;
			for(int j=i+1;j<len;j++){//内循环 arData[i]后的值
				if( arData[j]<arData[minIndex] ){
					minIndex = j;
				}
			}
			//替换 将最小的值与当前arData[i]的值替换
			if( i!= minIndex){
				int temp = arData[i];
				arData[i] = arData[minIndex];
				arData[minIndex]=temp;
			}
		}
	}//ebd method selectSort

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics