Channels ▼

Clay Breshears

Dr. Dobb's Bloggers

Bubble, BubbleSort, and Trouble

February 07, 2012

What's the worst that could happen in this example? During a pass through the books, Banquo may need to make a swap for almost every comparison or he might pause to warm his hands from the cold Scottish mist every so often. If Duncan walks faster or can swap books faster than Banquo (or maybe doesn't find any books that need to be swapped), then he could eventually catch up to Banquo's position. In such a situation, both Banquo and Duncan would compare the same two books and both decide that the books need to swap positions. If Banquo makes the swap and then Duncan makes the swap (having not noticed that Banquo had made the exchange), the books will be left in their same unsorted order. Then, since both have found an exchange, they will each make another full pass through the books.

Two extra runs through the data? Is that the worst? Unfortunately, Banquo and Duncan could meet again at the same adjacent books and achieve the same result. This relationship can perpetuate indefinitely. What I've described here is an example of a livelock situation. Our protagonists are doing some work (computation) by sweeping through the books looking for out-of-order texts, but are unable to proceed to completion due to the actions of some other character (the duplicate swap of books from the same two shelf slots).

If we had some mechanism to ensure Duncan and Banquo would never catch up to the other, we'd have a way to execute Bubblesort in parallel. As an illustration, imagine the shelf of books in a long corridor on a submarine. (Ha ha! I was able to work in the nautical theme.) There are hatches every few yards down the corridor and we impose the rule that, while sorting, whenever Duncan reaches the hatch into the next chamber holding books, he must first peek into the next area. If Banquo is still sorting books in that compartment, Duncan must wait for Banquo to complete his sorting mission in that zone and exit out the hatch at the other end. Obviously, Banquo (as well as Malcolm, Lennox, Ross, Fleance, and Lady Macduff if we add characters to the book-sorting project) must follow the same rule to enter only an empty chamber.

Getting back to programming with threads, one easy way to maximize the number of threads useful in Bubblesort would be to divide the data into a number of non-overlapping zones at least equal to the number of threads. Threads are not allowed to enter a zone until the preceding thread has completed the computations within that zone. These zones are then critical regions of data and I can use an appropriate synchronization mechanism for critical regions to control access to the data zones. If desired, the size of the zones can be dynamically shrunk as the number of unsorted elements decreases in each pass.

How much additional overhead does this modification add to the Bubblesort algorithm? There would need to be a check after each compare-exchange to determine when the end of a zone had been reached. At the end of each zone, a thread would exit the critical region of the current zone (yield the lock) and attempt to enter the succeeding zone. The more zones we install for scalability, the more overhead of exiting and entering critical regions there will be.

Since I've rambled on and on about waves and Shakespearean characters and shelves of books on submarines, I've run out of room. I'll present my version of the code based on the ideas above in the next post. You might take the time between now and then to see if you can develop a solution.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video