冒泡,快速,选择排序 的Java实现
package chapter1;
public class CheckExistDemo {
public static void main(String[] args) {
/* 新建一个数组 */
int[] testArray = { 12, 35, 99, 18, 76 };
CheckExistDemo.selectionSort(testArray);
//CheckExistDemo.bubbleSort(testArray);
//CheckExistDemo.quickSort(testArray, 1, testArray.length - 1);
System.out.print("排序的结果: ");
for (int i = 0; i < testArray.length; i++) {
if (i < testArray.length - 1) {
System.out.print(testArray[i] + ", ");
} else {
System.out.print(testArray[i]);
}
}
System.out.println();
}
/**
* Bubble sort: Time complexity is O(n^2). This one is not recommended.
* 冒泡排序:时间复杂度是O(n = 2)。这个是不推荐的。
*/
public static void bubbleSort(int[] array) {
int dwTemp = 0;
// loop the whole array
for (int i = 0; i < array.length; i++) {
/*
* for each loop, if the first value is greater than the second
* value, then, swap those two values.
* 对于每个循环,如果第一个值大于第二个值,那么,交换这两个值。
*/
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
dwTemp = array[j];
array[j] = array[j + 1];
array[j + 1] = dwTemp;
}
}
}
}
/**
* Quick Sort: it is suitable for sorting big data volume. The
* divide-conquer strategy is used in quicksort. Three steps: 1. Choose a
* pivot value: we take the value of the middle element as pivot value,
* which is in range of sorted values, even if it doesn't present in the
* array. 2. Partition: left to left, right to right. 3. Sort both parts:
* recursively. Time complexity is: the least one is O(N^2), and the average
* is O(nlgn) Two guards, right guard moves first, only if it detects the
* smaller one and stop. Then, left guard starts to move. If it detects
* greater one, then stop. Swap two values. recursively, detect all the
* values. Why does it work? On the partition step, algorithm divides the
* array into two parts and every element a from the left part is less or
* equal than every b from the right part. After completion of the recursion
* calls, both of the parts become sorted.
*
* Complexity analysis:Average complexity is O(nlgn), but the worst case is
* O(n^2).
*
* 快速排序:适合排序大数据量。 快速排序用分而治之的手段。三个步骤:
* 1.选择一个中间值:我们将中间元素的值作为转换值,在排序值的范围内,即使它不存在于数组中。 2.分区:从左到右,从右到右。 3.排序两部分:递归。
* 时间复杂度是:至少一个是O(N ^ 2),平均值是O(nlgn) 两个卫士,右卫兵首先移动,只有当它检测到较小的一个并且停止时。
* 然后,左卫兵开始移动。 如果检测到较大的一个,则停止。 交换两个值。 递归地检测所有的值。 为什么它工作?
* 在分区步骤中,算法将阵列分为两部分,从左侧部分的每个元素a小于或等于右侧每个b。 递归调用完成后,这两个部分都将被排序。
* 复杂度分析:平均复杂度为O(nlgn),但最差的情况是O(n ^ 2)。
*/
public static void quickSort(int[] array, int low, int high) {
int index = CheckExistDemo.partition(array, low, high);
if (low < index - 1) {
quickSort(array, low, index - 1);
}
if (index < high) {
quickSort(array, index, high);
}
}
/**
* 快速排序函数
*
* @param array
* @param low
* @param high
* @return
*/
public static int partition(int[] array, int low, int high) {
int dwLeftGuard = low;
int dwRightGuard = high;
int dwTemp = 0;
int dwKey = array[(low + high) / 2];
while (dwLeftGuard <= dwRightGuard) {
while (array[dwLeftGuard] < dwKey)
dwLeftGuard++;
while (array[dwRightGuard] > dwKey)
dwRightGuard--;
if (dwLeftGuard <= dwRightGuard) {
dwTemp = array[dwLeftGuard];
array[dwLeftGuard] = array[dwRightGuard];
array[dwRightGuard] = dwTemp;
dwLeftGuard++;
dwRightGuard--;
}
}
return dwLeftGuard;
}
/**
* Selection sort: Find the smallest element using a linear scan; Then, find
* the second smallest element, and move it to the front. Again, doing
* linear scan. Time complexity is O(n^2) 选择排序: 使用线性扫描找到最小的元素;
* 然后,找到第二个最小的元素,并移动到前面。 再次,做线性扫描。 时间复杂度是O(n = 2)
*/
public static void selectionSort(int[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = i + 1; j < array.length; j++) {
/* 碰上比前面一个数小的就交换 */
if (array[i] > array[j]) {
int dwTemp = array[j];
array[j] = array[i];
array[i] = dwTemp;
}
}
}
}
}