提交 71350c23 编写于 作者: GreyZeng's avatar GreyZeng

quick sort ref

上级 863a5494
......@@ -18,25 +18,18 @@ import java.util.Stack;
// 3)把每一种情况都列出来,会有每种情况下的时间复杂度,但概率都是1/N
// 4)那么所有情况都考虑,时间复杂度就是这种概率模型下的长期期望!
// 时间复杂度O(N*logN),额外空间复杂度O(logN)都是这么来的。
public class LintCode_0464_SortIntegersII {
public static void swap(int[] arr, int l, int r) {
if (l != r) {
arr[l] = arr[l] ^ arr[r];
arr[r] = arr[l] ^ arr[r];
arr[l] = arr[l] ^ arr[r];
}
}
public class Code_QuickSort {
// 递归方法
public void sortIntegers2(int[] a) {
public static void sort1(int[] a) {
if (null == a || a.length <= 1) {
return;
}
quickSort(a, 0, a.length - 1);
}
public void quickSort(int[] arr, int l, int r) {
// 快排算法递归解法
public static void quickSort(int[] arr, int l, int r) {
if (l < r) {
int p = l + (int) (Math.random() * (r - l));
swap(arr, p, r);
......@@ -46,7 +39,7 @@ public class LintCode_0464_SortIntegersII {
}
}
public int[] sortColors(int[] arr, int l, int r) {
public static int[] sortColors(int[] arr, int l, int r) {
int i = l;
int p = arr[r];
int s = l - 1;
......@@ -64,7 +57,8 @@ public class LintCode_0464_SortIntegersII {
return new int[]{s + 1, e - 1};
}
public void sortIntegers21(int[] a) {
// 快速排序非递归版本
public static void sort2(int[] a) {
if (null == a || a.length <= 1) {
return;
}
......@@ -87,84 +81,8 @@ public class LintCode_0464_SortIntegersII {
}
}
// merge sort
public void sortIntegers23(int[] a) {
if (null == a || a.length <= 1) {
return;
}
mergeSort(a, 0, a.length - 1);
}
public void mergeSort(int[] arr, int l, int r) {
if (l != r) {
int m = l + ((r - l) >> 1);
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
public void merge(int[] arr, int l, int m, int r) {
int[] help = new int[r - l + 1];
int i = 0;
int ls = l;
int rs = m + 1;
while (ls <= m && rs <= r) {
if (arr[ls] <= arr[rs]) {
help[i++] = arr[ls++];
} else {
help[i++] = arr[rs++];
}
}
while (ls <= m) {
help[i++] = arr[ls++];
}
while (rs <= r) {
help[i++] = arr[rs++];
}
for (i = 0; i < help.length; i++) {
arr[l + i] = help[i];
}
}
// heap sort
public void sortIntegers24(int[] arr) {
if (null == arr || arr.length < 2) {
return;
}
for (int i = arr.length - 1; i >= 0; i--) {
heapify(arr, i, arr.length);
}
int heapSize = arr.length;
// smallest to the arr.length - 1;
swap(arr, 0, --heapSize);
while (heapSize > 0) {
heapify(arr, 0, heapSize);
swap(arr, 0, --heapSize);
}
}
public void heapify(int[] arr, int i, int size) {
int leftChildIndex = 2 * i + 1;
while (leftChildIndex < size) {
int largest =
leftChildIndex + 1 < size && arr[leftChildIndex + 1] >= arr[leftChildIndex]
? leftChildIndex + 1
: leftChildIndex;
largest = arr[i] > arr[largest] ? i : largest;
if (largest == i) {
break;
}
swap(arr, largest, i);
i = largest;
leftChildIndex = 2 * i + 1;
}
}
// 快速排序非递归版本
public class Op {
public static class Op {
public int l;
public int r;
......@@ -173,4 +91,13 @@ public class LintCode_0464_SortIntegersII {
this.r = r;
}
}
public static void swap(int[] arr, int l, int r) {
if (l != r) {
arr[l] = arr[l] ^ arr[r];
arr[r] = arr[l] ^ arr[r];
arr[l] = arr[l] ^ arr[r];
}
}
}
package git.snippet.quicksort;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static git.snippet.common.Generator.copyArray;
import static git.snippet.common.Generator.generateRandomArray;
@DisplayName("快速排序")
public class Code_QuickSortTest {
@Test
@DisplayName("快速排序递归解法测试")
public void mergeSortTest2() {
int times = 500000; // 测试的次数
int maxSize = 100; // 数组的最大长度是100
int maxValue = 100; // 数组元素的大小[-100,100]
for (int i = 0; i < times; i++) {
int[] arr1 = generateRandomArray(maxSize, maxValue);
int[] arr4 = copyArray(arr1);
Code_QuickSort.sort1(arr1);
Arrays.sort(arr4);
Assertions.assertArrayEquals(arr1, arr4);
}
}
@Test
@DisplayName("快速排序非递归解法测试")
public void sortTest1() {
int times = 500000; // 测试的次数
int maxSize = 100; // 数组的最大长度是100
int maxValue = 100; // 数组元素的大小[-100,100]
for (int i = 0; i < times; i++) {
int[] arr1 = generateRandomArray(maxSize, maxValue);
int[] arr4 = copyArray(arr1);
Code_QuickSort.sort2(arr1);
Arrays.sort(arr4);
Assertions.assertArrayEquals(arr1, arr4);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册