提交 79f359f9 authored 作者: Thomas Mueller's avatar Thomas Mueller

Javadocs.

上级 1e9a21cf
...@@ -19,8 +19,14 @@ import org.h2.test.TestBase; ...@@ -19,8 +19,14 @@ import org.h2.test.TestBase;
*/ */
public class TestSort extends TestBase { public class TestSort extends TestBase {
/**
* The number of times the compare method was called.
*/
AtomicInteger compareCount = new AtomicInteger(); AtomicInteger compareCount = new AtomicInteger();
/**
* The comparison object used in this test.
*/
Comparator<Long> comp = new Comparator<Long>() { Comparator<Long> comp = new Comparator<Long>() {
public int compare(Long o1, Long o2) { public int compare(Long o1, Long o2) {
compareCount.incrementAndGet(); compareCount.incrementAndGet();
...@@ -46,32 +52,32 @@ public class TestSort extends TestBase { ...@@ -46,32 +52,32 @@ public class TestSort extends TestBase {
test(Arrays.class); test(Arrays.class);
} }
void test(Class<?> clazz) throws Exception { private void test(Class<?> clazz) throws Exception {
this.clazz = clazz; this.clazz = clazz;
ordered(array); ordered(array);
shuffle(array); shuffle(array);
stabalize(array); stabilize(array);
test("random"); test("random");
ordered(array); ordered(array);
stabalize(array); stabilize(array);
test("ordered"); test("ordered");
ordered(array); ordered(array);
reverse(array); reverse(array);
stabalize(array); stabilize(array);
test("reverse"); test("reverse");
ordered(array); ordered(array);
stretch(array); stretch(array);
shuffle(array); shuffle(array);
stabalize(array); stabilize(array);
test("few random"); test("few random");
ordered(array); ordered(array);
stretch(array); stretch(array);
stabalize(array); stabilize(array);
test("few ordered"); test("few ordered");
ordered(array); ordered(array);
reverse(array); reverse(array);
stretch(array); stretch(array);
stabalize(array); stabilize(array);
test("few reverse"); test("few reverse");
System.out.println(); System.out.println();
} }
...@@ -89,7 +95,7 @@ public class TestSort extends TestBase { ...@@ -89,7 +95,7 @@ public class TestSort extends TestBase {
clazz.getMethod("sort", Object[].class, Comparator.class).invoke(null, array, comp); clazz.getMethod("sort", Object[].class, Comparator.class).invoke(null, array, comp);
// System.out.printf( // System.out.printf(
// "%4d ms; %10d comparisons sorder: %s data: %s\n", // "%4d ms; %10d comparisons order: %s data: %s\n",
// (System.currentTimeMillis() - t), // (System.currentTimeMillis() - t),
// compareCount.get(), clazz, type); // compareCount.get(), clazz, type);
...@@ -149,7 +155,7 @@ public class TestSort extends TestBase { ...@@ -149,7 +155,7 @@ public class TestSort extends TestBase {
} }
} }
private static void stabalize(Long[] array) { private static void stabilize(Long[] array) {
for (int i = 0; i < array.length; i++) { for (int i = 0; i < array.length; i++) {
array[i] = (array[i] << 32) + i; array[i] = (array[i] << 32) + i;
} }
......
...@@ -81,10 +81,10 @@ public class InPlaceStableMergeSort<T> { ...@@ -81,10 +81,10 @@ public class InPlaceStableMergeSort<T> {
binaryInsertionSort(from, to); binaryInsertionSort(from, to);
return; return;
} }
int middle = (from + to) >>> 1; int m = (from + to) >>> 1;
mergeSort(from, middle); mergeSort(from, m);
mergeSort(middle + 1, to); mergeSort(m + 1, to);
merge(from, middle + 1, to); merge(from, m + 1, to);
} }
/** /**
...@@ -165,21 +165,21 @@ public class InPlaceStableMergeSort<T> { ...@@ -165,21 +165,21 @@ public class InPlaceStableMergeSort<T> {
*/ */
private void mergeBig(int from, int second, int to) { private void mergeBig(int from, int second, int to) {
int len1 = second - from, len2 = to - second + 1; int len1 = second - from, len2 = to - second + 1;
int firstCut, secondCut, newMid; int firstCut, secondCut, newSecond;
if (len1 > len2) { if (len1 > len2) {
firstCut = from + len1 / 2; firstCut = from + len1 / 2;
secondCut = findLower(data[firstCut], second, to); secondCut = findLower(data[firstCut], second, to);
int len = secondCut - second; int len = secondCut - second;
newMid = firstCut + len; newSecond = firstCut + len;
} else { } else {
int len = len2 / 2; int len = len2 / 2;
secondCut = second + len; secondCut = second + len;
firstCut = findUpper(data[secondCut], from, second - 1); firstCut = findUpper(data[secondCut], from, second - 1);
newMid = firstCut + len; newSecond = firstCut + len;
} }
swapBlocks(firstCut, second, secondCut - 1); swapBlocks(firstCut, second, secondCut - 1);
merge(from, firstCut, newMid - 1); merge(from, firstCut, newSecond - 1);
merge(newMid, secondCut, to); merge(newSecond, secondCut, to);
} }
/** /**
...@@ -228,9 +228,9 @@ public class InPlaceStableMergeSort<T> { ...@@ -228,9 +228,9 @@ public class InPlaceStableMergeSort<T> {
int len = to - from + 1, half; int len = to - from + 1, half;
while (len > 0) { while (len > 0) {
half = len / 2; half = len / 2;
int mid = from + half; int m = from + half;
if (comp.compare(data[mid], x) < 0) { if (comp.compare(data[m], x) < 0) {
from = mid + 1; from = m + 1;
len = len - half - 1; len = len - half - 1;
} else { } else {
len = half; len = half;
...@@ -252,9 +252,9 @@ public class InPlaceStableMergeSort<T> { ...@@ -252,9 +252,9 @@ public class InPlaceStableMergeSort<T> {
int len = to - from + 1, half; int len = to - from + 1, half;
while (len > 0) { while (len > 0) {
half = len / 2; half = len / 2;
int mid = from + half; int m = from + half;
if (comp.compare(data[mid], x) <= 0) { if (comp.compare(data[m], x) <= 0) {
from = mid + 1; from = m + 1;
len = len - half - 1; len = len - half - 1;
} else { } else {
len = half; len = half;
......
...@@ -247,13 +247,13 @@ public class InPlaceStableQuicksort<T> { ...@@ -247,13 +247,13 @@ public class InPlaceStableQuicksort<T> {
} }
/** /**
* Select the kth smallest element. * Select the specified element.
* *
* @param data the array * @param data the array
* @param from the index of the first element * @param from the index of the first element
* @param to the index of the last element * @param to the index of the last element
* @param k which element to return * @param k which element to return (1 means the lowest)
* @return the kth smallest element * @return the specified element
*/ */
private T select(T[] data, int from, int to, int k) { private T select(T[] data, int from, int to, int k) {
while (true) { while (true) {
...@@ -272,7 +272,7 @@ public class InPlaceStableQuicksort<T> { ...@@ -272,7 +272,7 @@ public class InPlaceStableQuicksort<T> {
} }
/** /**
* Partition the elements to select the kth element. * Partition the elements to select an element.
* *
* @param data the array * @param data the array
* @param from the index of the first element * @param from the index of the first element
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论