码迷,mamicode.com
首页 > 编程语言 > 详细

快速排序(内排序)

时间:2015-03-01 23:38:14      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

技术分享
 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

 

快速排序(内排序)

标签:

原文地址:http://www.cnblogs.com/trfizeng/p/4307718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!