473,486 Members | 1,640 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

can you find the bug?

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[i] a[m]) {
lo = m + 1;
} else if (a[i] < a[m]) {
hi = m;
} else
break;

m = (hi + lo) >1;
} while (lo < hi);

if (m < i) {
tmp = a[i];
memmove (a + m + 1, a + m, sizeof (type) * (i - m));
a[m] = tmp;
}
}
}
Jan 28 '07 #1
17 1392

"Jeffrey Stedfast" <st******@comcast.netwrote in message
news:tf******************************@comcast.com. ..
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[i] a[m]) {
lo = m + 1;
} else if (a[i] < a[m]) {
hi = m;
} else
break;

m = (hi + lo) >1;
} while (lo < hi);

if (m < i) {
tmp = a[i];
memmove (a + m + 1, a + m, sizeof (type) * (i - m));
a[m] = tmp;
}
}
}
Not compiling is a pretty big corner case:

error: `type' undeclared (first use in this function)
Jan 28 '07 #2
Barry wrote:
"Jeffrey Stedfast" <st******@comcast.netwrote in message
news:tf******************************@comcast.com. ..
>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[i] a[m]) {
lo = m + 1;
} else if (a[i] < a[m]) {
hi = m;
} else
break;

m = (hi + lo) >1;
} while (lo < hi);

if (m < i) {
tmp = a[i];
memmove (a + m + 1, a + m, sizeof (type) * (i - m));
a[m] = tmp;
}
}
}

Not compiling is a pretty big corner case:

error: `type' undeclared (first use in this function)

oops, sorry, change that to 'int'
Jan 28 '07 #3

is it about m = (hi + lo) >1;

( hi+lo ) might overflow ??

On Jan 28, 9:33 pm, Jeffrey Stedfast <stedf...@comcast.netwrote:
Barry wrote:
"Jeffrey Stedfast" <stedf...@comcast.netwrote in message
news:tf******************************@comcast.com. ..
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[i] a[m]) {
lo = m + 1;
} else if (a[i] < a[m]) {
hi = m;
} else
break;
m = (hi + lo) >1;
} while (lo < hi);
if (m < i) {
tmp = a[i];
memmove (a + m + 1, a + m, sizeof (type) * (i - m));
a[m] = tmp;
}
}
}
Not compiling is a pretty big corner case:
error: `type' undeclared (first use in this function)oops, sorry, change that to 'int'- Hide quoted text -- Show quoted text -
Jan 29 '07 #4
Mr.Unknown wrote:
is it about m = (hi + lo) >1;

( hi+lo ) might overflow ??
exactly :)
Jan 29 '07 #5
Mr.Unknown wrote:
>
is it about m = (hi + lo) >1;
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Jan 29 '07 #6
In article <52*************@mid.individual.net>,
Default User <de***********@yahoo.comwrote:
>Mr.Unknown wrote:
>>
is it about m = (hi + lo) >1;

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>
Get a life!

Jan 29 '07 #7
On Jan 29, 10:06 am, Jeffrey Stedfast <stedf...@comcast.netwrote:
Mr.Unknown wrote:
is it about m = (hi + lo) >1;
( hi+lo ) might overflow ??exactly :)
If you are using insertion sort on a list so large that size_t is in
danger of overflow, you had better go out for a cup of coffee.
You can use a donkey and go to South America and pick, roast and grind
it yourself.
In short, insertion sort is for small sets.

Testing for the largest (tested/allowed/possible) input would be a
good idea, no matter what sort of algorithm you choose.

IMO-YMMV.

Jan 29 '07 #8
user923005 said:

<snip>
If you are using insertion sort on a list so large that size_t is in
danger of overflow, you had better go out for a cup of coffee.
Indeed, since size_t (being an unsigned integer type) is *never* in danger
of overflow.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 29 '07 #9


On Jan 29, 2:08 pm, Richard Heathfield <r...@see.sig.invalidwrote:
user923005 said:

<snip>
If you are using insertion sort on a list so large that size_t is in
danger of overflow, you had better go out for a cup of coffee.Indeed, since size_t (being an unsigned integer type) is *never* in danger
of overflow.
Allow me to rephrase that:
"If you are using insertion sort on a list so large that size_t is in
danger of modulo reduction, you had better go out for a cup of
coffee."
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jan 29 '07 #10
user923005 wrote:
>
On Jan 29, 2:08 pm, Richard Heathfield <r...@see.sig.invalidwrote:
>user923005 said:

<snip>
>>If you are using insertion sort on a list so large that size_t is in
danger of overflow, you had better go out for a cup of coffee.Indeed, since size_t (being an unsigned integer type) is *never* in danger
of overflow.

Allow me to rephrase that:
"If you are using insertion sort on a list so large that size_t is in
danger of modulo reduction, you had better go out for a cup of
coffee."
....on today's hardware.

anyways, yes, it is unlikely anyone would feed a large enough dataset to
an insertion sort, but it's still a bug that's easy to fix.

Jeff
Jan 29 '07 #11
Jeffrey Stedfast wrote:
>
Mr.Unknown wrote:
is it about m = (hi + lo) >1;

( hi+lo ) might overflow ??

exactly :)
m = hi / 2 + lo / 2 + (hi & lo & 1);

--
pete
Jan 30 '07 #12
On Jan 29, 4:19 pm, pete <pfil...@mindspring.comwrote:
Jeffrey Stedfast wrote:
Mr.Unknown wrote:
is it about m = (hi + lo) >1;
( hi+lo ) might overflow ??
exactly :)
m = hi / 2 + lo / 2 + (hi & lo & 1);
Or you can retain the shift paradigm by
m = hi >1 + lo >1 + (hi & lo & 1);

Of course, a sensible compiler might do that for you.
Since this is C.L.C:
"Assumes 2's complement! {for the &1 bit}"
;-)

Jan 30 '07 #13
"user923005" <dc*****@connx.comwrites:
On Jan 29, 4:19 pm, pete <pfil...@mindspring.comwrote:
>Jeffrey Stedfast wrote:
Mr.Unknown wrote:
is it about m = (hi + lo) >1;
( hi+lo ) might overflow ??

m = hi / 2 + lo / 2 + (hi & lo & 1);

Or you can retain the shift paradigm by
m = hi >1 + lo >1 + (hi & lo & 1);
That expression is going to have very surprising results, given
C's precedence rules.
Of course, a sensible compiler might do that for you.
Since this is C.L.C:
"Assumes 2's complement! {for the &1 bit}"
All the variables in question have unsigned type, so that's an
unnecessary assumption.
--
Just another C hacker.
Jan 30 '07 #14
pete wrote:
Jeffrey Stedfast wrote:
>Mr.Unknown wrote:
>>is it about m = (hi + lo) >1;

( hi+lo ) might overflow ??

exactly :)

m = hi / 2 + lo / 2 + (hi & lo & 1);
which rounds down. You also have available:

roundup: m = hi/2 + lo/2 + ((hi | lo) & 1);
roundvarh: m = hi/2 + lo/2 + (hi & 1);
roundvarl: m = hi/2 + lo/2 + (lo & 1);

The roundvar* will avoid the bias of roundup and roundown.

Just to point out variants on your useful formation. These are
only valid on 2's complement or unsigned variables.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>

"A man who is right every time is not likely to do very much."
-- Francis Crick, co-discover of DNA
"There is nothing more amazing than stupidity in action."
-- Thomas Matthews
Jan 30 '07 #15
On Jan 30, 1:33 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
"user923005" <dcor...@connx.comwrites:
On Jan 29, 4:19 pm, pete <pfil...@mindspring.comwrote:
Jeffrey Stedfast wrote:
Mr.Unknown wrote:
is it about m = (hi + lo) >1;
( hi+lo ) might overflow ??

m = hi / 2 + lo / 2 + (hi & lo & 1);
Or you can retain the shift paradigm by
m = hi >1 + lo >1 + (hi & lo & 1);

That expression is going to have very surprising results, given
C's precedence rules.
Of course, a sensible compiler might do that for you.
Since this is C.L.C:
"Assumes 2's complement! {for the &1 bit}"

All the variables in question have unsigned type, so that's an
unnecessary assumption.
If you want robustness with respect to signed indexes, number
representation, C90 division, and rounding towards lo (which
many binary search implementations quietly assume), you can
do...

m = ((lo < 0) ^ (hi < 0))
? (lo + hi) / 2 - ((lo + hi) % 2 < 0)
: lo + (hi - lo) / 2;

Of course, if you know that hi and lo are non-negative, then
you can simplify this to the intuitive:

m = lo + (hi - lo) / 2;

Or, if you absolutely insist...

m = lo + ((hi - lo) >1);

--
Peter

Jan 30 '07 #16
On Jan 29, 4:19 pm, pete <pfil...@mindspring.comwrote:
Jeffrey Stedfast wrote:
Mr.Unknown wrote:
is it about m = (hi + lo) >1;
( hi+lo ) might overflow ??
exactly :)

m = hi / 2 + lo / 2 + (hi & lo & 1);
There is a well known alternative in the microprocessor world:

(x + y) = 2*(x & y) + (x ^ y)

So,

m = ((lo ^ hi) >1) + (lo & hi);

--
Paul Hsieh
http://www.pobox.com/~qed/

Jan 30 '07 #17
Peter Nilsson wrote:
On Jan 30, 1:33 pm, Ben Pfaff <b...@cs.stanford.eduwrote:
>"user923005" <dcor...@connx.comwrites:
>>On Jan 29, 4:19 pm, pete <pfil...@mindspring.comwrote:
Jeffrey Stedfast wrote:
Mr.Unknown wrote:
>is it about m = (hi + lo) >1;
>( hi+lo ) might overflow ??
m = hi / 2 + lo / 2 + (hi & lo & 1);
Or you can retain the shift paradigm by
m = hi >1 + lo >1 + (hi & lo & 1);
That expression is going to have very surprising results, given
C's precedence rules.
>>Of course, a sensible compiler might do that for you.
Since this is C.L.C:
"Assumes 2's complement! {for the &1 bit}"
All the variables in question have unsigned type, so that's an
unnecessary assumption.

If you want robustness with respect to signed indexes, number
representation, C90 division, and rounding towards lo (which
many binary search implementations quietly assume), you can
do...

m = ((lo < 0) ^ (hi < 0))
? (lo + hi) / 2 - ((lo + hi) % 2 < 0)
: lo + (hi - lo) / 2;

Of course, if you know that hi and lo are non-negative, then
you can simplify this to the intuitive:

m = lo + (hi - lo) / 2;

Or, if you absolutely insist...

m = lo + ((hi - lo) >1);
this is actually how I fixed it, overall the simplest fix I could think of.
>
--
Peter
Jan 30 '07 #18

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7577
by: Dan Jones | last post by:
I'm writing a script to process a directory tree of images.  In each directory, I need to process each image and generate an HTML file listing all of the images and links to the subdirectories....
7
12794
by: zhou | last post by:
Hi there, We have a compiler specific issue which requires us to force template instantiation. This works fine. The problem comes when I try using std:find() on vector. Since vector has no member...
0
2746
by: amit | last post by:
I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE to do it, the find proerty does it, the results are getting...
0
2561
by: AMIT PUROHIT | last post by:
hi, this is a qry which I m stuck up with I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE(EnvDTE) to do it,...
0
2098
by: amit | last post by:
hi I have created a tool which does a find and replace thru DTE, now after it is done, it opens up a window, "FIND REACHED THE STARTING POINT OF SEARCH" I want to disbale this window...
5
2998
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
3
7175
by: DJTN | last post by:
I'm getting the following error when I try to compile my setup project in VS 2002. I have re-installed the .net framework 1.1 and it didnt solve the problem. WARNING: Unable to find dependency...
3
16466
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
0
11249
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema...
0
7094
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6964
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6839
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7305
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5427
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3066
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.