# Simple sorting algorithm for arrays

**URL:** <https://techcommunity.softwareag.com/t/simple-sorting-algorithm-for-arrays/100954>\
**Category:** Forum\
**Tags:** Adabas-Natural, Natural, Natural-Code-Samples\
**Created:** [July 12, 2012, 1:28pm UTC](https://techcommunity.softwareag.com/t/simple-sorting-algorithm-for-arrays/100954 "2012-07-12T13:28:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [July 12, 2012, 1:28pm UTC](https://techcommunity.softwareag.com/t/simple-sorting-algorithm-for-arrays/100954/1 "2012-07-12T13:28:11Z")

</div>

Hello all!

The logic END-ALL SORT … END-SORT has a very bad performance for small amounts of data on our system - i.e. small arrays.

So here’s my quick’n’dirty implementation of a bubble sort. Please feel free to improve it - but please let the community know your improvements.

```auto
define data parameter
1 #sorting-tab(1:*)
  2 #values (A) dynamic
  2 #indexes (I4) /* e.g. for referencing a table-line
*
local
*
1 #index (I4)
1 #value (B) dynamic
*
1 #i (I4)
1 #n (I4)
1 #newn (I4)
1 #occ-1 (I4)
end-define
*
#n := *OCC(#sorting-tab.#values)
repeat
*
  #newn := 2
  #occ-1 := #n - 1
  for #i = 1 to #occ-1
    if #sorting-tab.#values(#i) > #sorting-tab.#values(#i + 1)
*
      #value := #sorting-tab.#values(#i)
      #sorting-tab.#values(#i) := #sorting-tab.#values(#i + 1)
      #sorting-tab.#values(#i + 1) := #value
      #index := #sorting-tab.#indexes(#i)
      #sorting-tab.#indexes(#i) := #sorting-tab.#indexes(#i + 1)
      #sorting-tab.#indexes(#i + 1) := #index
*
      #newn := #i + 1
    end-if
  end-for
  #n := #newn
*
  if #n <= 2
    escape bottom
  end-if
end-repeat
*
end
```

regards

Matthias

---

<div class="post-metadata">

**Author:** ![Steve\_Robinson](https://sea1.discourse-cdn.com/techcommunity/user_avatar/techcommunity.softwareag.com/steve_robinson/32/107_2.png) [@Steve\_Robinson](https://techcommunity.softwareag.com/u/Steve_Robinson)\
**Post date:** [July 13, 2012, 11:39am UTC](https://techcommunity.softwareag.com/t/simple-sorting-algorithm-for-arrays/100954/2 "2012-07-13T11:39:41Z")

</div>

Hi Matthias;

A number of years ago I wrote a bunch of sorts in Natural. Then I compared them to Natural’s own SORT. I did not even come close to Natural’s times. As I recall, a double bubble sort came closest, but it was 50% longer than Natural. I will have to see if I can find the old library where those tests would reside.

Have you tried both DYNAMIC and non DYNAMIC lengthed values, and X-array and non X-arrays? My guess is that perhaps the SORT does not deal well with variability in the data structure. For example, the IF statement has to deal with length values whereas with non DYNAMIC values, there is no length. Perhaps SORT has some less than optimal code for dealing with lengths. You might run your comparison with everything fixed ( no X-array, no DYNAMIC).

I too had an “index” array. Since I was playing with fixed lengthed fields then, what I did was define them as:

```
           1 #sort-field (a12/1:20) /* note, keeping within the old A253 constraint. old habits die hard
           1 redefine #sort-field
              2 #value (a10)   
              2 #index (n2)

```

Then you can reduce your six assigns to three. More importantly, you reduce your subscript use by half.

steve

---

<div class="post-metadata">

**Author:** ![Matthias1](https://avatars.discourse-cdn.com/v4/letter/m/9fc348/32.png) [@Matthias1](https://techcommunity.softwareag.com/u/Matthias1)\
**Post date:** [July 20, 2012, 1:03pm UTC](https://techcommunity.softwareag.com/t/simple-sorting-algorithm-for-arrays/100954/3 "2012-07-20T13:03:42Z")

</div>

Hi Steve Robinson!

> [@Steve Robinson](#):
>
> I will have to see if I can find the old library where those tests would reside.

Sounds interesting…

> [@](#):
>
> Have you tried both DYNAMIC and non DYNAMIC lengthed values, and X-array and non X-arrays?

Natural’s SORT is not able to deal with dynamics…

```auto
define data local
01 #myarray (A/9) dynamic
init <'bla','blub','foo','bar','zoo','a','c','bla'>
01 #i1 (I1)
end-define
for #i1 = 1 to 8
  ignore
end-all
sort by #myarray(#i1) using #i1 /* NAT0232 here
  display #i1 #myarray(#I1)
end-sort
end
```

Next thing is that you can’t nest Natural’s SORT. So this is one more reason to write an own sort…

Regards,

Matthias

---

<div class="post-metadata">

**Author:** ![Brenton\_Taylor](https://avatars.discourse-cdn.com/v4/letter/b/4491bb/32.png) [@Brenton\_Taylor](https://techcommunity.softwareag.com/u/Brenton_Taylor)\
**Post date:** [November 4, 2019, 8:37am UTC](https://techcommunity.softwareag.com/t/simple-sorting-algorithm-for-arrays/100954/4 "2019-11-04T08:37:12Z")

</div>

Hello my friend  
Thanks for attaching a sample code of the table sort  
Thank

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/techcommunity/original/3X/c/d/cd19967bb605d28a72d77eb83a1cae22f3df2eb8.svg) [@system](https://techcommunity.softwareag.com/u/system)\
**Post date:** [April 2, 2021, 3:02pm UTC](https://techcommunity.softwareag.com/t/simple-sorting-algorithm-for-arrays/100954/5 "2021-04-02T15:02:09Z")

</div>


