Kaydet (Commit) 82e6a8f8 authored tarafından Guido van Rossum's avatar Guido van Rossum

Quicksort retuned by Tim Peters.

üst bee64533
...@@ -634,7 +634,7 @@ static int ...@@ -634,7 +634,7 @@ static int
insertionsort(array, size, compare) insertionsort(array, size, compare)
PyObject **array; /* Start of array to sort */ PyObject **array; /* Start of array to sort */
int size; /* Number of elements to sort */ int size; /* Number of elements to sort */
PyObject *compare;/* Comparison function object, or NULL for default */ PyObject *compare;/* Comparison function object, or NULL => default */
{ {
register PyObject **a = array; register PyObject **a = array;
register PyObject **end = array+size; register PyObject **end = array+size;
...@@ -645,6 +645,8 @@ insertionsort(array, size, compare) ...@@ -645,6 +645,8 @@ insertionsort(array, size, compare)
register PyObject **q = p; register PyObject **q = p;
while (--q >= a) { while (--q >= a) {
register int k = docompare(*q, key, compare); register int k = docompare(*q, key, compare);
/* if (p-q >= MINSIZE)
fprintf(stderr, "OUCH! %d\n", p-q); */
if (k == CMPERROR) if (k == CMPERROR)
return -1; return -1;
if (k <= 0) if (k <= 0)
...@@ -703,7 +705,7 @@ quicksort(array, size, compare) ...@@ -703,7 +705,7 @@ quicksort(array, size, compare)
n = hi - lo; n = hi - lo;
if (n < MINSIZE) { if (n < MINSIZE) {
/* /*
* skip it. The insertion sort at the end will * skip it. The insertion sort at the end will
* catch these * catch these
*/ */
continue; continue;
...@@ -733,11 +735,14 @@ quicksort(array, size, compare) ...@@ -733,11 +735,14 @@ quicksort(array, size, compare)
{ tmp = *l; *l = *lo; *lo = tmp; } { tmp = *l; *l = *lo; *lo = tmp; }
pivot = *l; pivot = *l;
/* Move pivot off to the side (swap with lo+1) */
*l = *(lo+1); *(lo+1) = pivot;
/* Partition the array */ /* Partition the array */
l = lo+1; l = lo+2;
r = hi-2; r = hi-2;
for (;;) { do {
/* Move left index to element > pivot */ /* Move left index to element >= pivot */
while (l < hi) { while (l < hi) {
k = docompare(*l, pivot, compare); k = docompare(*l, pivot, compare);
if (k == CMPERROR) if (k == CMPERROR)
...@@ -746,8 +751,8 @@ quicksort(array, size, compare) ...@@ -746,8 +751,8 @@ quicksort(array, size, compare)
break; break;
l++; l++;
} }
/* Move right index to element < pivot */ /* Move right index to element <= pivot */
while (r >= lo) { while (r > lo) {
k = docompare(pivot, *r, compare); k = docompare(pivot, *r, compare);
if (k == CMPERROR) if (k == CMPERROR)
return -1; return -1;
...@@ -756,21 +761,17 @@ quicksort(array, size, compare) ...@@ -756,21 +761,17 @@ quicksort(array, size, compare)
r--; r--;
} }
/* If they met, we're through */ /* If they crossed, we're through */
if (l < r) { if (l <= r) {
/* Swap elements and continue */ /* Swap elements and continue */
tmp = *l; *l = *r; *r = tmp; tmp = *l; *l = *r; *r = tmp;
l++; r--; l++; r--;
} }
else if (l == r) {
l++; r--;
break;
}
if (l > r) } while (l <= r);
break;
}
/* Swap pivot back into place; *r <= pivot */
*(lo+1) = *r; *r = pivot;
/* We have now reached the following conditions: /* We have now reached the following conditions:
lo <= r < l <= hi lo <= r < l <= hi
...@@ -785,27 +786,19 @@ quicksort(array, size, compare) ...@@ -785,27 +786,19 @@ quicksort(array, size, compare)
n2 = hi - l; n2 = hi - l;
if (n > n2) { if (n > n2) {
/* First one is bigger */ /* First one is bigger */
if (n > MINSIZE) { lostack[top] = lo;
lostack[top] = lo; histack[top++] = r;
histack[top++] = r; lostack[top] = l;
if (n2 > MINSIZE) { histack[top++] = hi;
lostack[top] = l;
histack[top++] = hi;
}
}
} else { } else {
/* Second one is bigger */ /* Second one is bigger */
if (n2 > MINSIZE) { lostack[top] = l;
lostack[top] = l; histack[top++] = hi;
histack[top++] = hi; lostack[top] = lo;
if (n > MINSIZE) { histack[top++] = r;
lostack[top] = lo;
histack[top++] = r;
}
}
} }
/* Should assert top < STACKSIZE-1 */ /* Should assert top <= STACKSIZE */
} }
/* /*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment