我的快速排序

 

  1package sort;
  2
  3/** */
  4/**
  5 * @author 
  6 * @version 1.0
  7 */

  8public class QuickSort
  9{
 10    @SuppressWarnings("unchecked")
 11    public static void sort(Comparable[] array)
 12    {
 13        sort(array, 0, array.length - 1);
 14    }

 15    
 16    @SuppressWarnings("unchecked")
 17    private static void sort(Comparable[] array, int begin, int end)
 18    {
 19        if((end - begin) < 9)
 20        {
 21            insertionSort(array, begin, end);
 22            return;
 23        }

 24        int[] positions = choosePositions(array, begin, end);
 25        swap(array, positions[1], end);
 26        int first = findPositionAndSwap(array, begin, end - 1, positions[0]);
 27        int second = findPositionAndSwap(array, first + 1, end, end);
 28        positions = null;
 29        sort(array, begin, first - 1);
 30        sort(array, first + 1, second - 1);
 31        sort(array, second + 1, end);
 32    }

 33    
 34    @SuppressWarnings("unchecked")
 35    private static void insertionSort(Comparable[] array, int[] positions)
 36    {
 37        int range = 0;
 38        while(range < positions.length - 1)
 39        {
 40            int pointer = range + 1;
 41            while(pointer > 0)
 42            {
 43                if(array[positions[pointer]]
 44                        .compareTo(array[positions[pointer - 1]]) < 0)
 45                {
 46                    Comparable temp = array[positions[pointer]];
 47                    array[positions[pointer]] = array[positions[pointer - 1]];
 48                    array[positions[pointer - 1]] = temp;
 49                    pointer--;
 50                }

 51                else
 52                {
 53                    break;
 54                }

 55            }

 56            range++;
 57        }

 58    }

 59    
 60    @SuppressWarnings("unchecked")
 61    private static void insertionSort(Comparable[] array, int begin, int end)
 62    {
 63        int range = begin;
 64        while(range < end)
 65        {
 66            int pointer = range + 1;
 67            while(pointer > begin)
 68            {
 69                if(array[pointer].compareTo(array[pointer - 1]) < 0)
 70                {
 71                    Comparable temp = array[pointer];
 72                    array[pointer] = array[pointer - 1];
 73                    array[pointer - 1= temp;
 74                    pointer--;
 75                }

 76                else
 77                {
 78                    break;
 79                }

 80            }

 81            range++;
 82        }

 83    }

 84    
 85    @SuppressWarnings("unchecked")
 86    public static void swap(Comparable[] array, int left, int right)
 87    {
 88        Comparable temp = array[left];
 89        array[left] = array[right];
 90        array[right] = temp;
 91    }

 92    
 93    @SuppressWarnings("unchecked")
 94    private static int findPositionAndSwap(Comparable[] array, int left,
 95            int right, int position)
 96    {
 97        Comparable comparator = array[position];
 98        int tempLeft = left;
 99        int tempRight = right;
100        while(true)
101        {
102            while(array[tempLeft].compareTo(comparator) <= 0)
103            {
104                if(tempLeft == right)
105                {
106                    swap(array, right, position);
107                    return right;
108                }

109                tempLeft = tempLeft + 1;
110            }

111            while(comparator.compareTo(array[tempRight]) <= 0)
112            {
113                if(tempRight == left)
114                {
115                    swap(array, left, position);
116                    return left;
117                }

118                tempRight = tempRight - 1;
119            }

120            if(tempLeft > tempRight)
121            {
122                if(position > tempLeft)
123                {
124                    swap(array, tempLeft, position);
125                    return tempLeft;
126                }

127                else if(position < tempLeft)
128                {
129                    tempLeft = tempLeft - 1;
130                    swap(array, tempLeft, position);
131                    return tempLeft;
132                }

133            }

134            swap(array, tempLeft, tempRight);
135            tempLeft = tempLeft + 1;
136            tempRight = tempRight - 1;
137        }

138    }

139    
140    @SuppressWarnings("unchecked")
141    private static int[] choosePositions(Comparable[] array, int begin, int end)
142    {
143        int space = (end - begin + 1/ 7;
144        int[] positions = new int[8];
145        positions[0= begin;
146        positions[1= begin + space;
147        positions[2= positions[1+ space;
148        positions[3= positions[2+ space;
149        positions[4= positions[3+ space;
150        positions[5= positions[4+ space;
151        positions[6= positions[5+ space;
152        positions[7= end;
153        insertionSort(array, positions);
154        int[] result = new int[2];
155        result[0= positions[2];
156        result[1= positions[5];
157        return result;
158    }

159}

160
posted @ 2007-09-22 00:35  N/A2011  阅读(270)  评论(0编辑  收藏  举报