快速排序(内排序)

技术分享

技术分享
 1 /**
 2  * 
 3  */
 4 package com.trfizeng.changesort;
 5 
 6 /**
 7  * @author trfizeng 内部排序 交换排序—快速排序(Quick Sort)
 8  */
 9 public class QuickSort {
10 
11     // 寻找关键字的位置
12     public static int wordKey(int[] array, int low, int high) {
13         int word = array[low];
14         while (low < high) {
15             while (low < high && array[high] >= word) {
16                 // 向前扫描比关键字大的就继续往前走
17                 high--;
18             }
19             // 反之把高位给低位
20             array[low] = array[high];
21 
22             while (low < high && array[low] <= word) {
23                 // 向后扫描,比关键字小的就继续向后扫描
24                 low++;
25             }
26             // 反之,放在高位去
27             array[high] = array[low];
28         }
29         // 把关键字插入到正确的位置
30         array[low] = word;
31         return low;
32     }
33 
34     public static int[] qSort(int[] array, int low, int high) {
35         if (low < high) {
36             // 寻找关键字的位置,因为下一次要根据这个关键字的位置来确定子记录的low和high
37             int wordKey = QuickSort.wordKey(array, low, high);
38             // 对低序列递归
39             QuickSort.qSort(array, low, wordKey - 1);
40             // 对高序列递归
41             QuickSort.qSort(array, wordKey + 1, high);
42         }
43         // 完成排序
44         return array;
45     }
46 
47     public static int[] quickSort(int[] array) {
48         if (array != null && array.length != 0) {
49             int low = 0;
50             int high = array.length - 1;
51             QuickSort.qSort(array, low, high);
52         }
53         return array;
54     }
55 }
View Code
技术分享
 1 package com.trfizeng.test;
 2 
 3 import com.trfizeng.changesort.BubbleSort;
 4 import com.trfizeng.changesort.QuickSort;
 5 import com.trfizeng.insertionsort.StraightInsertionSort;
 6 import com.trfizeng.selectionsort.SimpleSelectionSort;
 7 
 8 /**
 9  * 测试类
10  * 
11  * @author trfizeng
12  * 
13  */
14 public class SortTest {
15     // 待排序数组
16     static int[] array = new int[] { 6, 1, 4, 10, 11, 8, 7, 1, 0 };
17 
18     /**
19      * 直接插入排序法测试
20      */
21     public static void straightInsertionSortTest() {
22         System.out.print("待排序数组:[ ");
23         for (int i = 0; i < array.length; i++) {
24             System.out.print(array[i] + " ");
25         }
26         System.out.print("]   ");
27 
28         array = StraightInsertionSort.straightInsertionSort(array);
29         System.out.print("排好序的数组:[ ");
30         for (int i = 0; i < array.length; i++) {
31             System.out.print(array[i] + " ");
32         }
33         System.out.print("]");
34     }
35 
36     /**
37      * 选择排序
38      */
39     public static void simpleSelectionSort() {
40         System.out.print("待排序数组:[ ");
41         for (int i = 0; i < array.length; i++) {
42             System.out.print(array[i] + " ");
43         }
44         System.out.print("]   ");
45 
46         array = SimpleSelectionSort.simpleSelectionSort(array);
47         System.out.print("排好序的数组:[ ");
48         for (int i = 0; i < array.length; i++) {
49             System.out.print(array[i] + " ");
50         }
51         System.out.print("]");
52     }
53 
54     /**
55      * 冒泡排序
56      */
57     public static void bubbleSort() {
58         System.out.print("待排序数组:[ ");
59         for (int i = 0; i < array.length; i++) {
60             System.out.print(array[i] + " ");
61         }
62         System.out.print("]   ");
63 
64         array = BubbleSort.bubbleSort(array);
65         System.out.print("排好序的数组:[ ");
66         for (int i = 0; i < array.length; i++) {
67             System.out.print(array[i] + " ");
68         }
69         System.out.print("]");
70     }
71 
72     /**
73      * 快速排序
74      */
75     public static void quickSort() {
76         System.out.print("待排序数组:[ ");
77         for (int i = 0; i < array.length; i++) {
78             System.out.print(array[i] + " ");
79         }
80         System.out.print("]   ");
81 
82         array = QuickSort.quickSort(array);
83         System.out.print("排好序的数组:[ ");
84         for (int i = 0; i < array.length; i++) {
85             System.out.print(array[i] + " ");
86         }
87         System.out.print("]");
88     }
89 
90     public static void main(String[] args) {
91         // SortTest.straightInsertionSortTest();
92 
93         // SortTest.simpleSelectionSort();
94 
95         // SortTest.bubbleSort();
96 
97         SortTest.quickSort();
98     }
99 }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。