<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://pickwiki.org/index.php?action=history&amp;feed=atom&amp;title=QuickSort</id>
	<title>QuickSort - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://pickwiki.org/index.php?action=history&amp;feed=atom&amp;title=QuickSort"/>
	<link rel="alternate" type="text/html" href="https://pickwiki.org/index.php?title=QuickSort&amp;action=history"/>
	<updated>2026-04-28T22:10:14Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://pickwiki.org/index.php?title=QuickSort&amp;diff=2142&amp;oldid=prev</id>
		<title>Conversion script: link fix</title>
		<link rel="alternate" type="text/html" href="https://pickwiki.org/index.php?title=QuickSort&amp;diff=2142&amp;oldid=prev"/>
		<updated>2015-02-26T23:48:55Z</updated>

		<summary type="html">&lt;p&gt;link fix&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;HomePage&amp;gt;&amp;gt;SourceCode&amp;gt;&amp;gt;BasicSource&amp;gt;&amp;gt;[[QuickSort]]&lt;br /&gt;
&lt;br /&gt;
This is a BASIC implementation of the quick sort algorithm.  I originally wrote it for &amp;lt;nowiki&amp;gt;AccuTerm&amp;lt;/nowiki&amp;gt; GUI programming, where I wanted to sort the columns in a grid control (that&amp;#039;s why VMC is a parameter.)  It&amp;#039;s come in handy for any programmatic sorting, and is pretty quick sorting 100,000 or more fields.&lt;br /&gt;
&lt;br /&gt;
Note that LOCATE&amp;#039;s are used for comparisons to make this sort consistent with database sorts.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
      SUBROUTINE QUICKSORT(ITEM, VMC, SORTORDER)&lt;br /&gt;
******&lt;br /&gt;
* QUICKSORT  $Revision: 1.1 $&lt;br /&gt;
* uniVerse BASIC implementation of the quick sort algorithm.&lt;br /&gt;
* Copyright (C) 2004 Rex Gozar&lt;br /&gt;
*&lt;br /&gt;
* This library is free software; you can redistribute it and/or&lt;br /&gt;
* modify it under the terms of the GNU Lesser General Public&lt;br /&gt;
* License as published by the Free Software Foundation; either&lt;br /&gt;
* version 2.1 of the License, or (at your option) any later version.&lt;br /&gt;
*&lt;br /&gt;
* This library is distributed in the hope that it will be useful,&lt;br /&gt;
* but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU&lt;br /&gt;
* Lesser General Public License for more details.&lt;br /&gt;
*&lt;br /&gt;
* You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
* License along with this library; if not, write to the Free Software&lt;br /&gt;
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA&lt;br /&gt;
* http://www.gnu.org/licenses/lgpl.html&lt;br /&gt;
*&lt;br /&gt;
* Rex Gozar&lt;br /&gt;
* rkgozar@juno.com&lt;br /&gt;
******&lt;br /&gt;
* NOTES:&lt;br /&gt;
*    This implementation of the quick sort algorithm is intended for sorting&lt;br /&gt;
* fields in a dynamic array, either by a specified value within the field or&lt;br /&gt;
* the entire field itself.  This makes it useful for sorting tables of values,&lt;br /&gt;
* where the fields/values represent the rows/columns of a table.&lt;br /&gt;
*    When you want to sort by the entire field, set VMC to 0.  Otherwise,&lt;br /&gt;
* to sort by a specific value set VMC to the appropriate value number.&lt;br /&gt;
******&lt;br /&gt;
$OPTIONS DEFAULT&lt;br /&gt;
      EQU QSORT$MIN TO 101&lt;br /&gt;
      IF NOT(VMC MATCHES &amp;quot;0N&amp;quot;) THEN&lt;br /&gt;
         ABORTM &amp;quot;INVALID VMC&amp;quot;&lt;br /&gt;
      END&lt;br /&gt;
      BEGIN CASE&lt;br /&gt;
         CASE SORTORDER = &amp;quot;AL&amp;quot;&lt;br /&gt;
         CASE SORTORDER = &amp;quot;AR&amp;quot;&lt;br /&gt;
         CASE SORTORDER = &amp;quot;DL&amp;quot;&lt;br /&gt;
         CASE SORTORDER = &amp;quot;DR&amp;quot;&lt;br /&gt;
         CASE 1&lt;br /&gt;
            ABORTM &amp;quot;INVALID SORTORDER&amp;quot;&lt;br /&gt;
      END CASE&lt;br /&gt;
      MAXFIELDS = DCOUNT(ITEM, @FM)&lt;br /&gt;
      IF MAXFIELDS &amp;lt; 2 THEN&lt;br /&gt;
         RETURN&lt;br /&gt;
      END&lt;br /&gt;
      DIM ARRAY(MAXFIELDS)&lt;br /&gt;
      MATPARSE ARRAY FROM ITEM&lt;br /&gt;
***&lt;br /&gt;
* Since recursive calls in BASIC are expensive, keep the&lt;br /&gt;
* arguments for recursive processing in a stack.&lt;br /&gt;
***&lt;br /&gt;
      STACK = 1:@VM:MAXFIELDS&lt;br /&gt;
      LOOP&lt;br /&gt;
         RANGE = STACK&amp;lt;1&amp;gt;&lt;br /&gt;
         DEL STACK&amp;lt;1&amp;gt;&lt;br /&gt;
      WHILE RANGE # &amp;quot;&amp;quot; DO&lt;br /&gt;
         BEGPOS = RANGE&amp;lt;1,1&amp;gt;&lt;br /&gt;
         ENDPOS = RANGE&amp;lt;1,2&amp;gt;&lt;br /&gt;
         IF (ENDPOS-BEGPOS) LT QSORT$MIN THEN&lt;br /&gt;
            GOSUB LOCATE.SORT&lt;br /&gt;
         END ELSE&lt;br /&gt;
            GOSUB QUICK.SORT&lt;br /&gt;
         END&lt;br /&gt;
      REPEAT&lt;br /&gt;
      MATBUILD ITEM FROM ARRAY&lt;br /&gt;
      RETURN&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
LOCATE.SORT:&lt;br /&gt;
      NEW = &amp;quot;&amp;quot;&lt;br /&gt;
      FOR J = BEGPOS TO ENDPOS&lt;br /&gt;
         VALUE = LOWER(ARRAY(J)&amp;lt;1,VMC&amp;gt;)&lt;br /&gt;
         ROW = LOWER(ARRAY(J))&lt;br /&gt;
         LOCATE(VALUE, NEW, 1 ; FOUND ; SORTORDER) ELSE NULL&lt;br /&gt;
         INS VALUE BEFORE NEW&amp;lt;1,FOUND&amp;gt;&lt;br /&gt;
         INS ROW BEFORE NEW&amp;lt;2,FOUND&amp;gt;&lt;br /&gt;
      NEXT J&lt;br /&gt;
      FOUND = 0&lt;br /&gt;
      FOR J = BEGPOS TO ENDPOS&lt;br /&gt;
         FOUND += 1&lt;br /&gt;
         ARRAY(J) = RAISE(NEW&amp;lt;2,FOUND&amp;gt;)&lt;br /&gt;
      NEXT J&lt;br /&gt;
      RETURN&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
QUICK.SORT:&lt;br /&gt;
      PPOS = BEGPOS + INT((ENDPOS-BEGPOS)/2)&lt;br /&gt;
      PIVOT = ARRAY(PPOS)&lt;br /&gt;
      ARRAY(PPOS) = ARRAY(ENDPOS)&lt;br /&gt;
      ARRAY(ENDPOS) = PIVOT&lt;br /&gt;
      PIVOT = PIVOT&amp;lt;1,VMC&amp;gt;&lt;br /&gt;
      BEGPTR = BEGPOS&lt;br /&gt;
      ENDPTR = ENDPOS&lt;br /&gt;
      LOOP&lt;br /&gt;
         LOOP&lt;br /&gt;
            VALUE = ARRAY(BEGPTR)&amp;lt;1,VMC&amp;gt;&lt;br /&gt;
            LOCATE(VALUE, PIVOT; FOUND ; SORTORDER) ELSE NULL&lt;br /&gt;
         WHILE BEGPTR &amp;lt; ENDPTR AND FOUND &amp;lt;= 1 DO&lt;br /&gt;
            BEGPTR += 1&lt;br /&gt;
         REPEAT&lt;br /&gt;
         LOOP&lt;br /&gt;
            VALUE = ARRAY(ENDPTR)&amp;lt;1,VMC&amp;gt;&lt;br /&gt;
            LOCATE(VALUE, PIVOT; FOUND ; SORTORDER) ELSE NULL&lt;br /&gt;
         WHILE BEGPTR &amp;lt; ENDPTR AND FOUND &amp;gt; 1 DO&lt;br /&gt;
            ENDPTR -= 1&lt;br /&gt;
         REPEAT&lt;br /&gt;
      WHILE BEGPTR &amp;lt; ENDPTR DO&lt;br /&gt;
         SCRAP = ARRAY(BEGPTR)&lt;br /&gt;
         ARRAY(BEGPTR) = ARRAY(ENDPTR)&lt;br /&gt;
         ARRAY(ENDPTR) = SCRAP&lt;br /&gt;
      REPEAT&lt;br /&gt;
      BEGPTR = ENDPTR - 1&lt;br /&gt;
***&lt;br /&gt;
* special logic to handle repeating values in large&lt;br /&gt;
* data sets&lt;br /&gt;
***&lt;br /&gt;
      LOOP&lt;br /&gt;
      WHILE BEGPOS &amp;lt; BEGPTR AND ARRAY(BEGPTR)&amp;lt;1,VMC&amp;gt; = PIVOT DO&lt;br /&gt;
         BEGPTR -= 1&lt;br /&gt;
      REPEAT&lt;br /&gt;
***&lt;br /&gt;
* sort the two partitions&lt;br /&gt;
***&lt;br /&gt;
      IF BEGPOS &amp;lt; BEGPTR THEN&lt;br /&gt;
         STACK&amp;lt;-1&amp;gt; = BEGPOS:@VM:BEGPTR&lt;br /&gt;
      END&lt;br /&gt;
      IF ENDPTR &amp;lt; ENDPOS THEN&lt;br /&gt;
         STACK&amp;lt;-1&amp;gt; = ENDPTR:@VM:ENDPOS&lt;br /&gt;
      END&lt;br /&gt;
      RETURN&lt;br /&gt;
   END&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Any optimizations or suggestions are welcome :)&lt;br /&gt;
&lt;br /&gt;
http://www.autopower.com/rgozar/pixel.gif&lt;/div&gt;</summary>
		<author><name>Conversion script</name></author>
	</entry>
</feed>