我写的5种排序算法的java实现

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

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

16    
17    @SuppressWarnings("unchecked")
18    private static void sort(Comparable[] array, int begin, int end)
19    {
20        for(int i = begin; i <= end; i++)
21        {
22            for(int j = end; j > i; j--)
23            {
24                if(array[j].compareTo(array[j - 1]) < 0)
25                {
26                    Comparable temp = array[j];
27                    array[j] = array[j - 1];
28                    array[j - 1= temp;
29                }

30            }

31        }

32    }

33}

34

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

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

14    
15    @SuppressWarnings("unchecked")
16    private static void sort(Comparable[] array, int begin, int end)
17    {
18        int range = begin;
19        while(range < end)
20        {
21            int pointer = range + 1;
22            while(pointer > begin)
23            {
24                if(array[pointer].compareTo(array[pointer - 1]) < 0)
25                {
26                    Comparable temp = array[pointer];
27                    array[pointer] = array[pointer - 1];
28                    array[pointer - 1= temp;
29                    pointer--;
30                }

31                else
32                {
33                    break;
34                }

35            }

36            range++;
37        }

38    }

39}

40

Merge
 1 package  sort;
 2
 3 /** */ /**
 4 * @author 
 5 * @version 1.0
 6 */
 7 public   class  MergeSort
 8 {
 9    @SuppressWarnings("unchecked")
10    public static void sort(Comparable[] array)
11    {
12        sort(array, 0, array.length - 1);
13    }

14    
15    @SuppressWarnings("unchecked")
16    private static void sort(Comparable[] array, int begin, int end)
17    {
18        if(end > begin)
19        {
20            int middle = (begin + end) / 2;
21            sort(array, begin, middle);
22            sort(array, middle + 1, end);
23            merge(array, begin, middle, end);
24        }

25    }

26    
27    @SuppressWarnings( "unchecked""unused" })
28    private static Comparable[] combine(Comparable[] left, Comparable[] right)
29    {
30        Comparable[] result = new Comparable[left.length + right.length];
31        int leftPointer = 0;
32        int rightPointer = 0;
33        int combinePointer = 0;
34        while(combinePointer < result.length)
35        {
36            if(leftPointer >= left.length)
37            {
38                result[combinePointer] = right[rightPointer];
39                combinePointer++;
40                rightPointer++;
41            }

42            else if(rightPointer >= right.length)
43            {
44                result[combinePointer] = left[leftPointer];
45                combinePointer++;
46                leftPointer++;
47            }

48            else
49            {
50                if(left[leftPointer].compareTo(right[rightPointer]) > 0)
51                {
52                    result[combinePointer] = right[rightPointer];
53                    combinePointer++;
54                    rightPointer++;
55                }

56                else
57                {
58                    result[combinePointer] = left[leftPointer];
59                    combinePointer++;
60                    leftPointer++;
61                }

62            }

63        }

64        return result;
65    }

66    
67    @SuppressWarnings("unchecked")
68    private static void merge(Comparable[] array, int begin, int middle, int end)
69    {
70        int pointer;
71        pointer = 0;
72        Comparable[] left = new Comparable[middle - begin + 1];
73        for(int i = begin; i <= middle; i++)
74        {
75            left[pointer] = array[i];
76            pointer++;
77        }

78        Comparable[] right = new Comparable[end - middle];
79        pointer = 0;
80        for(int i = middle + 1; i <= end; i++)
81        {
82            right[pointer] = array[i];
83            pointer++;
84        }

85        Comparable[] result = combine(left, right);
86        pointer = 0;
87        for(int i = begin; i <= end; i++)
88        {
89            array[i] = result[pointer];
90            pointer++;
91        }

92    }

93}

94

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

  8 public   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

Selection
 1 package  sort;
 2
 3 /** */ /**
 4 * @author 
 5 * @version 1.0
 6 */
 7 public   class  SelectionSort
 8 {
 9    @SuppressWarnings("unchecked")
10    public static void sort(Comparable[] array)
11    {
12        sort(array, 0, array.length - 1);
13    }
14    
15    @SuppressWarnings("unchecked")
16    private static void sort(Comparable[] array, int begin, int end)
17    {
18        for(int i = begin; i <= end; i++)
19        {
20            int smallest = getSmallest(array, i, end);
21            Comparable temp = array[i];
22            array[i] = array[smallest];
23            array[smallest] = temp;
24        }

25    }

26    
27    @SuppressWarnings("unchecked")
28    private static int getSmallest(Comparable[] array, int begin, int end)
29    {
30        Comparable smallest = array[begin];
31        int pointer = begin;
32        for(int i = begin; i <= end; i++)
33        {
34            if(smallest.compareTo(array[i]) > 0)
35            {
36                smallest = array[i];
37                pointer = i;
38            }

39        }

40        return pointer;
41    }

42}

43

转载于:https://www.cnblogs.com/aspxphpjsprb/archive/2007/10/10/919311.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值