`

排序算法

    博客分类:
  • java
 
阅读更多
public class TR {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//冒泡
		int []array = {3,24,51,12,556,112,1};
		int tmp;
		for(int i=0; i <array.length-1; i++){
			for(int j=0; j<array.length-1-i; j++){
				if(array[j]>array[j+1]){
					tmp=array[j];
					array[j]=array[j+1];
					array[j+1]=tmp;
				}
			}
		}
         //快排
		Integer []iarray = {3,24,51,12,556,112,1};
		quick(iarray,0,iarray.length-1);
		
	}
	
	public static void quick(Integer[] array, int low, int high){
		if(low<high){
			int k = parthandle(array, low, high);
			quick(array,low,k-1);
			quick(array,k+1,high);
		}
	}
	
	public static int parthandle(Integer[] n, int low, int high){
		int key = n[low];
		while(low<high){
			while(low<high&&n[high]>=key) high--;
			n[low] = n[high];
			while(low<high&&n[low]<=key) low++;
			n[high] = n[low];
		}
		n[low]= key;
		return low;
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics