Unit Sort1

*********************************************************************** * This is a small example application which demonstrates the use of * the QSort procedure to sort the contents of a Listbox. * Developed Oct 95 by Barry Schlereth * ??? WHY ??? * The sorted parameter of a Listbox is nice, but what if you want to * sort the strings by their numerical representation not alphabetically? * Or, maybe you have a table and you would like to sort the rows of the * table according to the floating point numbers displayed in one column. * That is what this example shows. I hope you find it useful. * This example can be freely distributed. Be sure to follow the * copyrights shown below. * If you feel very appreciative, a small donation - 1 dollar or a couple * cereal coupons (Special K, Corn Flakes, Cheerios) - may be sent to: * Barry * Box 176 * Syracuse, NY 13215

Classes

TForm1 -

Functions

Compare - This is the companion function Compare.
QSort - ****************************************************************** * QSort - Quick Sort * Adapted for Delphi Pascal by Barry Schlereth Oct 95 * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice and the * original copyright appears in all copies.

Types

Constants

Variables

Form1


Functions


function Compare (const i, j: Integer) : Integer;

This is the companion function Compare. It provides the relationship comparison for QSort. The indicies (i, j) can index into any type of Array, StringList, etc. In real-life you would speed things alot by by building and sorting a dummy floating point array derived from the values in DataBox1.Items instead of converting with each comparison as is shown in this example!

procedure QSort(var a: array of Integer; const lo0, hi0: Integer);

****************************************************************** * QSort - Quick Sort * Adapted for Delphi Pascal by Barry Schlereth Oct 95 * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice and the * original copyright appears in all copies. (Also see below) * THIS SOURCE CODE IS SUPPLIED "AS IS" AND IS NOT WARRANTIED IN ANY * WAY, EXPRESS OR IMPLIED. * Original "C" implementation by James Gosling (see below) * The QSort procedure takes three parameters: * a - an integer array of indices. * lo0 - the lower index of a to sort. * hi0 - the top index of a to sort (Count of a -1) * Qsort requires a companion function, Compare(i, j), which tells * it how to sort the indices. Compare returns -1, 0, +1, (<, =, >) * depending on the relationship of a[i] to a[j]. In this example * Compare(i, j) compares the StrToFloat of Item[i] to Item[j] in * the ListBox (DataBox1). * QSort is recursive - watch your stack when sorting large arrays. *----------------------------------------------------------------- * Quick Sort Algorithm * original implementation by James Gosling v1.6 95/01/31 * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved. * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. Please refer to the file "copyright.html" * for further important copyright and licensing information. * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. *---------------------------------------------------------------------

Types


Constants


Variables

Form1 : TForm1

Public declarations