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


    1 out of 1 members found this post helpful. 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, 07:55
  2. Simple Array Demo
    By Archangel in forum Code Examples
    Replies: 5
    Last Post: - 15th February 2010, 04:46
  3. Manipulation of string array
    By Benny007 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd April 2008, 20:50
  4. RS232 receive an array (packet)
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th February 2008, 05:02
  5. Word array behaving like byte array.... wierd
    By forgie in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 2nd August 2005, 15:43

Members who have read this thread : 3

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