Sorting an Array


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2003
    Posts
    2,358

    Default Sorting an Array

    How can I sort an array? In case anyone wants one way of doing it here's a ready to run example...

    CounterA var BYTE
    DataTemp var WORD
    MyData var WORD[12]

    SortArray subroutine sorts the example 12 element MyData array in sequence placing the smallest value into MyData[0] and the largest value into MyData[11]...

    SortArray:
    CounterA=0
    SortArrayLoop:
    If MyData[CounterA+1] < MyData[CounterA] then
    DataTemp=MyData[CounterA]
    MyData[CounterA]=MyData[CounterA+1]
    MyData[CounterA+1]=DataTemp
    If CounterA>0 then CounterA=CounterA-2
    endif
    CounterA=CounterA+1
    If CounterA<11 then goto SortArrayLoop
    Return

    By changing the <11 in the line above, you can sort whatever size of array you've got. The value is arraysize-1, so <24 will sort a 25 element array. Just redefine WORDS to BYTES if you want an 8-bit sort routine.

    A routine like this could be much simplified if MeLabs could get SWAP to work on variable index arrays... perhaps a future suggestion.

    Melanie

  2. #2
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    First appologies to the 179 people who viewed the above thread...

    I have discovered that if you post on this forum a left square bracket ] immediately followed by a less-than sign < then it calls strange Masonic rites, invokes an end-of-the-world scenario and corrupts your posting. Leaving spaces between them ensures continuance of the world as we know it. Many thanks to Marcos for pointing out the error. Teaches me to read what I post instead of assuming when you press Send it actually comes out the other end the way you expected.

    The above posting now shows the code CORRECTLY.

    Melanie

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 08:55
  2. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 05:46
  3. Manipulation of string array
    By Benny007 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd April 2008, 21:50
  4. RS232 receive an array (packet)
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2008, 06:02
  5. Word array behaving like byte array.... wierd
    By forgie in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 2nd August 2005, 16:43

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts