快速排序的三种实现

1、轴固定在左边的实现。

package com.student.common;

import java.sql.Timestamp;



public class QuickSort1 {
	
	
    public static void main(String[] args) {
	
    	int [] number = {41,24,76,11,45,64,21,69,19,36};  	
      
    	sort(number);
    		     
	}
	
	 public static void sort(int[] number) { 
		 sort(number, 0, number.length-1);    
	 }
    
	 private static void sort(int[] number, int left, int right) {  
		 
		 if(left < right) { 
			
		  int s = number[left]; 
		 
		  int i = left;      
		  int j = right + 1;
		  
		  while(true) {                 
			  
			  while(i + 1 < number.length && number[++i] < s) ;     // 向右找           
			  
			  while(j -1 > -1 && number[--j] > s) ;     // 向左找
			   if(i >= j)     break;
			   
			   swap(number, i, j);       
		 }
		  
		  number[left] = number[j];   
		  number[j] = s;      
		  sort(number, left, j-1);   // 对左边进行递回   
		  sort(number, j+1, right);  // 对右边进行递回
		  
		}
	 }
	 
	 private static void swap(int[] number, int i, int j) {   
		 int t;
		 t = number[i];   
		 number[i] = number[j];
		 number[j] = t;    
	}

}

 

2、轴也参与进来排序的实现。

package com.student.common;

import java.util.Date;



public class QuickSort2 {
	
	  public static void main(String[] args) {
			
	    	int [] number = {41,24,76,11,45,64,21,69,19,36};
	    	
	        sort(number); 
	  
	    	
	 }
		
		 public static void sort(int[] number) { 
			 sort(number, 0, number.length-1);    
		 }
	    
		 private static void sort(int[] number, int left, int right) {  
			 
			 if(left < right) { 
				
			  int s = number[(left+right)/2]; 
			 
			  int i = left-1;      
			  int j = right + 1;
			  
			  while(true) {                 
				  
				  while(i + 1 < number.length && number[++i] < s) ;     // 向右找           
				  
				  while(j -1 > -1 && number[--j] > s) ;     // 向左找
				   if(i >= j)     break;
				   
				   swap(number, i, j);       
			 }
			  
			  
			  sort(number, left, i-1);   // 对左边进行递回   
			  sort(number, j+1, right);  // 对右边进行递回
			  
			}
		 }
		 
		 private static void swap(int[] number, int i, int j) {   
			 int t;
			 t = number[i];   
			 number[i] = number[j];
			 number[j] = t;    
		}

}

 

3、轴的选择是快速排序法的效率关键之一,在这边的快速排序法的轴选择方式更加快了快速排序法的效率,它是来自演算法名书 Introduction to Algorithms 之中。

 

image

先说明这个快速排序法的概念,它以最右边的值s作比较的标准,将整个数列分为三个部份,一个是小于s的部份,一个是大于s的部份,一个是未处理的部份

package com.student.common;

public class QuickSort3 {
	
	
	 public static void main(String[] args) {
			
	    	int [] number = {41,24,76,11,45,64,21,69,19,36};
	    	
	    
	    	sort(number);
	     for(int j : number)
	     System.out.println(j);
	    	
	    	
	}
	
	 public static void sort(int[] number) { 
		 sort(number, 0, number.length-1);    
	 }
    
	 private static void sort(int[] number, int left, int right) {  
		 if(left < right) {   
			 int q = partition(number, left, right);       
			 sort(number, left, q-1);    
			 sort(number, q+1, right);  
		} 
	 }
	
	private static int partition(int number[], int left, int right) {    
		int s = number[right]; 
		int i = left - 1;       
		
		for(int j = left; j < right; j++) {       
			if(number[j] <= s) {       
				i++;         
				swap(number, i, j);     
			}         
		}     
		swap(number, i+1, right);  
		return i+1; 
	}
	
	 private static void swap(int[] number, int i, int j) {   
		 int t;
		 t = number[i];   
		 number[i] = number[j];
		 number[j] = t;    
	}

}
posted @ 2010-05-21 15:07  phpzxh  阅读(408)  评论(0编辑  收藏  举报