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

Javadocs.

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