当前位置:首页 > 开发 > 编程语言 > 编程 > 正文

Sort

发表于: 2015-06-08   作者:残云cruel   来源:转载   浏览:
摘要: BubbleSort.Java package sort; public class BubbleSort { public static void main(String[] args) { int[] number={6,7,8,5,4,1,2,3}; // bubbleSort(number); int low=0; int high=numb
BubbleSort.Java

package sort;

public class BubbleSort {

    public static void main(String[] args) {
		int[] number={6,7,8,5,4,1,2,3};
//		bubbleSort(number);
		int low=0;
		int high=number.length-1;
	    quickSort(number,low,high);
		//print 
		for(int i=0;i<number.length;i++)
			System.out.print(number[i]+" ");				
	}
    //bubbleSort

	public static  void bubbleSort(int[] number) {	
		int tempt;
		boolean continueSort=true;
		while(continueSort){		
			continueSort=false;
			for(int i=0;i<number.length-1;i++){
				if(number[i]>number[i+1]){
					tempt=number[i+1];
					number[i+1]=number[i];
					number[i]=tempt;
					continueSort=true;				
				}			
			}		
		}
	}
	//recursion quickSort
	private static void quickSort(int[] number,int low,int high) {
	    if(low<high){//to identify whether this part has more than 1 numbers
	    	int pivotloc=Partition(number,low,high);
			quickSort(number,low,pivotloc-1);
			quickSort(number,pivotloc+1,high);	    	
	    }	
	}
	//Partition算法10.6(b)
	static int Partition(int[] number,int low,int high){
		int tempt=number[low];
		int pivotkey=number[low];
		while(low<high){
			while(low<high && number[high]>=pivotkey)
				high--;
			number[low]=number[high];
			while(low<high && number[low]<=pivotkey)
				low++;
			number[high]=number[low];		
		}
		number[low]=tempt;
		return low;	
	}	
}

 

Sort

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号