budd/src/main/java/io/github/ehlxr/sort/QuickSort.java

176 lines
6.2 KiB
Java
Raw Normal View History

2020-12-10 03:05:29 +00:00
/*
* The MIT License (MIT)
*
* Copyright © 2020 xrv <xrg@live.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2020-12-09 09:55:24 +00:00
package io.github.ehlxr.sort;
2020-09-28 09:58:41 +00:00
import java.util.Arrays;
2020-10-01 09:34:22 +00:00
/**
*
* <p>
* Quicksort
*
*
*
*
*
* @author ehlxr
* @since 2020-10-01 16:40.
*/
2020-09-28 09:58:41 +00:00
public class QuickSort {
2020-10-01 09:34:22 +00:00
public static void main(String[] args) {
2021-07-02 14:40:15 +00:00
// int[] arr = {4, 9, 1, 8, 6, 2};
int[] arr = new int[800_000];
for (int i = 0; i < 800_000; i++) {
arr[i] = (int) (Math.random() * 80_000_000); //生成一个[0, 8000000) 数
}
long startTime = System.currentTimeMillis();
2020-10-01 09:34:22 +00:00
sort(arr, 0, arr.length - 1);
2021-07-02 14:40:15 +00:00
System.out.printf("排序花费时间 %dms.", System.currentTimeMillis() - startTime);
2020-09-28 09:58:41 +00:00
}
/**
2020-10-01 09:34:22 +00:00
* 使listsub-lists
*
* . "基准"pivot
* .
* partition
* . recursively
* <p>
*
* iteration
2020-09-28 09:58:41 +00:00
*
2020-10-01 09:34:22 +00:00
* @param arr
* @param l
* @param r
2020-09-28 09:58:41 +00:00
*/
2020-10-01 09:34:22 +00:00
public static void sort(int[] arr, int l, int r) {
2020-09-28 09:58:41 +00:00
if (l >= r) {
return;
}
if (arr == null || arr.length <= 0) {
return;
}
2020-10-01 09:34:22 +00:00
int i = l, j = r,
// 一般选择数组左边界作为基准
k = l;
2020-09-28 09:58:41 +00:00
while (l < r) {
2020-10-01 09:34:22 +00:00
// 首先循环递减右边界,直到找到小于基准的元素,相互交换
2020-10-01 13:42:54 +00:00
while (l < r && arr[r] >= arr[k]) {
2020-09-28 09:58:41 +00:00
r--;
}
2020-10-01 13:42:54 +00:00
swap(arr, k, r);
2020-10-01 09:34:22 +00:00
k = r;
2020-09-28 09:58:41 +00:00
2020-10-01 09:34:22 +00:00
// 其次循环递增左边界,直到找到大于基准的元素,相互交换
2020-10-01 13:42:54 +00:00
while (l < r && arr[l] <= arr[k]) {
2020-09-28 09:58:41 +00:00
l++;
}
2020-10-01 13:42:54 +00:00
swap(arr, k, l);
2020-10-01 09:34:22 +00:00
k = l;
// 循环以上步骤,直到 l 和 r 相遇
2020-09-28 09:58:41 +00:00
}
2020-10-01 13:42:54 +00:00
// arr[k] = p;
2021-07-02 14:40:15 +00:00
// System.out.println("Sorting: " + Arrays.toString(arr));
2020-09-28 09:58:41 +00:00
2020-10-01 09:34:22 +00:00
sort(arr, i, k - 1);
sort(arr, k + 1, j);
}
/*
*
*/
// public static void sort(int[] arr, int low, int high) {
// if (arr == null || arr.length <= 0) {
// return;
// }
// if (low >= high) {
// return;
// }
//
// int left = low;
// int right = high;
//
// int key = arr[left];
//
// while (left < right) {
// while (left < right && arr[right] >= key) {
// right--;
// }
// while (left < right && arr[left] <= key) {
// left++;
// }
// if (left < right) {
// swap(arr, left, right);
// }
// }
// swap(arr, low, left);
// System.out.println("Sorting: " + Arrays.toString(arr));
// sort(arr, low, left - 1);
// sort(arr, left + 1, high);
// }
/*
*
*/
// public static void sort2(int[] arr, int low, int high) {
// if (arr == null || arr.length <= 0) {
// return;
// }
// if (low >= high) {
// return;
// }
//
// int left = low;
// int right = high;
// // 挖坑1保存基准的值
// int temp = arr[left];
//
// while (left < right) {
// while (left < right && arr[right] >= temp) {
// right--;
// }
// //坑2从后向前找到比基准小的元素插入到基准位置坑1中
// arr[left] = arr[right];
// while (left < right && arr[left] <= temp) {
// left++;
// }
// //坑3从前往后找到比基准大的元素放到刚才挖的坑2中
// arr[right] = arr[left];
// }
// //基准值填补到坑3中准备分治递归快排
// arr[left] = temp;
// System.out.println("Sorting: " + Arrays.toString(arr));
// sort2(arr, low, left - 1);
// sort2(arr, left + 1, high);
// }
public static void swap(int[] arr, int low, int high) {
int tmp = arr[low];
arr[low] = arr[high];
arr[high] = tmp;
2020-09-28 09:58:41 +00:00
}
}