Implementation of Quick Sort

Posted on 2007-02-06 12:49  QT_pixy  阅读(578)  评论(0)    收藏  举报

For this program I will implement the quick sort. I did not use the median-of-three method to select the pivot. Instead, I pick the middle element of the array (or the sub-arrays) to be the pivot. That is, pivot = A[(left+right)/2].

Note: I use a CUTOFF value of 5. That is, switch to insertion sort when an array has 5 elements or less.

/**************************
 * Quick Sort
 *
 * Author: QT_pixy
 * Date: Feb. 5, 07
 *************************
*/


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

    
public static <AnyType extends Comparable<? super AnyType>>
    
void QSort( AnyType [ ] a )
    
{
        sort(a, 
0, a.length - 1);
    }

    
    
private static <AnyType extends Comparable<? super AnyType>>
    
void sort( AnyType [ ] a, int left, int right )
    
{
        
if(left + CUTOFF <= right)
        
{        
            
int median = (left + right)/2;
            AnyType pivot 
= a[median];
            
            
int i = left - 1, j = right;
            
            swap(a, median, right);
    
            
for( ; ; )
            
{
                
while( a[++i].compareTo( pivot ) < 0 && i <= right - 1)
                
{
                    
                }

                
while( a[--j].compareTo( pivot ) > 0 && j > left)
                
{
                    
                }

                
if( i < j )
                    swap( a, i, j );
                
else
                    
break;         
            }

    
            swap( a, i, right );   
// Restore pivot
    
            sort( a, left, i 
- 1 );    // Sort small elements
            sort( a, i + 1, right );   // Sort large elements
        }

        
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 swap(AnyType [] b, int x, int y)
    
{
        AnyType temp 
= b[x];
        b[x] 
= b[y];
        b[y] 
= temp;
    }

}

Below I add a Main program which implement the quick sort routine.
I used Random class in the public library to generate random number between 0 to 99.
Then use these numbers to test the sorting program.
/**************************
 * Quick Sort
 *
 * Author: QT_pixy
 * Date: Feb. 5, 07
 *************************
*/


import java.util.Random;

public class Main1
{
    
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();
            
        
        qsort.QSort(intArray);
        print(intArray);
    }

}

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