自己整理的排序算法(2)用遞歸實現選擇排序


//用遞歸的方法實現選擇排序
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]+" ");
		}
	}
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM