J
Jeffrey Stedfast
Going through some of my old code (currently unemployed so have a lot of
free time ;-)
In the following implementation of Binary Insertion Sort, there is at
least 1 bug (as corner case as it might be; hopefully it's also the only
bug or I might be embarrassed).
This probably won't be much of a challenge to the CLC veterans to find,
but hopefully it will present enough of a puzzle to some subset of this
newsgroup's readers to be somewhat of a challenge (and hopefully in that
challenge, an element of fun).
With all the students asking for the easy way out with their homework
assignments on this newsgroup, I suppose the way to handle this would be
(since I don't have a website any longer...), if you want to check your
answer, feel free to email me requesting the answer and I will respond
(or post here if you believe I'm not trying to get you to do my homework
for me ;-)
Jeff
void
BinaryInsertionSort (int *a, size_t n)
{
size_t hi, lo, m, i;
int tmp;
for (i = 1; i < n; i++) {
lo = 0, hi = i;
m = i >> 1;
do {
if (a > a[m]) {
lo = m + 1;
} else if (a < a[m]) {
hi = m;
} else
break;
m = (hi + lo) >> 1;
} while (lo < hi);
if (m < i) {
tmp = a;
memmove (a + m + 1, a + m, sizeof (type) * (i - m));
a[m] = tmp;
}
}
}
free time ;-)
In the following implementation of Binary Insertion Sort, there is at
least 1 bug (as corner case as it might be; hopefully it's also the only
bug or I might be embarrassed).
This probably won't be much of a challenge to the CLC veterans to find,
but hopefully it will present enough of a puzzle to some subset of this
newsgroup's readers to be somewhat of a challenge (and hopefully in that
challenge, an element of fun).
With all the students asking for the easy way out with their homework
assignments on this newsgroup, I suppose the way to handle this would be
(since I don't have a website any longer...), if you want to check your
answer, feel free to email me requesting the answer and I will respond
(or post here if you believe I'm not trying to get you to do my homework
for me ;-)
Jeff
void
BinaryInsertionSort (int *a, size_t n)
{
size_t hi, lo, m, i;
int tmp;
for (i = 1; i < n; i++) {
lo = 0, hi = i;
m = i >> 1;
do {
if (a > a[m]) {
lo = m + 1;
} else if (a < a[m]) {
hi = m;
} else
break;
m = (hi + lo) >> 1;
} while (lo < hi);
if (m < i) {
tmp = a;
memmove (a + m + 1, a + m, sizeof (type) * (i - m));
a[m] = tmp;
}
}
}