Sorting algorithms/Bubble sort: Difference between revisions
Content added Content deleted
(BBC BASIC solution replaced) |
|||
Line 237: | Line 237: | ||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
The Bubble sort is very inefficient for 99% of cases. This |
The Bubble sort is very inefficient for 99% of cases. This routine uses a couple of 'tricks' to try and mitigate the inefficiency to a limited extent. Note that the array index is assumed to start at zero. |
||
<lang bbcbasic> |
<lang bbcbasic> DIM test(9) |
||
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1 |
|||
PROCbubblesort(test(), 10) |
|||
I%=Size%+1 |
|||
FOR i% = 0 TO 9 |
|||
⚫ | |||
PRINT test(i%) ; |
|||
I%-=1 |
|||
NEXT |
|||
LastChange%=2 |
|||
PRINT |
|||
END |
|||
⚫ | |||
⚫ | |||
DEF PROCbubblesort(a(), n%) |
|||
LOCAL i%, l% |
|||
⚫ | |||
NEXT J% |
|||
l% = 0 |
|||
I%=LastChange% |
|||
FOR i% = 1 TO n%-1 |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
l% = i% |
|||
ENDIF |
|||
NEXT |
|||
n% = l% |
|||
⚫ | |||
⚫ | |||
'''Output:''' |
|||
<pre> |
|||
-31 0 1 2 2 4 65 83 99 782 |
|||
</pre> |
|||
=={{header|C}}== |
=={{header|C}}== |