<HTML><HEAD>
<!--
-------
Sorting
-------
-->
<SCRIPT LANGUAGE="JavaScript"><!-- hide from old browsers
/*
THE JAVASCRIPT COOKBOOK by Erica Sadun, webrx@mindspring.com
Copyright (c)1998 by Charles River Media. All Rights Reserved.
This applet can only be re-used or modifed by license holders of the
JavaScript Cookbook CD-ROM. Credit must be given in the source
code and this copyright notice must be maintained. If you do
not hold a license to the JavaScript Cookbook, you may NOT
duplicate or modify this code for your own use.
Use at your own risk. No warranty is given or implied of the suitability
of this applet for any specific application. Neither Erica Sadun nor
Charles River Media will be held responsible for any unwanted effects
due to the use of this applet or any derivative.
*/
// Create an Array
function createArray(n)
{
var t = new Array(n);
for (var i = 0; i < n; i++)
t[i] = 0;
return t;
}
// Initialize Global Variables
myArray = createArray(50) // 50 should be big enough
arrayTop = 0 // Top of myArray
// Show the Array
function showArray()
{
var answer=""
for (var i = 0; i < arrayTop; i++)
answer = "["+myArray[i] + "]" + answer
document.forms[0].myArray.value = answer
// prepare for next entry
document.forms[0].input.focus()
document.forms[0].input.select()
}
// Add value to array
function add()
{
myArray[arrayTop]=""+document.forms[0].input.value
arrayTop++ // increase top
showArray()
}
// Clear the array
function clearArray()
{
arrayTop = 0
document.forms[0].input.value=''
for(var i = 0; i < myArray.length; i++)
myArray[i]='';
showArray()
}
// Sort the array using a simple bubblesort
function sort()
{
for (var i = 0; i < arrayTop; i++)
{
for (var j = i; j < arrayTop; j++)
if (myArray[i] < myArray[j])
{
// out of order: swap them.
var tmp = myArray[i]
myArray[i] = myArray[j]
myArray[j] = tmp
}
}
showArray()
}
<!-- done hiding --></SCRIPT></HEAD>
<BODY bgcolor="ffffff" link="0000ff" vlink="770077"
onLoad="document.forms[0].input.focus();document.forms[0].input.select()">
<FONT COLOR="007777"><H1><IMG SRC="../GRAFX/UTENS.JPG" WIDTH=80 HEIGHT=50
ALIGN = CENTER>Sorting</H1></FONT>
<FONT COLOR="770000">
This applet demonstrates array sorting.
Use the form below to add values to the array.
Type in the box labelled <b>Data</b> and
tap <b>[Add to Array]</b> to insert data at the front
of the array. Tap on the <b>[Sort]</b> button to sort
them using a simple bubble sort.
This will sort in "dictionary" order because all values are
stored as strings. The string "1" comes before "11" which
in turn comes before "2" and that before "ab".
This same algorithm would sort in
numeric order if you pass it an array
of numbers, but this applet
sends it strings.
</FONT>
<BR><BR>
</SCRIPT>
<CENTER><FORM><TABLE BORDER=1>
<TR>
<TD align=center colspan=3><input type="text" name="myArray" value=""
size=40>Array</TD>
</TR>
<TR>
<TD align=center colspan=3><input type="text" name="input"
value="" size=8>Data</TD>
</TR>
<TR>
<TD align=center>
<input type="button" value="Sort" onClick="sort()"></TD>
<TD align=center>
<input type="button" value="Add to Array" onClick="add()"></TD>
<TD align=center>
<input type="button" value="Clear Array" onClick="clearArray()"></TD>
</TR>
</TABLE></FORM></CENTER>
<br><br>
<FONT COLOR="007777"><H2>Discussion</H2></FONT>
<FONT SIZE=4>
The bubble sort algorithm pushes the biggest values to the
top of the array one at a time. This example uses both local
variables (scoped within a function) and global variables (used by many
functions, e.g.
<FONT COLOR="770000">myArray</FONT> and <FONT COLOR="770000">arrayTop</FONT>).
</FONT>
<FONT COLOR="770000"><PRE>
// Sort the array using a simple bubblesort
function sort()
{
for (var i = 0; i < arrayTop; i++)
{
for (var j = i; j < arrayTop; j++)
if (myArray[i] < myArray[j])
{
// out of order: swap them.
var tmp = myArray[i]
myArray[i] = myArray[j]
myArray[j] = tmp
}
}
showArray()
}
</PRE></FONT>
<h5>Copyright ©1996 by Charles River Media, All Rights Reserved</h5>
</BODY>
</HTML>