play and relax: games for kids games
  Cut the knot: learn to enjoy mathematics
A math books store at a unique math study site. Learn to enjoy mathematics.
Google
Web CTK
Best sites for teachers
Sites for teachers
Sites for parents
Terms of use
Awards

Interactive Activities
CTK Exchange
CTK Insights - a blog

Games & Puzzles
What Is What
Arithmetic/Algebra
Geometry
Probability
Outline Mathematics
Make an Identity
Book Reviews
Eye Opener
Analog Gadgets
Inventor's Paradox
Did you know?...
Proofs
Math as Language
Things Impossible
Visual Illusions
My Logo
Math Poll
Cut The Knot!
MSET99 Talk
Other Math sites
Front Page
Movie shortcuts
Personal info
Privacy Policy

Guest book
News sites

Recommend this site

Best sites for teachers
Sites for teachers
Sites for parents

Education & Parenting

Manifesto: what CTK is about Search CTK Buying a book is a commitment to learning Table of content Things you can find on CTK Chronology of updates Email to Cut The Knot Recommend this page

Various sorting algorithms

The applet below illustrates several sorting algorithms. What is sorted is arrays of numbers. A member of an array is represented by two coordinates: horizontal (index into the array) and vertical (the magnitude of that member). Graphically the members may be represented by small circles or by lines of the corresponding length.

It's not difficult to obtain information about and a programming advice, even the source code, for a specific algorithm, see the references below. A search on the web brings up a multitude of online sources. Some source code is being distributed by the Sun, Inc., the makers of Java. However, the main problem with an algorithm demonstration is in making it interactive. I can say that, on my part, the real challenge in writing the applet was in getting the One step button. The way I do that is running the algorithms in what's called a thread in Java parlance. A thread is a thread of execution, if this helps you better understand what is it about. A single applet or an application may run multiple (execution) threads simultaneously. In my implementation of the applet the drawing part runs in one thread, the algorithms in another. After an algorithm executes one step, it notifies the drawing thread about the fact and suspends itself. The drawing step then draws the new state of the sorting process and then asks the algorithm to resume.

This appears to be a reasonable approach that also allows for algorithm animation with a minimum effort. However the applet had a trouble running in Netscape browsers. The One step button works fine, while the animation had a tendency to hang and needed repeated prompting (pressing the Run button.) The Netscape even issues a warning (that could be seen in the Java console) to the effect that suspending a thread may cause the browser to misbehave. The applet however runs fine in the Internet Explorer v 5.0.

To overcome that problem I have slowed the algorithms artificially; and at this point the applet seems to run in both Netscape 4.6 and Internet Explorer 5.0. However, it still appears to do some violence to Nescape 6.0. The algorithms do run. However the display flickers rhythmically and unpleasantly.

The various algorithms behave differently in different situations. To allow for experimentation and algorithm comparison, the initial array could be created with random data, or strictly increasing data, or in some other standard ways. The same array could be used with different algorithms - just check the Same array button.

Please note: due to an idiosyncrasy of implementation, the array to be sorted is not shown until one of the "action" buttons (Run or One step) has been pressed.)


This applet requires Sun's Java VM 2 which your browser may perceive as a popup. Which it is not. If you want to see the applet work, visit Sun's website at http://www.java.com/en/download/index.jsp, download and install Java VM and enjoy the applet.


Buy this applet
What if applet does not run?

The algorithms have been adapted from http://www.cs.bell-labs.com/cm/cs/pearls/SortAnim.java

References

  1. J. Bentley, Programming Pearls, Addison-Wesley, 2000
  2. A. Levitin, Introduction to The Design & Analysis of Algorithms, Addison-Wesley, 2003
  3. R. Sedgewick, Bundle of Algorithms in C++, Addison-Wesley, 2001, 3rd edition

Copyright © 1996-2008 Alexander Bogomolny

 

 

 

 

 

 

 

 

 

 

29400253Page copy protected against web site content infringement by Copyscape


Search:
Keywords:


Latest on CTK Exchange
try this puzzle ?/?? + ?/?? + ?/? ...
Posted by albert1950
0 messages
07:05 AM, Jul-10-08

Monty Hall Problem
Posted by linkdon
71 messages
03:06 PM, Jul-18-08

Missing information
Posted by roboknight
2 messages
07:32 AM, Jun-22-08

Golden Number linked to 666
Posted by Titus
4 messages
02:53 PM, Jul-19-08

Mistake on the page (an aside, Be ...
Posted by Max
4 messages
10:28 AM, Feb-28-08

trisection of an angle
Posted by Pytheus
2 messages
06:39 AM, Jul-14-08

You can drill a square hole
Posted by Giorgis
1 messages
10:15 PM, Jul-12-08