MergeSort

Posted on 2007-02-08 10:42  QT_pixy  阅读(584)  评论(0)    收藏  举报
MergeSort, I didnot use median of 3 method to choose pivot, instead, I use center = (left + right)/2 as a pivot to partition the array. Note, when left + CUTOFF <= right, which means there is only CUTOFF(5) elements in the subarray. Then we do not continue the MergeSort, we change to use insertion sort since it is more efficient than MergeSort when we have a small array.
/**************************
 * Merge Sort
 *
 * Author: QT_pixy
 * Date: Feb. 5, 07
 *************************
*/



public class mergesort
{
    
private static final int CUTOFF = 5;

    
public static <AnyType extends Comparable<? super AnyType>>
    
void MergeSort(AnyType [] a)
    
{
        AnyType [ ] tmpArray 
= (AnyType[]) new Comparable[ a.length ];

        sort( a, tmpArray, 
0, a.length - 1 );
    }

    
    
private static <AnyType extends Comparable<? super AnyType>>
    
void sort( AnyType [ ] a, AnyType [ ] tmpArray, int left, int right )
    
{
        
if(left + CUTOFF <= right )
        
{
            
int center = ( left + right ) / 2;
            sort( a, tmpArray, left, center );
            sort( a, tmpArray, center 
+ 1, right );
            merge( a, tmpArray, left, center 
+ 1, right );
        }

        
else
            insertionSort(a, left, right);
    }


    
private static <AnyType extends Comparable<? super AnyType>>
    
void insertionSort( AnyType [ ] a, int left, int right )
    
{
        
int j;

        
forint p = left + 1; p < right + 1; p++ )
        
{
            AnyType tmp 
= a[ p ];
            
for( j = p; j > left && tmp.compareTo( a[ j - 1 ] ) < 0; j-- )
                a[ j ] 
= a[ j - 1 ];
            a[ j ] 
= tmp;
        }

    }

    
    
private static <AnyType extends Comparable<? super AnyType>>
    
void merge( AnyType [ ] a, AnyType [ ] tmpArray,
                
int leftPos, int rightPos, int rightEnd )
    
{
        
int leftEnd = rightPos - 1;
        
int tmpPos = leftPos;
        
int numElements = rightEnd - leftPos + 1;

        
// Main loop
        while( leftPos <= leftEnd && rightPos <= rightEnd )
            
if( a[ leftPos ].compareTo( a[ rightPos ] ) <= 0 )
                tmpArray[ tmpPos
++ ] = a[ leftPos++ ];
            
else
                tmpArray[ tmpPos
++ ] = a[ rightPos++ ];

        
while( leftPos <= leftEnd )    // Copy rest of first half
            tmpArray[ tmpPos++ ] = a[ leftPos++ ];

        
while( rightPos <= rightEnd )  // Copy rest of right half
            tmpArray[ tmpPos++ ] = a[ rightPos++ ];

        
// Copy tmpArray back
        forint i = 0; i < numElements; i++, rightEnd-- )
            a[ rightEnd ] 
= tmpArray[ rightEnd ];
    }


}
Here is the main program which is used to test the sorting program.
/**************************
 * Merge Sort
 *
 * Author: QT_pixy
 * Date: Feb. 5, 07
 *************************
*/


import java.util.Random;

public class Main3
{
    
private static <AnyType extends Comparable<? super AnyType>>
    
void print(AnyType [] A)
    
{
        
for (int i = 0 ; i < A.length; i++)
        
{
            System.out.print(A[i] 
+ " " );
        }

        System.out.println();
    }


    
public static void main(String[] Args)
    
{
        Integer [] intArray1 
= {10252345222341470623201524};

        Integer [] intArray 
= intArray1;
        
        Random rand 
= new Random();
        
for(int i = 0; i < 15; i++)
            intArray[i] 
= rand.nextInt(100);
            
        
for (int i = 0 ; i < intArray.length; i++)
        
{
            System.out.print(intArray[i] 
+ " " );
        }

        System.out.println();

        mergesort.MergeSort(intArray);
        print(intArray);
    }

}

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3