Cosmic Software
Member
As part of a project that I am working on, I have generated 6 random numbers. The query that I have is how to put these numbers into ascending order. I have used the principle of the bubble sort algorithm, and adapted it for picaxe. Currently, this is the code that I have, working on a 28X:
====CODE====
sort:
check1:
if b6>b7 then swap1
check2:
if b7>b8 then swap2
check3:
if b8>b9 then swap3
check4:
if b9>b10 then swap4
check5:
if b10>b11 then swap5
goto show_sort
swap1:
b12=b6
b6=b7
b7=b12
goto sort
swap2:
b12=b7
b7=b8
b8=b12
goto sort
swap3:
b12=b8
b8=b9
b9=b12
goto sort
swap4:
b12=b9
b9=10
b10=b12
goto sort
swap5:
b12=b10
b10=b11
b10=b12
goto sort
show_sort:
====END CODE====
I have not yet been able to test it, but I have posted it here for two reasons.
1) It appears rather long and messy for what it does, and wondered whether anybody would be able to tidy it up for me.
2)If this code does work, I believe that it could come in useful for other people, because a search on the forum revealed nothing of the sort.
====CODE====
sort:
check1:
if b6>b7 then swap1
check2:
if b7>b8 then swap2
check3:
if b8>b9 then swap3
check4:
if b9>b10 then swap4
check5:
if b10>b11 then swap5
goto show_sort
swap1:
b12=b6
b6=b7
b7=b12
goto sort
swap2:
b12=b7
b7=b8
b8=b12
goto sort
swap3:
b12=b8
b8=b9
b9=b12
goto sort
swap4:
b12=b9
b9=10
b10=b12
goto sort
swap5:
b12=b10
b10=b11
b10=b12
goto sort
show_sort:
====END CODE====
I have not yet been able to test it, but I have posted it here for two reasons.
1) It appears rather long and messy for what it does, and wondered whether anybody would be able to tidy it up for me.
2)If this code does work, I believe that it could come in useful for other people, because a search on the forum revealed nothing of the sort.