首页 > 技术文章 > 自己整理的排序算法(2)用递归实现选择排序

kisty 2016-09-07 21:55 原文

//用递归的方法实现选择排序
package sort;

public class RecursiveSelectionSort {
	public static void sort(double[] list){
		sort(list,0,list.length-1);
	}
	
	public static void sort(double[] list,int low,int high){
		if(low<high){
		 double	currentMin = list[low];
		 int currentMinIndex = low;
		 
		 for(int i = low+1;i<=high;i++){
			 if(currentMin>list[i]){
				 currentMin = list[i];
				 currentMinIndex = i;
			 }
		 }
		 
			 list[currentMinIndex ] = list[low];
			 list[low] = currentMin;
			 
		 sort(list,low+1,high);
		}
	}
	
	public static void main(String[] args){
		double[] list ={5.2 , 1.4 , 6.3,  2.3  ,4.6};
		sort(list);
		for(int i =0;i<list.length;i++){
			System.out.print(list[i]+" ");
		}
	}
}

  

推荐阅读