빠른 정렬 알고리즘(직접 쓰기)

4797 단어 빠른 정렬
            。
package endual;

public class QuickSortClass {

/**
	public void recQuickSort(int left, int right) {
		
		if(right-left <= 0) {
			
			return ;
			
		}else {
		
			int partition = partitionIn(left,right) ; //        key         
			recQuickSort(left,partition-1); //        
			recQuickSort(partition+1,right) ; //        
			
		}

	}
	
	1.                   
	2.                (    )
	3.                {    )
	
	                   key 。
	  key ,           key 
	
	    (     key )
	partitionInt()            
	1.                   ,              pivot  
	2.             。    ,                           
	3.      ,                     ,                 
	           。  ,                  ,                
	          ,      。        ,                    ,  
	          。    ,         。
	
		public void recQuickSort(int left, int right) {
		
		if(right-left <= 0) {
			
			return ;
			
		}else {
		    long provit = theArray[right] ;
			int partition = partitionIn(left,right,pivot) ; //        key         
			recQuickSort(left,partition-1); //        
			recQuickSort(partition+1,right) ; //        
			
		}

	}
	
	                         ,    partitionIntde   。      ,    
	         。                             。
	
	**/
	
	
	
}

 
구분 알고리즘 - 빠른 정렬 알고리즘의 핵심
 
빠른 정렬

     

  
            ,               ,            。
                ,                 ,                   。
   
             :
               。          :      15    15           15     。
                                ,                    。
     
              :
              ,       ,          60    60    ,   60       (   ,
     )。         ,         (   ,        ,                  )。
          ,           ,           ,         :
  
  45,43,54,21,43,【60】,78,98,78,65,67
  
                 ,              。                   。
        ,    ,                      。   ,                。
         。
              ,             。(                  ,   C++      。
       ,leftPtr,    ,       ,rightPtr,     。
 
 
        
              N
 
구분 알고리즘의 코드
 
   package endual;
//       
public class Partition {

	private long[] theArray ;
	private int nElems ;
	
	public Partition(long[] theArray, int nElems) {
		super();
		this.theArray = theArray;
		this.nElems = nElems;
	}
	
	public void dispay() {
		System.out.println("          ") ;
		for (long a : this.theArray) {
			
			System.out.println(a);
			
		}
	}// end dispay
	
	public int partitionIt(int left, int right, long pivot) {
		
		int leftPtr = left - 1 ;
		int rightPtr = right - 1 ;
		
		while (true) {
			
			while (leftPtr < right && theArray[++leftPtr] < pivot) {
				
			}
			
			while (rightPtr < left && theArray[--rightPtr] > pivot) {
				
			}
			
			if (leftPtr >= rightPtr) {
				break ;
			}else {
               
				swap(leftPtr, rightPtr) ;
				
			}	
		}	//end while
		
		return leftPtr ; //        key          
	}

	private void swap(int leftPtr, int rightPtr) {

       long temp ;
       temp = this.theArray[leftPtr] ;
       this.theArray[leftPtr] = this.theArray[rightPtr] ;
       this.theArray[rightPtr] = temp ;
		
	}

}

 
 
빠른 정렬 알고리즘 이론
빠른 정렬 알고리즘
               。        ,       ,         。     NlogN.
                     ,         ,            。     1962    。



          ,           。                       ,          
                。
  ,                  。         key               。           
    。         。

                           ,              
   
   
 
코드























좋은 웹페이지 즐겨찾기