Shop OBEX P1 Docs P2 Docs Learn Events
Sorting an array in spin — Parallax Forums

Sorting an array in spin

youngdaveyoungdave Posts: 70
edited 2009-08-11 15:00 in Propeller 1
Dear All
* I have an array of word variables which starts out as Variables[noparse][[/noparse]Population_Size]. During processing, values of the word variables change, and some values drop below a critical level and become ‘dead’.
* I'd appreciate ideas in Spin on packing the array to the left each time a ‘dead’ value is found so that it contains only ‘live’ values, and keeping a track of (updating) ‘Population_Size’.
TIA David Young

Post Edited By Moderator (Bean (Hitt Consulting)) : 8/10/2009 11:59:52 AM GMT

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2009-08-10 10:28
    similar to bubblesort
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2009-08-10 11:38
    Loop through the array. On each element check if the previous element was zero, if so, move it over and clear the element you are moving from to zero. In addition, instead of just checking if the previous element is zero, you can look so many units back and then move by as many zeros as needed.

    BTW - it helps if you put a subject title on the forum thread. You can edit your post and write a title like, "need help on array processing" or something similar. This will help to attract various users and help to index the thread in the search engine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
    www.tdswieter.com
  • SRLMSRLM Posts: 5,045
    edited 2009-08-10 15:42
    The OP didn't actually mention if order was important. If it isn't, then just moving the last element to the hole would, of course, be more efficient.
  • MagIO2MagIO2 Posts: 2,243
    edited 2009-08-10 16:06
    Well ... guess for a good advice we'd need more info. Is the array sorted or not - is calulation round-based or based on neighbours ....

    If the dead values are not needed for further processing (say .. all values are needed to calculate the each single value and processing is kind of round-based), then the processing method can simply use the wordmove instruction to move the rest of the array one element to the left (overwriting the dead value) and decrease the population size variable. Advantage here is that the move instructions are executed in PASM which will be faster than looping through the array in spin.

    The previous post suggested to move the last element to the hole .. why not have 2 index-counters? One for the next element to process and one for the place where to store the result. In the beginning both are equal. If the·current element dies, you only increase the pointer to the next element. If the current element does not die, you store it at the place the result pointer points to and increase both. In the end the difference of the pointers show you how much values died and have to be subtracted from the population size.
    This way the order can be kept.

    Post Edited (MagIO2) : 8/10/2009 4:16:31 PM GMT
  • BaggersBaggers Posts: 3,019
    edited 2009-08-10 16:13
    One good way to do it, that I use on many occassions is once you kill an entry, just copy the last entry (alivecount) over this "dead" entry, and then decrease the number of alivecount by one, that way, your buffer will always only hold the number of alivecount ones in, without having to do a full shunt every time one dies. [noparse];)[/noparse]

    if that's not sounding clear, let me know, and I'll explain it a bit more.

    Baggers.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • youngdaveyoungdave Posts: 70
    edited 2009-08-11 04:29
    Dear All
    Thanks for the suggestions.
    I've got it worked out.

    BTW - it helps if you put a subject title on the forum thread. >>>>>>>>>>>Probably a stupid question, but how do I do this?

    TIA David Young··
  • kwinnkwinn Posts: 8,697
    edited 2009-08-11 04:46
    When you start a new topic or edit an existing one there is a "Subject" line just below the post icons. Put your subject there.
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2009-08-11 15:00
    No stupid questions here David. Feel free to ask. I think Kwinn pointed you in the right direction. We are all newbies at one point.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, 3.0" LCD Composite video display, eProto for SunSPOT
    www.tdswieter.com
Sign In or Register to comment.